refactor: helper function manual throw

This change is to prevent swagger to infer empty type from return type
This commit is contained in:
Methapon Metanipat 2024-09-11 14:38:26 +07:00
parent 5b9472b466
commit 4da867cde1
4 changed files with 22 additions and 22 deletions

View file

@ -26,7 +26,7 @@ import {
} from "../services/permission"; } from "../services/permission";
import { filterStatus } from "../services/prisma"; import { filterStatus } from "../services/prisma";
import { connectOrDisconnect, connectOrNot } from "../utils/relation"; import { connectOrDisconnect, connectOrNot } from "../utils/relation";
import { throwNotFound } from "../utils/error"; import { notFoundError } from "../utils/error";
if (!process.env.MINIO_BUCKET) { if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket."); throw Error("Require MinIO bucket.");
@ -298,7 +298,7 @@ export class BranchController extends Controller {
where: { id: branchId }, where: { id: branchId },
}); });
if (!record) return throwNotFound("Branch"); if (!record) throw notFoundError("Branch");
return record; return record;
} }
@ -540,7 +540,7 @@ export class BranchController extends Controller {
where: { id: branchId }, where: { id: branchId },
}); });
if (!record) return throwNotFound("Branch"); if (!record) throw notFoundError("Branch");
await permissionCheck(req.user, record); await permissionCheck(req.user, record);
@ -589,7 +589,7 @@ export class BranchFileController extends Controller {
include: branchRelationPermInclude(user), include: branchRelationPermInclude(user),
where: { id }, where: { id },
}); });
if (!data) return throwNotFound("Branch"); if (!data) throw notFoundError("Branch");
await permissionCheck(user, data); await permissionCheck(user, data);
} }

View file

@ -25,7 +25,7 @@ import {
createPermCondition, createPermCondition,
} from "../services/permission"; } from "../services/permission";
import { connectOrDisconnect, connectOrNot } from "../utils/relation"; 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"; import { deleteFile, fileLocation, getFile, listFile, setFile } from "../utils/minio";
if (!process.env.MINIO_BUCKET) { if (!process.env.MINIO_BUCKET) {
@ -357,7 +357,7 @@ export class EmployeeController extends Controller {
where: { id: employeeId }, where: { id: employeeId },
}); });
if (!record) return throwNotFound("Employee"); if (!record) throw notFoundError("Employee");
return record; return record;
} }
@ -382,10 +382,10 @@ export class EmployeeController extends Controller {
}, },
}), }),
]); ]);
if (body.provinceId !== province?.id) return throwRelationError("Province"); if (body.provinceId !== province?.id) throw relationError("Province");
if (body.districtId !== district?.id) return throwRelationError("District"); if (body.districtId !== district?.id) throw relationError("District");
if (body.subDistrictId !== subDistrict?.id) return throwRelationError("Sub-district"); if (body.subDistrictId !== subDistrict?.id) throw relationError("Sub-district");
if (!customerBranch) return throwRelationError("Customer Branch"); if (!customerBranch) throw relationError("Customer Branch");
await permissionCheck(req.user, customerBranch.customer.registeredBranch); 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.provinceId && !province) throw relationError("Province");
if (body.districtId && !district) return throwRelationError("District"); if (body.districtId && !district) throw relationError("District");
if (body.subDistrictId && !subDistrict) return throwRelationError("SubDistrict"); if (body.subDistrictId && !subDistrict) throw relationError("SubDistrict");
if (body.customerBranchId && !customerBranch) return throwRelationError("Customer"); if (body.customerBranchId && !customerBranch) throw relationError("Customer");
if (!employee) { if (!employee) {
throw new HttpError(HttpStatus.NOT_FOUND, "Employee cannot be found.", "employeeNotFound"); 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); 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); await permissionCheck(user, data.customerBranch.customer.registeredBranch);
} }

View file

@ -25,7 +25,7 @@ import {
createPermCondition, createPermCondition,
} from "../services/permission"; } from "../services/permission";
import { filterStatus } from "../services/prisma"; import { filterStatus } from "../services/prisma";
import { throwRelationError } from "../utils/error"; import { relationError } from "../utils/error";
import { deleteFile, fileLocation, getFile, listFile, setFile } from "../utils/minio"; import { deleteFile, fileLocation, getFile, listFile, setFile } from "../utils/minio";
const MANAGE_ROLES = [ 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); await permissionCheck(req.user, productGroup.registeredBranch);

View file

@ -1,16 +1,16 @@
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status"; import HttpStatus from "../interfaces/http-status";
export function throwRelationError(name: string) { export function relationError(name: string) {
throw new HttpError( return new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
`${name} cannot be found.`, `${name} cannot be found.`,
`relation${name.replaceAll(" ", "")}NotFound`, `relation${name.replaceAll(" ", "")}NotFound`,
); );
} }
export function throwNotFound(name: string) { export function notFoundError(name: string) {
throw new HttpError( return new HttpError(
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
`${name} cannot be found.`, `${name} cannot be found.`,
`${name.charAt(0).toLowerCase() + name.replaceAll(" ", "").slice(1)}NotFound`, `${name.charAt(0).toLowerCase() + name.replaceAll(" ", "").slice(1)}NotFound`,