2023-11-06 02:57:00 +01:00
|
|
|
import { defineEventHandler } from "h3";
|
2023-11-08 05:35:48 +01:00
|
|
|
import { type Order, type Client, Prisma } from "@prisma/client";
|
2023-05-24 09:40:45 +02:00
|
|
|
|
2023-11-08 05:35:48 +01: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
|
|
|
|
2023-11-08 05:35:48 +01: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
|
|
|
|
2023-11-08 05:35:48 +01: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
|
|
|
|
2023-11-08 05:35:48 +01:00
|
|
|
const rvalue = new Array<orderSummary>();
|
2023-05-24 09:40:45 +02:00
|
|
|
|
2023-11-08 05:35:48 +01: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
|
|
|
|
2023-11-08 05:35:48 +01: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;
|
2023-11-08 05:35:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defineEventHandler((e) => {
|
|
|
|
const pageParameters = getPaginatedParameters(e, 50, 200);
|
|
|
|
return getOrders(pageParameters, {}).then(prismaToWeb);
|
2023-05-24 09:40:45 +02:00
|
|
|
});
|