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