WorkshopTasker/server/api/orders.get.ts

37 lines
1.1 KiB
TypeScript

import { defineEventHandler } from "h3";
import BaaPagination from "../utils/baaPagination";
import { type data, database } from "../utils/database";
import { type client, type 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;
});