feat: check code constraints
This commit is contained in:
parent
f7e4d41549
commit
0ffa7e410a
1 changed files with 20 additions and 0 deletions
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue