From 6b4e13e94d9eda0fa2a9bf3ee9674aa8fbdb36db Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 19 Dec 2024 16:04:07 +0700 Subject: [PATCH] feat: check if installments is valid --- src/controllers/04-service-controller.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/controllers/04-service-controller.ts b/src/controllers/04-service-controller.ts index 0934238..6c0cf4d 100644 --- a/src/controllers/04-service-controller.ts +++ b/src/controllers/04-service-controller.ts @@ -308,6 +308,18 @@ export class ServiceController extends Controller { await permissionCheck(req.user, productGroup.registeredBranch); + if (body.installments && body.work) { + for (let i = 0; i < body.installments; i++) { + if (!body.work.some((w) => w.product.some((p) => p.installmentNo === i + 1))) { + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Installments validate failed.", + "installmentsValidateFailed", + ); + } + } + } + const record = await prisma.$transaction( async (tx) => { const branch = productGroup.registeredBranch; @@ -419,6 +431,18 @@ export class ServiceController extends Controller { await permissionCheck(req.user, productGroup.registeredBranch); } + if (body.installments && body.work) { + for (let i = 0; i < body.installments; i++) { + if (!body.work.some((w) => w.product.some((p) => p.installmentNo === i + 1))) { + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Installments validate failed.", + "installmentsValidateFailed", + ); + } + } + } + const record = await prisma.$transaction(async (tx) => { if (payload.workflowId === "") payload.workflowId = undefined;