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 & { 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(); 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); });