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 } }); + }); } }