diff --git a/src/controllers/04-product-controller.ts b/src/controllers/04-product-controller.ts index 6ff0ce4..db2f3ac 100644 --- a/src/controllers/04-product-controller.ts +++ b/src/controllers/04-product-controller.ts @@ -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", ); }