WorkshopTasker/server/api/orders.get.ts
Wroclaw eebf25198d Replace mysql2 with prisma
also I updated packages,
and properly typed api input
a lot of time was spent, I don't remeber what really I did x3
but everything was related to replacing mysql2 with prisma
2023-11-08 05:36:12 +01:00

65 lines
1.6 KiB
TypeScript

import { defineEventHandler } from "h3";
import { type Order, type Client, Prisma } from "@prisma/client";
import getPaginatedParameters, { type pageData } from "../utils/baaPageParsing";
import { database } from "../utils/database";
import { prismaToWeb } from "~/server/utils/prismaToWeb";
type orderSummary = Omit<Order, "clientId"> & {
client: Client;
value: number;
imported_products_count: number;
work_count: number;
};
export async function getOrders(
pageParameters: pageData,
where?: Prisma.OrderWhereInput,
) {
const data = await database.order.findPaginated(
pageParameters,
{
select: {
id: true,
client: true,
userId: true,
draft: true,
imported_products: {
select: {
price: true,
},
},
work: {
select: {
price: true,
},
},
},
where,
},
);
const rvalue = new Array<orderSummary>();
for (const i of data) {
const importedProductsPriceSum = i.imported_products.reduce((pv, cv) => pv + cv.price.toNumber(), 0);
const workPriceSum = i.work.reduce((pv, cv) => pv + cv.price.toNumber(), 0);
rvalue.push({
id: i.id,
client: i.client,
draft: i.draft,
imported_products_count: i.imported_products.length,
userId: i.userId,
value: importedProductsPriceSum + workPriceSum,
work_count: i.work.length,
});
}
return rvalue;
}
export default defineEventHandler((e) => {
const pageParameters = getPaginatedParameters(e, 50, 200);
return getOrders(pageParameters, {}).then(prismaToWeb);
});