diff --git a/src/controllers/branch-controller.ts b/src/controllers/branch-controller.ts index 5e65729..c5384e3 100644 --- a/src/controllers/branch-controller.ts +++ b/src/controllers/branch-controller.ts @@ -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,