refactor: throw common error with util function instead
This commit is contained in:
parent
68f9dc203e
commit
4de0e1da87
12 changed files with 67 additions and 142 deletions
|
|
@ -26,7 +26,7 @@ import {
|
|||
} from "../services/permission";
|
||||
import { filterStatus } from "../services/prisma";
|
||||
import { connectOrDisconnect, connectOrNot, whereAddressQuery } from "../utils/relation";
|
||||
import { notFoundError, relationError } from "../utils/error";
|
||||
import { isUsedError, notFoundError, relationError } from "../utils/error";
|
||||
|
||||
if (!process.env.MINIO_BUCKET) {
|
||||
throw Error("Require MinIO bucket.");
|
||||
|
|
@ -547,9 +547,7 @@ export class BranchController extends Controller {
|
|||
|
||||
await permissionCheck(req.user, record);
|
||||
|
||||
if (record.status !== Status.CREATED) {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, "Branch is in used.", "branchInUsed");
|
||||
}
|
||||
if (record.status !== Status.CREATED) throw isUsedError("Branch");
|
||||
|
||||
return await prisma.$transaction(async (tx) => {
|
||||
const data = await tx.branch.delete({
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ import {
|
|||
createPermCondition,
|
||||
} from "../services/permission";
|
||||
import { connectOrDisconnect, connectOrNot, whereAddressQuery } from "../utils/relation";
|
||||
import { notFoundError, relationError } from "../utils/error";
|
||||
import { isUsedError, notFoundError, relationError } from "../utils/error";
|
||||
|
||||
if (!process.env.MINIO_BUCKET) {
|
||||
throw Error("Require MinIO bucket.");
|
||||
|
|
@ -638,17 +638,13 @@ export class UserController extends Controller {
|
|||
where: { id: userId },
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "userNotFound");
|
||||
}
|
||||
if (!record) throw notFoundError("User");
|
||||
|
||||
await Promise.all([
|
||||
...record.branch.map(async ({ branch }) => await permissionCheck(req.user, branch)),
|
||||
]);
|
||||
|
||||
if (record.status !== Status.CREATED) {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, "User is in used.", "userInUsed");
|
||||
}
|
||||
if (record.status !== Status.CREATED) throw isUsedError("User");
|
||||
|
||||
await deleteFolder(fileLocation.user.profile(userId));
|
||||
await deleteFolder(fileLocation.user.attachment(userId));
|
||||
|
|
@ -686,9 +682,7 @@ async function getUserCheckPerm(user: RequestWithUser["user"], userId: string) {
|
|||
where: { id: userId },
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "userNotFound");
|
||||
}
|
||||
if (!record) throw notFoundError("User");
|
||||
|
||||
await Promise.all(record.branch.map(async ({ branch }) => await permissionCheck(user, branch)));
|
||||
}
|
||||
|
|
@ -702,9 +696,7 @@ export class UserProfileController extends Controller {
|
|||
where: { id: userId },
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "userNotFound");
|
||||
}
|
||||
if (!record) throw notFoundError("User");
|
||||
|
||||
return await listFile(fileLocation.user.profile(userId));
|
||||
}
|
||||
|
|
@ -739,9 +731,7 @@ export class UserAttachmentController extends Controller {
|
|||
where: { id: userId },
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "userNotFound");
|
||||
}
|
||||
if (!record) throw notFoundError("User");
|
||||
|
||||
const list = await listFile(fileLocation.user.attachment(userId));
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ import {
|
|||
import { RequestWithUser } from "../interfaces/user";
|
||||
import prisma from "../db";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import { isSystem } from "../utils/keycloak";
|
||||
import {
|
||||
branchRelationPermInclude,
|
||||
|
|
@ -25,7 +24,7 @@ import {
|
|||
} from "../services/permission";
|
||||
import { filterStatus } from "../services/prisma";
|
||||
import { connectOrDisconnect, connectOrNot, whereAddressQuery } from "../utils/relation";
|
||||
import { notFoundError, relationError } from "../utils/error";
|
||||
import { isUsedError, notFoundError, relationError } from "../utils/error";
|
||||
import { deleteFile, deleteFolder, fileLocation, getFile, listFile, setFile } from "../utils/minio";
|
||||
|
||||
const MANAGE_ROLES = [
|
||||
|
|
@ -415,9 +414,7 @@ export class CustomerBranchController extends Controller {
|
|||
},
|
||||
});
|
||||
|
||||
if (!branch) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound");
|
||||
}
|
||||
if (!branch) throw notFoundError("Customer Branch");
|
||||
|
||||
await permissionCheck(req.user, branch.customer.registeredBranch);
|
||||
|
||||
|
|
@ -487,13 +484,7 @@ export class CustomerBranchController extends Controller {
|
|||
|
||||
await permissionCheck(req.user, record.customer.registeredBranch);
|
||||
|
||||
if (record.status !== Status.CREATED) {
|
||||
throw new HttpError(
|
||||
HttpStatus.FORBIDDEN,
|
||||
"Customer branch is in used.",
|
||||
"customerBranchInUsed",
|
||||
);
|
||||
}
|
||||
if (record.status !== Status.CREATED) throw isUsedError("Customer Branch");
|
||||
|
||||
return await prisma.$transaction(async (tx) => {
|
||||
if (record.code.endsWith("00")) {
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ import {
|
|||
} from "../services/permission";
|
||||
import { filterStatus } from "../services/prisma";
|
||||
import { deleteFile, deleteFolder, fileLocation, getFile, listFile, setFile } from "../utils/minio";
|
||||
import { notFoundError, relationError } from "../utils/error";
|
||||
import { isUsedError, notFoundError, relationError } from "../utils/error";
|
||||
import { connectOrNot } from "../utils/relation";
|
||||
|
||||
const MANAGE_ROLES = [
|
||||
|
|
@ -361,13 +361,7 @@ export class CustomerController extends Controller {
|
|||
await permissionCheck(req.user, branch);
|
||||
}
|
||||
|
||||
if (!!body.registeredBranchId && !branch) {
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Branch cannot be found.",
|
||||
"relationBranchNotFound",
|
||||
);
|
||||
}
|
||||
if (!!body.registeredBranchId && !branch) throw relationError("Branch");
|
||||
|
||||
let companyBefore = (customer.registeredBranch.headOffice || customer.registeredBranch).code;
|
||||
let companyAfter =
|
||||
|
|
@ -418,15 +412,11 @@ export class CustomerController extends Controller {
|
|||
},
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "customerNotFound");
|
||||
}
|
||||
if (!record) throw notFoundError("Customer");
|
||||
|
||||
await permissionCheck(req.user, record.registeredBranch);
|
||||
|
||||
if (record.status !== Status.CREATED) {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, "Customer is in used.", "customerInUsed");
|
||||
}
|
||||
if (record.status !== Status.CREATED) throw isUsedError("Customer");
|
||||
|
||||
return await prisma.customer
|
||||
.delete({ where: { id: customerId } })
|
||||
|
|
|
|||
|
|
@ -155,13 +155,7 @@ export class EmployeeCheckupController extends Controller {
|
|||
async deleteById(@Path() employeeId: string, @Path() checkupId: string) {
|
||||
const record = await prisma.employeeCheckup.findFirst({ where: { id: checkupId, employeeId } });
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(
|
||||
HttpStatus.NOT_FOUND,
|
||||
"Employee checkup cannot be found.",
|
||||
"employeeCheckupNotFound",
|
||||
);
|
||||
}
|
||||
if (!record) throw notFoundError("Employee Checkup");
|
||||
|
||||
return await prisma.employeeCheckup.delete({
|
||||
include: { createdBy: true, updatedBy: true },
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ import {
|
|||
createPermCondition,
|
||||
} from "../services/permission";
|
||||
import { connectOrDisconnect, connectOrNot, whereAddressQuery } from "../utils/relation";
|
||||
import { notFoundError, relationError } from "../utils/error";
|
||||
import { isUsedError, notFoundError, relationError } from "../utils/error";
|
||||
import { deleteFile, deleteFolder, fileLocation, getFile, listFile, setFile } from "../utils/minio";
|
||||
|
||||
if (!process.env.MINIO_BUCKET) {
|
||||
|
|
@ -402,9 +402,7 @@ export class EmployeeController extends Controller {
|
|||
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");
|
||||
}
|
||||
if (!employee) throw notFoundError("Employee");
|
||||
|
||||
await permissionCheck(req.user, employee.customerBranch.customer.registeredBranch);
|
||||
if (body.customerBranchId && customerBranch) {
|
||||
|
|
@ -515,9 +513,7 @@ export class EmployeeController extends Controller {
|
|||
|
||||
await permissionCheck(req.user, record.customerBranch.customer.registeredBranch);
|
||||
|
||||
if (record.status !== Status.CREATED) {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, "Employee is in used.", "employeeInUsed");
|
||||
}
|
||||
if (record.status !== Status.CREATED) throw isUsedError("Employee");
|
||||
|
||||
await Promise.all([
|
||||
deleteFolder(fileLocation.employee.img(employeeId)),
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ import {
|
|||
import { isSystem } from "../utils/keycloak";
|
||||
import { filterStatus } from "../services/prisma";
|
||||
import { deleteFile, fileLocation, getFile, listFile, setFile } from "../utils/minio";
|
||||
import { notFoundError, relationError } from "../utils/error";
|
||||
import { isUsedError, notFoundError, relationError } from "../utils/error";
|
||||
|
||||
const MANAGE_ROLES = [
|
||||
"system",
|
||||
|
|
@ -357,9 +357,7 @@ export class ProductController extends Controller {
|
|||
|
||||
if (!record) throw notFoundError("Product");
|
||||
|
||||
if (record.status !== Status.CREATED) {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, "Product is in used.", "productInUsed");
|
||||
}
|
||||
if (record.status !== Status.CREATED) throw isUsedError("Product");
|
||||
|
||||
return await prisma.product.delete({
|
||||
include: {
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ import {
|
|||
createPermCondition,
|
||||
} from "../services/permission";
|
||||
import { filterStatus } from "../services/prisma";
|
||||
import { notFoundError } from "../utils/error";
|
||||
import { isUsedError, notFoundError, relationError } from "../utils/error";
|
||||
|
||||
type ProductGroupCreate = {
|
||||
name: string;
|
||||
|
|
@ -197,13 +197,7 @@ export class ProductGroup extends Controller {
|
|||
},
|
||||
},
|
||||
});
|
||||
if (!record) {
|
||||
throw new HttpError(
|
||||
HttpStatus.NOT_FOUND,
|
||||
"Product group cannot be found.",
|
||||
"productGroupNotFound",
|
||||
);
|
||||
}
|
||||
if (!record) throw notFoundError("Product Group");
|
||||
|
||||
if (record.registeredBranch) await permissionCheck(req.user, record.registeredBranch);
|
||||
|
||||
|
|
@ -218,13 +212,7 @@ export class ProductGroup extends Controller {
|
|||
await permissionCheck(req.user, branch);
|
||||
}
|
||||
|
||||
if (!!body.registeredBranchId && !branch) {
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Branch cannot be found.",
|
||||
"relationBranchNotFound",
|
||||
);
|
||||
}
|
||||
if (!!body.registeredBranchId && !branch) throw relationError("Branch");
|
||||
|
||||
let companyBefore = (record.registeredBranch.headOffice || record.registeredBranch).code;
|
||||
let companyAfter =
|
||||
|
|
@ -262,19 +250,11 @@ export class ProductGroup extends Controller {
|
|||
},
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(
|
||||
HttpStatus.NOT_FOUND,
|
||||
"Product group cannot be found.",
|
||||
"productGroupNotFound",
|
||||
);
|
||||
}
|
||||
if (!record) throw notFoundError("Product Group");
|
||||
|
||||
if (record.registeredBranch) await permissionCheck(req.user, record.registeredBranch);
|
||||
|
||||
if (record.status !== Status.CREATED) {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, "Product group is in used.", "productGroupInUsed");
|
||||
}
|
||||
if (record.status !== Status.CREATED) throw isUsedError("Product Group");
|
||||
|
||||
return await prisma.productGroup.delete({
|
||||
include: {
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ import {
|
|||
createPermCondition,
|
||||
} from "../services/permission";
|
||||
import { filterStatus } from "../services/prisma";
|
||||
import { relationError } from "../utils/error";
|
||||
import { isUsedError, notFoundError, relationError } from "../utils/error";
|
||||
import { deleteFile, fileLocation, getFile, listFile, setFile } from "../utils/minio";
|
||||
|
||||
const MANAGE_ROLES = [
|
||||
|
|
@ -196,9 +196,7 @@ export class ServiceController extends Controller {
|
|||
where: { id: serviceId },
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "serviceNotFound");
|
||||
}
|
||||
if (!record) throw notFoundError("Service");
|
||||
|
||||
return record;
|
||||
}
|
||||
|
|
@ -322,24 +320,24 @@ export class ServiceController extends Controller {
|
|||
@Body() body: ServiceUpdate,
|
||||
@Path() serviceId: string,
|
||||
) {
|
||||
if (!(await prisma.service.findUnique({ where: { id: serviceId } }))) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "serviceNotFound");
|
||||
}
|
||||
const { work, productGroupId, ...payload } = body;
|
||||
|
||||
const [service, productGroup] = await prisma.$transaction([
|
||||
prisma.service.findUnique({
|
||||
include: {
|
||||
productGroup: {
|
||||
include: {
|
||||
registeredBranch: {
|
||||
include: branchRelationPermInclude(req.user),
|
||||
},
|
||||
const service = await prisma.service.findUnique({
|
||||
where: { id: serviceId },
|
||||
include: {
|
||||
productGroup: {
|
||||
include: {
|
||||
registeredBranch: {
|
||||
include: branchRelationPermInclude(req.user),
|
||||
},
|
||||
},
|
||||
},
|
||||
where: { id: serviceId },
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
if (!service) throw notFoundError("Service");
|
||||
|
||||
const { work, productGroupId, ...payload } = body;
|
||||
|
||||
const [productGroup] = await prisma.$transaction([
|
||||
prisma.productGroup.findFirst({
|
||||
include: {
|
||||
registeredBranch: {
|
||||
|
|
@ -352,19 +350,12 @@ export class ServiceController extends Controller {
|
|||
}),
|
||||
]);
|
||||
|
||||
if (!service) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "serviceNotFound");
|
||||
}
|
||||
if (!service) throw notFoundError("Service");
|
||||
|
||||
if (!!body.productGroupId && !productGroup) {
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Product Type cannot be found.",
|
||||
"relationproductGroupNotFound",
|
||||
);
|
||||
}
|
||||
if (!!body.productGroupId && !productGroup) throw relationError("Product Group");
|
||||
|
||||
await permissionCheck(req.user, service.productGroup.registeredBranch);
|
||||
|
||||
if (!!body.productGroupId && productGroup) {
|
||||
await permissionCheck(req.user, productGroup.registeredBranch);
|
||||
}
|
||||
|
|
@ -417,15 +408,11 @@ export class ServiceController extends Controller {
|
|||
where: { id: serviceId },
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "serviceNotFound");
|
||||
}
|
||||
if (!record) throw notFoundError("Service");
|
||||
|
||||
await permissionCheck(req.user, record.productGroup.registeredBranch);
|
||||
|
||||
if (record.status !== Status.CREATED) {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, "Service is in used.", "serviceInUsed");
|
||||
}
|
||||
if (record.status !== Status.CREATED) throw isUsedError("Service");
|
||||
|
||||
return await prisma.service.delete({
|
||||
include: {
|
||||
|
|
@ -453,9 +440,8 @@ export class ServiceFileController extends Controller {
|
|||
},
|
||||
where: { id },
|
||||
});
|
||||
if (!data) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "serviceNotFound");
|
||||
}
|
||||
if (!data) throw notFoundError("Service");
|
||||
|
||||
await permissionCheck(user, data.productGroup.registeredBranch);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import prisma from "../db";
|
|||
import { RequestWithUser } from "../interfaces/user";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import { isUsedError, notFoundError } from "../utils/error";
|
||||
|
||||
type WorkCreate = {
|
||||
order: number;
|
||||
|
|
@ -83,7 +84,7 @@ export class WorkController extends Controller {
|
|||
where: { id: workId },
|
||||
});
|
||||
|
||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "Work cannot be found.", "workNotFound");
|
||||
if (!record) throw notFoundError("Work");
|
||||
|
||||
return record;
|
||||
}
|
||||
|
|
@ -219,11 +220,11 @@ export class WorkController extends Controller {
|
|||
@Body() body: WorkUpdate,
|
||||
@Path() workId: string,
|
||||
) {
|
||||
const work = await prisma.work.findUnique({ where: { id: workId } });
|
||||
|
||||
const { productId, ...payload } = body;
|
||||
|
||||
if (!(await prisma.work.findUnique({ where: { id: workId } }))) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Work cannot be found.", "workNotFound");
|
||||
}
|
||||
if (!work) throw notFoundError("Work");
|
||||
|
||||
const exist = await prisma.work.findFirst({
|
||||
include: {
|
||||
|
|
@ -313,13 +314,8 @@ export class WorkController extends Controller {
|
|||
where: { id: workId },
|
||||
});
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Work cannot be found.", "workNotFound");
|
||||
}
|
||||
|
||||
if (record.status !== Status.CREATED) {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, "Work is in used.", "workInUsed");
|
||||
}
|
||||
if (!record) throw notFoundError("Work");
|
||||
if (record.status !== Status.CREATED) throw isUsedError("Work");
|
||||
|
||||
return await prisma.work.delete({ where: { id: workId } });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ import {
|
|||
createPermCondition,
|
||||
} from "../services/permission";
|
||||
import { isSystem } from "../utils/keycloak";
|
||||
import { notFoundError, relationError } from "../utils/error";
|
||||
import { isUsedError, notFoundError, relationError } from "../utils/error";
|
||||
|
||||
type QuotationCreate = {
|
||||
status?: Status;
|
||||
|
|
@ -632,9 +632,7 @@ export class QuotationController extends Controller {
|
|||
|
||||
await permissionCheck(req.user, record.customerBranch.customer.registeredBranch);
|
||||
|
||||
if (record.status !== Status.CREATED) {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, "Quotation is in used.", "quotationInUsed");
|
||||
}
|
||||
if (record.status !== Status.CREATED) throw isUsedError("Quotation");
|
||||
|
||||
return await prisma.quotation.delete({
|
||||
include: {
|
||||
|
|
|
|||
|
|
@ -16,3 +16,11 @@ export function notFoundError(name: string) {
|
|||
`${name.charAt(0).toLowerCase() + name.replaceAll(" ", "").slice(1)}NotFound`,
|
||||
);
|
||||
}
|
||||
|
||||
export function isUsedError(name: string) {
|
||||
return new HttpError(
|
||||
HttpStatus.PRECONDITION_FAILED,
|
||||
`${name} cannot be found.`,
|
||||
`${name.charAt(0).toLowerCase() + name.replaceAll(" ", "").slice(1)}IsUsed`,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue