diff --git a/src/controllers/01-branch-controller.ts b/src/controllers/01-branch-controller.ts index d8d2fcb..251a5b3 100644 --- a/src/controllers/01-branch-controller.ts +++ b/src/controllers/01-branch-controller.ts @@ -378,25 +378,19 @@ export class BranchController extends Controller { update: { value: { increment: 1 } }, }); + const errorBranchExists = new HttpError( + HttpStatus.BAD_REQUEST, + "Branch with same code already exists.", + "sameBranchCodeExists", + ); + if (last.value === 1) { const exist = await tx.branch.findFirst({ where: { code: `${code?.toLocaleUpperCase()}${`${last.value - 1}`.padStart(5, "0")}` }, }); - if (exist) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Branch with same code already exists.", - "sameBranchCodeExists", - ); - } - - if (last.value !== 1 && !headOfficeId) { - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Branch with same code already exists.", - "sameBranchCodeExists", - ); + if (exist) throw errorBranchExists; } + if (last.value !== 1 && !headOfficeId) throw errorBranchExists; return await tx.branch.create({ include: {