feat: detect same name of product in the same company

This commit is contained in:
Methapon Metanipat 2024-09-27 13:35:09 +07:00
parent 56c2f1d5ed
commit 9889317b08

View file

@ -42,7 +42,7 @@ function globalAllow(user: RequestWithUser["user"]) {
return allowList.some((v) => user.roles?.includes(v));
}
const permissionCondShared = createPermCondition((_) => true);
const permissionCondCompany = createPermCondition((_) => true);
const permissionCond = createPermCondition(globalAllow);
const permissionCheck = createPermCheck(globalAllow);
@ -113,7 +113,7 @@ export class ProductController extends Controller {
{
shared: true,
productGroup: {
registeredBranch: { OR: permissionCondShared(req.user) },
registeredBranch: { OR: permissionCondCompany(req.user) },
},
},
],
@ -147,7 +147,7 @@ export class ProductController extends Controller {
{
shared: true,
productGroup: {
registeredBranch: { OR: permissionCondShared(req.user) },
registeredBranch: { OR: permissionCondCompany(req.user) },
},
},
],
@ -195,7 +195,7 @@ export class ProductController extends Controller {
@Post()
@Security("keycloak", MANAGE_ROLES)
async createProduct(@Request() req: RequestWithUser, @Body() body: ProductCreate) {
const [productGroup] = await prisma.$transaction([
const [productGroup, productSameName] = await prisma.$transaction([
prisma.productGroup.findFirst({
include: {
registeredBranch: {
@ -206,13 +206,26 @@ export class ProductController extends Controller {
},
where: { id: body.productGroupId },
}),
prisma.product.findMany({
where: {
productGroup: {
registeredBranch: {
OR: permissionCondCompany(req.user),
},
},
name: body.name,
},
}),
]);
if (!productGroup) {
if (!productGroup) throw relationError("Product Group");
console.log(productSameName);
if (productSameName.some((v) => v.code.slice(0, -3) === body.code.toUpperCase())) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Product Group cannot be found.",
"relationProductGroupNotFound",
"Product with the same name and code already exists",
"productNameExists",
);
}