From 9889317b08d889f5140d4bd5b2d4429862f89b75 Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Fri, 27 Sep 2024 13:35:09 +0700 Subject: [PATCH] feat: detect same name of product in the same company --- src/controllers/04-product-controller.ts | 27 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 7 deletions(-) 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", ); }