refactor: throw common error with util function instead

This commit is contained in:
Methapon Metanipat 2024-10-03 09:41:47 +07:00
parent 68f9dc203e
commit 4de0e1da87
12 changed files with 67 additions and 142 deletions

View file

@ -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);
}