From 20725b0357b30bf27f6ef3b934d6357b7a7a0383 Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Thu, 12 Sep 2024 14:31:21 +0700 Subject: [PATCH] refactor: reduce code --- src/controllers/01-branch-controller.ts | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) 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: {