From ad551bdf101f8b2f37f3c6420c90e862c7485a8b Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Fri, 20 Dec 2024 17:24:26 +0700 Subject: [PATCH] refactor: update shared cond to the same as product Currently this is not used anywhere in the project. --- src/controllers/04-service-controller.ts | 71 +++++++++++++++--------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/src/controllers/04-service-controller.ts b/src/controllers/04-service-controller.ts index 6c0cf4d..624073b 100644 --- a/src/controllers/04-service-controller.ts +++ b/src/controllers/04-service-controller.ts @@ -44,7 +44,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); @@ -136,7 +136,7 @@ export class ServiceController extends Controller { { shared: true, productGroup: { - registeredBranch: { OR: permissionCondShared(req.user) }, + registeredBranch: { OR: permissionCondCompany(req.user) }, }, }, ], @@ -155,7 +155,20 @@ export class ServiceController extends Controller { @Query() productGroupId?: string, @Query() fullDetail?: boolean, @Query() activeOnly?: boolean, + @Query() shared?: boolean, ) { + // NOTE: will be used to scope product within product group that is shared between branch but not company when select shared product if user is system + const targetGroup = + productGroupId && req.user.roles.includes("system") + ? await prisma.productGroup.findFirst({ + where: { id: productGroupId }, + }) + : undefined; + + if (targetGroup !== undefined && !targetGroup) throw notFoundError("Product Group"); + + const targetBranchId = targetGroup?.registeredBranchId; + const where = { OR: queryOrNot(query, [ { name: { contains: query } }, @@ -164,33 +177,39 @@ export class ServiceController extends Controller { ]), AND: { ...filterStatus(activeOnly ? Status.ACTIVE : status), - productGroupId, productGroup: { status: activeOnly ? { not: Status.INACTIVE } : undefined, - registeredBranch: activeOnly - ? { - OR: [ - { headOffice: { status: { not: Status.INACTIVE } } }, - { headOffice: null, status: { not: Status.INACTIVE } }, - ], - } - : undefined, + registeredBranch: { OR: permissionCondCompany(req.user, { activeOnly, targetBranchId }) }, }, - OR: isSystem(req.user) - ? undefined - : [ - { - productGroup: { - registeredBranch: { OR: permissionCond(req.user, { activeOnly }) }, - }, - }, - { - shared: true, - productGroup: { - registeredBranch: { OR: permissionCondShared(req.user, { activeOnly }) }, - }, - }, - ], + OR: [ + ...(productGroupId + ? [ + shared + ? { + OR: [ + { productGroupId }, + { + shared: true, + productGroup: { + registeredBranch: { + OR: permissionCondCompany(req.user, { activeOnly, targetBranchId }), + }, + }, + }, + { + productGroup: { + shared: true, + registeredBranch: { + OR: permissionCondCompany(req.user, { activeOnly, targetBranchId }), + }, + }, + }, + ], + } + : { productGroupId }, + ] + : []), + ], }, } satisfies Prisma.ServiceWhereInput;