feat: handle running no on delete customer

This commit is contained in:
Methapon Metanipat 2024-10-09 10:22:59 +07:00
parent 8172bd4ab6
commit 19f4a58df4

View file

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