WorkshopTasker/server/api/orders.get.ts

66 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

import { defineEventHandler } from "h3";
import { type Order, type Client, Prisma } from "@prisma/client";
2023-05-24 09:40:45 +02:00
import getPaginatedParameters, { type pageData } from "../utils/baaPageParsing";
import { database } from "../utils/database";
import { prismaToWeb } from "~/server/utils/prismaToWeb";
2023-05-24 09:40:45 +02:00
type orderSummary = Omit<Order, "clientId"> & {
client: Client;
value: number;
imported_products_count: number;
work_count: number;
};
2023-05-24 09:40:45 +02:00
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,
},
);
2023-05-24 09:40:45 +02:00
const rvalue = new Array<orderSummary>();
2023-05-24 09:40:45 +02:00
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);
2023-05-24 09:40:45 +02:00
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,
});
}
2023-05-24 09:40:45 +02:00
return rvalue;
}
export default defineEventHandler((e) => {
const pageParameters = getPaginatedParameters(e, 50, 200);
return getOrders(pageParameters, {}).then(prismaToWeb);
2023-05-24 09:40:45 +02:00
});