refactor: add helper function

This commit is contained in:
Methapon Metanipat 2024-09-10 13:16:19 +07:00
parent 114f008934
commit f79650a9bd

View file

@ -16,6 +16,28 @@ export function branchRelationPermInclude(user: RequestWithUser["user"]) {
};
}
export function createPermCondition(globalAllow: (user: RequestWithUser["user"]) => boolean) {
return (user: RequestWithUser["user"]) =>
isSystem(user)
? undefined
: [
{
user: { some: { userId: user.sub } },
},
{
branch: { some: { user: { some: { userId: user.sub } } } },
},
{
headOffice: globalAllow(user)
? { branch: { some: { user: { some: { userId: user.sub } } } } }
: undefined,
},
{
headOffice: globalAllow(user) ? { user: { some: { userId: user.sub } } } : undefined,
},
];
}
export async function getBranchPermissionCheck(user: RequestWithUser["user"], branchId: string) {
return await prisma.branch.findUnique({
include: {