From e4064a0695f322a96f2eacb0df77c0d36016aa15 Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Wed, 25 Sep 2024 16:34:43 +0700 Subject: [PATCH] fix: delete customer branch hq now also delete parent --- .../03-customer-branch-controller.ts | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/controllers/03-customer-branch-controller.ts b/src/controllers/03-customer-branch-controller.ts index 8cd5f61..4f6bb60 100644 --- a/src/controllers/03-customer-branch-controller.ts +++ b/src/controllers/03-customer-branch-controller.ts @@ -493,12 +493,33 @@ export class CustomerBranchController extends Controller { ); } - return await prisma.customerBranch - .delete({ - include: { createdBy: true, updatedBy: true }, - where: { id: branchId }, - }) - .then((v) => deleteFolder(fileLocation.customerBranch.attachment(branchId)).then(() => v)); + return await prisma.$transaction(async (tx) => { + if (record.code.endsWith("00")) { + await Promise.all([ + tx.customer.delete({ + where: { id: record.customerId }, + }), + tx.runningNo.delete({ + where: { key: record.code.slice(0, -3) }, + }), + ]); + } + return await prisma.customerBranch + .delete({ + include: { createdBy: true, updatedBy: true }, + where: { id: branchId }, + }) + .then((v) => + Promise.all([ + deleteFolder(fileLocation.customerBranch.attachment(branchId)), + deleteFolder(fileLocation.customerBranch.citizen(branchId)), + deleteFolder(fileLocation.customerBranch.powerOfAttorney(branchId)), + deleteFolder(fileLocation.customerBranch.vatRegistration(branchId)), + deleteFolder(fileLocation.customerBranch.houseRegistration(branchId)), + deleteFolder(fileLocation.customerBranch.commercialRegistration(branchId)), + ]).then(() => v), + ); + }); } }