From 42d5e3e26d04e93e105783c859e4915ec05acfb2 Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Tue, 10 Sep 2024 16:42:11 +0700 Subject: [PATCH] refactor: use relation helper function --- src/controllers/03-employee-controller.ts | 58 ++++------------------- 1 file changed, 10 insertions(+), 48 deletions(-) diff --git a/src/controllers/03-employee-controller.ts b/src/controllers/03-employee-controller.ts index 876e8ea..3c2d81e 100644 --- a/src/controllers/03-employee-controller.ts +++ b/src/controllers/03-employee-controller.ts @@ -26,6 +26,7 @@ import { createPermCondition, } from "../services/permission"; import { connectOrDisconnect, connectOrNot } from "../utils/relation"; +import { throwRelationError } from "../utils/error"; if (!process.env.MINIO_BUCKET) { throw Error("Require MinIO bucket."); @@ -396,30 +397,10 @@ export class EmployeeController extends Controller { }, }), ]); - if (body.provinceId !== province?.id) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Province cannot be found.", - "relationProvinceNotFound", - ); - if (body.districtId !== district?.id) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "District cannot be found.", - "relationDistrictNotFound", - ); - if (body.subDistrictId !== subDistrict?.id) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Sub-district cannot be found.", - "relationSubDistrictNotFound", - ); - if (!customerBranch) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Customer Branch cannot be found.", - "relationCustomerBranchNotFound", - ); + if (body.provinceId !== province?.id) return throwRelationError("Province"); + if (body.districtId !== district?.id) return throwRelationError("District"); + if (body.subDistrictId !== subDistrict?.id) return throwRelationError("Sub-district"); + if (!customerBranch) return throwRelationError("Customer Branch"); await permissionCheck(req.user, customerBranch.customer.registeredBranch); @@ -574,30 +555,11 @@ export class EmployeeController extends Controller { }, }), ]); - if (body.provinceId && !province) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Province cannot be found.", - "relationProvinceNotFound", - ); - if (body.districtId && !district) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "District cannot be found.", - "relationDistrictNotFound", - ); - if (body.subDistrictId && !subDistrict) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Sub-district cannot be found.", - "relationSubDistrictNotFound", - ); - if (body.customerBranchId && !customerBranch) - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Customer cannot be found.", - "relationCustomerNotFound", - ); + + if (body.provinceId && !province) return throwRelationError("Province"); + if (body.districtId && !district) return throwRelationError("District"); + if (body.subDistrictId && !subDistrict) return throwRelationError("SubDistrict"); + if (body.customerBranchId && !customerBranch) return throwRelationError("Customer"); if (!employee) { throw new HttpError(HttpStatus.NOT_FOUND, "Employee cannot be found.", "employeeNotFound"); }