refactor: view condition and permission
This commit is contained in:
parent
9f426d9b08
commit
e61111cac7
3 changed files with 118 additions and 117 deletions
|
|
@ -27,6 +27,10 @@ if (!process.env.MINIO_BUCKET) {
|
|||
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
||||
const MANAGE_ROLES = ["system", "head_of_admin"];
|
||||
|
||||
function globalAllow(user: RequestWithUser["user"]) {
|
||||
return MANAGE_ROLES.some((v) => user.roles?.includes(v));
|
||||
}
|
||||
|
||||
type BranchCreate = {
|
||||
status?: Status;
|
||||
code: string;
|
||||
|
|
@ -191,10 +195,17 @@ export class BranchController extends Controller {
|
|||
AND: {
|
||||
zipCode,
|
||||
headOfficeId: headOfficeId ?? (filter === "head" || tree ? null : undefined),
|
||||
user: !MANAGE_ROLES.some((v) => req.user.roles?.includes(v))
|
||||
? { some: { userId: req.user.sub } }
|
||||
: undefined,
|
||||
NOT: { headOfficeId: filter === "sub" && !headOfficeId ? null : undefined },
|
||||
OR: globalAllow(req.user)
|
||||
? undefined
|
||||
: [
|
||||
{ user: !globalAllow(req.user) ? { some: { userId: req.user.sub } } : undefined },
|
||||
{
|
||||
headOffice: !globalAllow(req.user)
|
||||
? { user: { some: { userId: req.user.sub } } }
|
||||
: undefined,
|
||||
},
|
||||
],
|
||||
},
|
||||
OR: [
|
||||
{ nameEN: { contains: query } },
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue