fix: permission condition

This commit is contained in:
Methapon Metanipat 2024-09-25 14:26:21 +07:00
parent 4036a36236
commit ea72b1a1bb

View file

@ -12,6 +12,7 @@ export function branchRelationPermInclude(user: RequestWithUser["user"]) {
user: { where: { userId: user.sub } },
},
},
branch: { where: { user: { some: { userId: user.sub } } } },
user: { where: { userId: user.sub } },
};
}
@ -43,15 +44,7 @@ export function createPermCondition(globalAllow: (user: RequestWithUser["user"])
export async function getBranchPermissionCheck(user: RequestWithUser["user"], branchId: string) {
return await prisma.branch.findUnique({
include: {
headOffice: {
include: {
branch: { where: { user: { some: { userId: user.sub } } } },
user: { where: { userId: user.sub } },
},
},
user: { where: { userId: user.sub } },
},
include: branchRelationPermInclude(user),
where: { id: branchId },
});
}
@ -78,7 +71,7 @@ export function createPermCheck(globalAllow: (user: RequestWithUser["user"]) =>
);
} else {
if (
(branch.user.length === 0 && !branch.headOffice) ||
(branch.user.length === 0 && branch.branch.length === 0 && !branch.headOffice) ||
(branch.headOffice &&
branch.headOffice.user.length === 0 &&
branch.headOffice.branch.length === 0)