fix: delete unused branch cause error new branch same code

This commit is contained in:
Methapon2001 2024-08-13 20:26:32 +07:00
parent 4be62e6bd2
commit 20cf11f988

View file

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