refactor: update shared cond to the same as product

Currently this is not used anywhere in the project.
This commit is contained in:
Methapon2001 2024-12-20 17:24:26 +07:00
parent 6625326f46
commit ad551bdf10

View file

@ -44,7 +44,7 @@ function globalAllow(user: RequestWithUser["user"]) {
return allowList.some((v) => user.roles?.includes(v)); return allowList.some((v) => user.roles?.includes(v));
} }
const permissionCondShared = createPermCondition((_) => true); const permissionCondCompany = createPermCondition((_) => true);
const permissionCond = createPermCondition(globalAllow); const permissionCond = createPermCondition(globalAllow);
const permissionCheck = createPermCheck(globalAllow); const permissionCheck = createPermCheck(globalAllow);
@ -136,7 +136,7 @@ export class ServiceController extends Controller {
{ {
shared: true, shared: true,
productGroup: { productGroup: {
registeredBranch: { OR: permissionCondShared(req.user) }, registeredBranch: { OR: permissionCondCompany(req.user) },
}, },
}, },
], ],
@ -155,7 +155,20 @@ export class ServiceController extends Controller {
@Query() productGroupId?: string, @Query() productGroupId?: string,
@Query() fullDetail?: boolean, @Query() fullDetail?: boolean,
@Query() activeOnly?: 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 = { const where = {
OR: queryOrNot<Prisma.ServiceWhereInput[]>(query, [ OR: queryOrNot<Prisma.ServiceWhereInput[]>(query, [
{ name: { contains: query } }, { name: { contains: query } },
@ -164,32 +177,38 @@ export class ServiceController extends Controller {
]), ]),
AND: { AND: {
...filterStatus(activeOnly ? Status.ACTIVE : status), ...filterStatus(activeOnly ? Status.ACTIVE : status),
productGroupId,
productGroup: { productGroup: {
status: activeOnly ? { not: Status.INACTIVE } : undefined, status: activeOnly ? { not: Status.INACTIVE } : undefined,
registeredBranch: activeOnly registeredBranch: { OR: permissionCondCompany(req.user, { activeOnly, targetBranchId }) },
},
OR: [
...(productGroupId
? [
shared
? { ? {
OR: [ OR: [
{ headOffice: { status: { not: Status.INACTIVE } } }, { productGroupId },
{ headOffice: null, status: { not: Status.INACTIVE } },
],
}
: undefined,
},
OR: isSystem(req.user)
? undefined
: [
{
productGroup: {
registeredBranch: { OR: permissionCond(req.user, { activeOnly }) },
},
},
{ {
shared: true, shared: true,
productGroup: { productGroup: {
registeredBranch: { OR: permissionCondShared(req.user, { activeOnly }) }, registeredBranch: {
OR: permissionCondCompany(req.user, { activeOnly, targetBranchId }),
}, },
}, },
},
{
productGroup: {
shared: true,
registeredBranch: {
OR: permissionCondCompany(req.user, { activeOnly, targetBranchId }),
},
},
},
],
}
: { productGroupId },
]
: []),
], ],
}, },
} satisfies Prisma.ServiceWhereInput; } satisfies Prisma.ServiceWhereInput;