forked from Wroclaw/WorkshopTasker
38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
|
/* global defineEventHandler */
|
||
|
|
||
|
import BaaPagination from "../utils/baaPagination";
|
||
|
import { data, database } from "../utils/database";
|
||
|
import { client, orderSummary } from "~/utils/types/database";
|
||
|
|
||
|
export const baaWrapper = new BaaPagination<orderSummary, "id">(
|
||
|
"orderSummaries",
|
||
|
"id",
|
||
|
"*, CONVERT(`client`, CHAR) AS `client`, CONVERT(`user`, CHAR) as `user`",
|
||
|
);
|
||
|
|
||
|
export default defineEventHandler(async (e) => {
|
||
|
const orders = await baaWrapper.RESTget(e, 50, 200);
|
||
|
|
||
|
const uniqueClients: Array<string> = [];
|
||
|
for (const i of orders) {
|
||
|
if (!uniqueClients.includes(i.client))
|
||
|
uniqueClients.push(database.escape(i.client));
|
||
|
}
|
||
|
|
||
|
const [clients] = await database.query(
|
||
|
["SELECT",
|
||
|
"*,",
|
||
|
"CONVERT(`id`, CHAR) AS `id`",
|
||
|
"FROM `clients`",
|
||
|
"WHERE `id` IN",
|
||
|
`(${uniqueClients.join(', ')})`,
|
||
|
].join(" "),
|
||
|
) as data<client>;
|
||
|
|
||
|
const rvalue: Array<Omit<typeof orders, "client"> | { client?: client }> = [];
|
||
|
|
||
|
for (const i of orders)
|
||
|
rvalue.push({ ...i, client: clients.find(e => i.client === e.id) });
|
||
|
return rvalue;
|
||
|
});
|