From 19f4a58df44b4e16ab64a983a82a1c96fe6a70ca Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Wed, 9 Oct 2024 10:22:59 +0700 Subject: [PATCH] feat: handle running no on delete customer --- src/controllers/03-customer-controller.ts | 30 ++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/controllers/03-customer-controller.ts b/src/controllers/03-customer-controller.ts index e5ed73a..87e708a 100644 --- a/src/controllers/03-customer-controller.ts +++ b/src/controllers/03-customer-controller.ts @@ -418,9 +418,33 @@ export class CustomerController extends Controller { if (record.status !== Status.CREATED) throw isUsedError("Customer"); - return await prisma.customer - .delete({ where: { id: customerId } }) - .then((data) => deleteFolder(`customer/${customerId}`).then(() => data)); + await prisma.$transaction(async (tx) => { + await deleteFolder(`customer/${customerId}`); + const data = await tx.customer.delete({ + include: { + branch: true, + registeredBranch: { + include: { + headOffice: true, + }, + }, + }, + where: { id: customerId }, + }); + + await tx.runningNo.deleteMany({ + where: { + key: { + in: data.branch.map( + (v) => + `CUSTOMER_BRANCH_${(data.registeredBranch.headOffice || data.registeredBranch).code}_${v.code.slice(0, -3)}`, + ), + }, + }, + }); + + return await tx.customer.delete({ where: { id: customerId } }); + }); } }