refactor: roles
This commit is contained in:
parent
dfb2ab3928
commit
cae2ab7ba3
1 changed files with 11 additions and 6 deletions
|
|
@ -25,6 +25,7 @@ if (!process.env.MINIO_BUCKET) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
||||||
|
const MANAGE_ROLES = ["system", "head_of_admin", "admin"];
|
||||||
|
|
||||||
type BranchCreate = {
|
type BranchCreate = {
|
||||||
status?: Status;
|
status?: Status;
|
||||||
|
|
@ -176,6 +177,7 @@ export class BranchController extends Controller {
|
||||||
@Get()
|
@Get()
|
||||||
@Security("keycloak")
|
@Security("keycloak")
|
||||||
async getBranch(
|
async getBranch(
|
||||||
|
@Request() req: RequestWithUser,
|
||||||
@Query() zipCode?: string,
|
@Query() zipCode?: string,
|
||||||
@Query() filter?: "head" | "sub",
|
@Query() filter?: "head" | "sub",
|
||||||
@Query() headOfficeId?: string,
|
@Query() headOfficeId?: string,
|
||||||
|
|
@ -189,6 +191,9 @@ export class BranchController extends Controller {
|
||||||
AND: {
|
AND: {
|
||||||
zipCode,
|
zipCode,
|
||||||
headOfficeId: headOfficeId ?? (filter === "head" || tree ? null : undefined),
|
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 },
|
NOT: { headOfficeId: filter === "sub" && !headOfficeId ? null : undefined },
|
||||||
},
|
},
|
||||||
OR: [
|
OR: [
|
||||||
|
|
@ -280,7 +285,7 @@ export class BranchController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
@Security("keycloak", ["system", "head_of_admin", "admin"])
|
@Security("keycloak", MANAGE_ROLES)
|
||||||
async createBranch(@Request() req: RequestWithUser, @Body() body: BranchCreate) {
|
async createBranch(@Request() req: RequestWithUser, @Body() body: BranchCreate) {
|
||||||
const [province, district, subDistrict, head] = await prisma.$transaction([
|
const [province, district, subDistrict, head] = await prisma.$transaction([
|
||||||
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
||||||
|
|
@ -415,7 +420,7 @@ export class BranchController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put("{branchId}")
|
@Put("{branchId}")
|
||||||
@Security("keycloak", ["system", "head_of_admin", "admin", "branch_admin", "branch_manager"])
|
@Security("keycloak", MANAGE_ROLES.concat("branch_manager"))
|
||||||
async editBranch(
|
async editBranch(
|
||||||
@Request() req: RequestWithUser,
|
@Request() req: RequestWithUser,
|
||||||
@Body() body: BranchUpdate,
|
@Body() body: BranchUpdate,
|
||||||
|
|
@ -538,7 +543,7 @@ export class BranchController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete("{branchId}")
|
@Delete("{branchId}")
|
||||||
@Security("keycloak", ["system", "head_of_admin", "admin", "branch_manager"])
|
@Security("keycloak", MANAGE_ROLES)
|
||||||
async deleteBranch(@Request() req: RequestWithUser, @Path() branchId: string) {
|
async deleteBranch(@Request() req: RequestWithUser, @Path() branchId: string) {
|
||||||
const record = await prisma.branch.findFirst({
|
const record = await prisma.branch.findFirst({
|
||||||
include: {
|
include: {
|
||||||
|
|
@ -616,7 +621,7 @@ export class BranchController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put("{branchId}/line-image")
|
@Put("{branchId}/line-image")
|
||||||
@Security("keycloak", ["system", "head_of_admin", "admin", "branch_admin", "branch_manager"])
|
@Security("keycloak", MANAGE_ROLES.concat("branch_manager"))
|
||||||
async setLineImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
|
async setLineImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
|
||||||
const record = await prisma.branch.findUnique({
|
const record = await prisma.branch.findUnique({
|
||||||
include: {
|
include: {
|
||||||
|
|
@ -657,7 +662,7 @@ export class BranchController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put("{branchId}/branch-image")
|
@Put("{branchId}/branch-image")
|
||||||
@Security("keycloak", ["system", "head_of_admin", "admin", "branch_admin", "branch_manager"])
|
@Security("keycloak", MANAGE_ROLES.concat("branch_manager"))
|
||||||
async setBranchImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
|
async setBranchImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
|
||||||
const record = await prisma.branch.findUnique({
|
const record = await prisma.branch.findUnique({
|
||||||
include: {
|
include: {
|
||||||
|
|
@ -698,7 +703,7 @@ export class BranchController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put("{branchId}/map-image")
|
@Put("{branchId}/map-image")
|
||||||
@Security("keycloak", ["system", "head_of_admin", "admin", "branch_admin", "branch_manager"])
|
@Security("keycloak", MANAGE_ROLES.concat("branch_manager"))
|
||||||
async setMapImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
|
async setMapImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) {
|
||||||
const record = await prisma.branch.findUnique({
|
const record = await prisma.branch.findUnique({
|
||||||
include: {
|
include: {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue