feat: check code constraints

This commit is contained in:
Methapon2001 2024-08-08 10:18:04 +07:00
parent f7e4d41549
commit 0ffa7e410a

View file

@ -297,6 +297,14 @@ export class BranchController extends Controller {
const { provinceId, districtId, subDistrictId, headOfficeId, bank, contact, code, ...rest } =
body;
if (head && head.code.slice(0, -6) !== code) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Headoffice code not match with branch code",
"codeMismatch",
);
}
const record = await prisma.$transaction(
async (tx) => {
const last = await tx.runningNo.upsert({
@ -310,6 +318,18 @@ export class BranchController extends Controller {
update: { value: { increment: 1 } },
});
if (last.value === 1) {
const exist = await tx.branch.findFirst({
where: { code: `${code?.toLocaleUpperCase()}${`${last.value - 1}`.padStart(6, "0")}` },
});
if (exist)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Branch with same code already exists.",
"sameBranchCodeExists",
);
}
return await tx.branch.create({
include: {
province: true,