fix: delete customer branch hq now also delete parent

This commit is contained in:
Methapon Metanipat 2024-09-25 16:34:43 +07:00
parent 0d41f60829
commit e4064a0695

View file

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