feat: implement active only condition

This commit is contained in:
Methapon2001 2024-12-06 16:01:04 +07:00
parent 57397cc894
commit 9d6fd2d8d0

View file

@ -1,4 +1,4 @@
import { Prisma } from "@prisma/client"; import { Prisma, Status } from "@prisma/client";
import prisma from "../db"; import prisma from "../db";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status"; import HttpStatus from "../interfaces/http-status";
@ -18,30 +18,46 @@ export function branchRelationPermInclude(user: RequestWithUser["user"]) {
}; };
} }
export function createPermCondition(globalAllow: (user: RequestWithUser["user"]) => boolean) { export function createPermCondition(
return ( globalAllow: (user: RequestWithUser["user"]) => boolean,
user: RequestWithUser["user"], ): (
opts?: { alwaysIncludeHead?: boolean; includeInActive?: boolean }, user: RequestWithUser["user"],
) => opts?: { alwaysIncludeHead?: boolean; activeOnly?: boolean },
) => Prisma.BranchWhereInput["OR"] {
return (user, opts) =>
isSystem(user) isSystem(user)
? undefined ? undefined
: [ : [
{ {
status: opts?.activeOnly ? { not: Status.INACTIVE } : undefined,
user: { some: { userId: user.sub } }, user: { some: { userId: user.sub } },
}, },
{ {
status: opts?.activeOnly ? { not: Status.INACTIVE } : undefined,
branch: branch:
opts?.alwaysIncludeHead || globalAllow(user) opts?.alwaysIncludeHead || globalAllow(user)
? { some: { user: { some: { userId: user.sub } } } } ? {
some: { user: { some: { userId: user.sub } } },
}
: undefined, : undefined,
}, },
{ {
headOffice: globalAllow(user) headOffice: globalAllow(user)
? { branch: { some: { user: { some: { userId: user.sub } } } } } ? {
status: opts?.activeOnly ? { not: Status.INACTIVE } : undefined,
branch: {
some: { user: { some: { userId: user.sub } } },
},
}
: undefined, : undefined,
}, },
{ {
headOffice: globalAllow(user) ? { user: { some: { userId: user.sub } } } : undefined, headOffice: globalAllow(user)
? {
status: opts?.activeOnly ? { not: Status.INACTIVE } : undefined,
user: { some: { userId: user.sub } },
}
: undefined,
}, },
]; ];
} }