From 0bcb69b09f8d40304ef8eed256107e689155e36d Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 23 Jul 2024 11:24:07 +0700 Subject: [PATCH] feat: add quotation endpoint (complete) --- src/controllers/quotation-controller.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/controllers/quotation-controller.ts b/src/controllers/quotation-controller.ts index d988a27..68d5dbb 100644 --- a/src/controllers/quotation-controller.ts +++ b/src/controllers/quotation-controller.ts @@ -259,6 +259,13 @@ export class QuotationController extends Controller { "relationProductNotFound", ); } + if (existingEmployee.length !== employee.length) { + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Some worker(employee) cannot be found.", + "relationWorkerNotFound", + ); + } if (!customer) throw new HttpError( HttpStatus.BAD_REQUEST, @@ -280,7 +287,7 @@ export class QuotationController extends Controller { const { service: _service, worker: _worker, ...rest } = body; - await prisma.$transaction(async (tx) => { + return await prisma.$transaction(async (tx) => { const nonExistEmployee = body.worker.filter((v) => typeof v !== "string"); const lastEmployee = await tx.runningNo.upsert({ where: { @@ -351,7 +358,6 @@ export class QuotationController extends Controller { 100, ) / 100; - console.log(e.vat); return { ...e, vat: e.vat === undefined ? 0.07 : e.vat, @@ -404,9 +410,8 @@ export class QuotationController extends Controller { await Promise.all( restructureService.map(async (a) => { - await tx.quotationService.create({ + const { id: _currentServiceId } = await tx.quotationService.create({ data: { - id: a.id, code: a.code, name: a.name, detail: a.detail, @@ -419,11 +424,10 @@ export class QuotationController extends Controller { a.work.map(async (b) => { await tx.quotationServiceWork.create({ data: { - id: b.id, order: b.order, name: b.name, attributes: b.attributes, - serviceId: a.id, + serviceId: _currentServiceId, productOnWork: { createMany: { data: b.product.map((v, i) => ({ @@ -443,7 +447,7 @@ export class QuotationController extends Controller { }), ); - const result = await tx.quotation.findUnique({ + return await tx.quotation.findUnique({ include: { service: { include: { @@ -467,12 +471,6 @@ export class QuotationController extends Controller { }, where: { id: quotation.id }, }); - - console.log(JSON.stringify(result, null, 2)); - - // console.log("Re-Structure:", JSON.stringify(restructureService, null, 2)); - // console.log("Price:", JSON.stringify(price, null, 2)); - throw new Error(""); }); }