diff --git a/src/controllers/branch-controller.ts b/src/controllers/branch-controller.ts index e8fb801..84f29d1 100644 --- a/src/controllers/branch-controller.ts +++ b/src/controllers/branch-controller.ts @@ -561,22 +561,34 @@ export class BranchController extends Controller { throw new HttpError(HttpStatus.FORBIDDEN, "Branch is in used.", "branchInUsed"); } - await minio.removeObject(MINIO_BUCKET, lineImageLoc(branchId), { - forceDelete: true, - }); - await minio.removeObject(MINIO_BUCKET, branchImageLoc(branchId), { - forceDelete: true, - }); + return await prisma.$transaction(async (tx) => { + const data = await tx.branch.delete({ + include: { + province: true, + district: true, + subDistrict: true, + createdBy: true, + updatedBy: true, + }, + where: { id: branchId }, + }); - return await prisma.branch.delete({ - include: { - province: true, - district: true, - subDistrict: true, - createdBy: true, - updatedBy: true, - }, - where: { id: branchId }, + if (record.isHeadOffice) { + await tx.runningNo.delete({ + where: { + key: `MAIN_BRANCH_${record.code.slice(0, -6)}`, + }, + }); + } + + await minio.removeObject(MINIO_BUCKET, lineImageLoc(branchId), { + forceDelete: true, + }); + await minio.removeObject(MINIO_BUCKET, branchImageLoc(branchId), { + forceDelete: true, + }); + + return data; }); }