refactor: use helper

This commit is contained in:
Methapon Metanipat 2024-09-11 14:40:28 +07:00
parent 29fd02cfee
commit 35adc4b43a

View file

@ -26,6 +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";
const MANAGE_ROLES = [
"system",
@ -186,9 +187,7 @@ export class ProductController extends Controller {
where: { id: productId },
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Product cannot be found.", "productNotFound");
}
if (!record) throw notFoundError("Product");
return record;
}
@ -300,17 +299,8 @@ export class ProductController extends Controller {
}),
]);
if (!product) {
throw new HttpError(HttpStatus.NOT_FOUND, "Product cannot be found.", "productNotFound");
}
if (!!body.productGroupId && !productGroup) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Product Group cannot be found.",
"relationProductGroupNotFound",
);
}
if (!product) throw notFoundError("Product");
if (!!body.productGroupId && !productGroup) throw relationError("Product Group");
await permissionCheck(req.user, product.productGroup.registeredBranch);
if (body.productGroupId && productGroup) {
@ -353,9 +343,7 @@ export class ProductController extends Controller {
where: { id: productId },
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Product cannot be found.", "productNotFound");
}
if (!record) throw notFoundError("Product");
if (record.status !== Status.CREATED) {
throw new HttpError(HttpStatus.FORBIDDEN, "Product is in used.", "productInUsed");
@ -387,9 +375,7 @@ export class ProductFileController extends Controller {
},
where: { id },
});
if (!data) {
throw new HttpError(HttpStatus.NOT_FOUND, "Product cannot be found.", "productNotFound");
}
if (!data) throw notFoundError("Product");
await permissionCheck(user, data.productGroup.registeredBranch);
}