diff --git a/src/controllers/01-branch-controller.ts b/src/controllers/01-branch-controller.ts index adf928d..da0690f 100644 --- a/src/controllers/01-branch-controller.ts +++ b/src/controllers/01-branch-controller.ts @@ -26,7 +26,7 @@ import { } from "../services/permission"; import { filterStatus } from "../services/prisma"; import { connectOrDisconnect, connectOrNot } from "../utils/relation"; -import { throwNotFound } from "../utils/error"; +import { notFoundError } from "../utils/error"; if (!process.env.MINIO_BUCKET) { throw Error("Require MinIO bucket."); @@ -298,7 +298,7 @@ export class BranchController extends Controller { where: { id: branchId }, }); - if (!record) return throwNotFound("Branch"); + if (!record) throw notFoundError("Branch"); return record; } @@ -540,7 +540,7 @@ export class BranchController extends Controller { where: { id: branchId }, }); - if (!record) return throwNotFound("Branch"); + if (!record) throw notFoundError("Branch"); await permissionCheck(req.user, record); @@ -589,7 +589,7 @@ export class BranchFileController extends Controller { include: branchRelationPermInclude(user), where: { id }, }); - if (!data) return throwNotFound("Branch"); + if (!data) throw notFoundError("Branch"); await permissionCheck(user, data); } diff --git a/src/controllers/03-employee-controller.ts b/src/controllers/03-employee-controller.ts index 9a57772..e70a98c 100644 --- a/src/controllers/03-employee-controller.ts +++ b/src/controllers/03-employee-controller.ts @@ -25,7 +25,7 @@ import { createPermCondition, } from "../services/permission"; import { connectOrDisconnect, connectOrNot } from "../utils/relation"; -import { throwNotFound, throwRelationError } from "../utils/error"; +import { notFoundError, relationError } from "../utils/error"; import { deleteFile, fileLocation, getFile, listFile, setFile } from "../utils/minio"; if (!process.env.MINIO_BUCKET) { @@ -357,7 +357,7 @@ export class EmployeeController extends Controller { where: { id: employeeId }, }); - if (!record) return throwNotFound("Employee"); + if (!record) throw notFoundError("Employee"); return record; } @@ -382,10 +382,10 @@ export class EmployeeController extends Controller { }, }), ]); - 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"); + if (body.provinceId !== province?.id) throw relationError("Province"); + if (body.districtId !== district?.id) throw relationError("District"); + if (body.subDistrictId !== subDistrict?.id) throw relationError("Sub-district"); + if (!customerBranch) throw relationError("Customer Branch"); await permissionCheck(req.user, customerBranch.customer.registeredBranch); @@ -541,10 +541,10 @@ export class EmployeeController extends Controller { }), ]); - 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 (body.provinceId && !province) throw relationError("Province"); + if (body.districtId && !district) throw relationError("District"); + if (body.subDistrictId && !subDistrict) throw relationError("SubDistrict"); + if (body.customerBranchId && !customerBranch) throw relationError("Customer"); if (!employee) { throw new HttpError(HttpStatus.NOT_FOUND, "Employee cannot be found.", "employeeNotFound"); } @@ -735,7 +735,7 @@ export class EmployeeController extends Controller { }, }); - if (!record) return throwNotFound("Employee"); + if (!record) throw notFoundError("Employee"); await permissionCheck(req.user, record.customerBranch.customer.registeredBranch); @@ -783,7 +783,7 @@ export class EmployeeFileController extends Controller { }, }, }); - if (!data) return throwNotFound("Employee"); + if (!data) throw notFoundError("Employee"); await permissionCheck(user, data.customerBranch.customer.registeredBranch); } diff --git a/src/controllers/04-service-controller.ts b/src/controllers/04-service-controller.ts index 4b0d68a..d284e7c 100644 --- a/src/controllers/04-service-controller.ts +++ b/src/controllers/04-service-controller.ts @@ -25,7 +25,7 @@ import { createPermCondition, } from "../services/permission"; import { filterStatus } from "../services/prisma"; -import { throwRelationError } from "../utils/error"; +import { relationError } from "../utils/error"; import { deleteFile, fileLocation, getFile, listFile, setFile } from "../utils/minio"; const MANAGE_ROLES = [ @@ -253,7 +253,7 @@ export class ServiceController extends Controller { }), ]); - if (!productGroup) return throwRelationError("Product Type"); + if (!productGroup) throw relationError("Product Type"); await permissionCheck(req.user, productGroup.registeredBranch); diff --git a/src/utils/error.ts b/src/utils/error.ts index ec62f5b..614fff3 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -1,16 +1,16 @@ import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; -export function throwRelationError(name: string) { - throw new HttpError( +export function relationError(name: string) { + return new HttpError( HttpStatus.BAD_REQUEST, `${name} cannot be found.`, `relation${name.replaceAll(" ", "")}NotFound`, ); } -export function throwNotFound(name: string) { - throw new HttpError( +export function notFoundError(name: string) { + return new HttpError( HttpStatus.BAD_REQUEST, `${name} cannot be found.`, `${name.charAt(0).toLowerCase() + name.replaceAll(" ", "").slice(1)}NotFound`,