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));
}
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<Prisma.ServiceWhereInput[]>(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;