feat: protect endpoints with role

This commit is contained in:
Methapon2001 2024-07-03 15:42:49 +07:00
parent 20c7414407
commit 9a310420e5
4 changed files with 60 additions and 4 deletions

View file

@ -16,6 +16,16 @@ import prisma from "../db";
import HttpStatus from "../interfaces/http-status"; import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
const MANAGE_ROLES = [
"system",
"head_of_admin",
"admin",
"branch_admin",
"branch_manager",
"head_of_sale",
"sale",
];
type EmployeeCheckupPayload = { type EmployeeCheckupPayload = {
checkupType?: string | null; checkupType?: string | null;
checkupResult?: string | null; checkupResult?: string | null;
@ -32,9 +42,9 @@ type EmployeeCheckupPayload = {
@Route("api/v1/employee/{employeeId}/checkup") @Route("api/v1/employee/{employeeId}/checkup")
@Tags("Employee Checkup") @Tags("Employee Checkup")
@Security("keycloak")
export class EmployeeCheckupController extends Controller { export class EmployeeCheckupController extends Controller {
@Get() @Get()
@Security("keycloak")
async list(@Path() employeeId: string) { async list(@Path() employeeId: string) {
return prisma.employeeCheckup.findMany({ return prisma.employeeCheckup.findMany({
include: { include: {
@ -47,6 +57,7 @@ export class EmployeeCheckupController extends Controller {
} }
@Get("{checkupId}") @Get("{checkupId}")
@Security("keycloak")
async getById(@Path() employeeId: string, @Path() checkupId: string) { async getById(@Path() employeeId: string, @Path() checkupId: string) {
const record = await prisma.employeeCheckup.findFirst({ const record = await prisma.employeeCheckup.findFirst({
include: { include: {
@ -66,6 +77,7 @@ export class EmployeeCheckupController extends Controller {
} }
@Post() @Post()
@Security("keycloak", MANAGE_ROLES)
async create( async create(
@Request() req: RequestWithUser, @Request() req: RequestWithUser,
@Path() employeeId: string, @Path() employeeId: string,
@ -109,6 +121,7 @@ export class EmployeeCheckupController extends Controller {
} }
@Put("{checkupId}") @Put("{checkupId}")
@Security("keycloak", MANAGE_ROLES)
async editById( async editById(
@Request() req: RequestWithUser, @Request() req: RequestWithUser,
@Path() employeeId: string, @Path() employeeId: string,
@ -165,6 +178,7 @@ export class EmployeeCheckupController extends Controller {
} }
@Delete("{checkupId}") @Delete("{checkupId}")
@Security("keycloak", MANAGE_ROLES)
async deleteById(@Path() employeeId: string, @Path() checkupId: string) { async deleteById(@Path() employeeId: string, @Path() checkupId: string) {
const record = await prisma.employeeCheckup.findFirst({ where: { id: checkupId, employeeId } }); const record = await prisma.employeeCheckup.findFirst({ where: { id: checkupId, employeeId } });

View file

@ -24,6 +24,15 @@ 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",
"branch_admin",
"branch_manager",
"head_of_sale",
"sale",
];
function imageLocation(id: string) { function imageLocation(id: string) {
return `employee/${id}/profile-image`; return `employee/${id}/profile-image`;
@ -200,9 +209,9 @@ type EmployeeUpdate = {
@Route("api/v1/employee") @Route("api/v1/employee")
@Tags("Employee") @Tags("Employee")
@Security("keycloak")
export class EmployeeController extends Controller { export class EmployeeController extends Controller {
@Get("stats") @Get("stats")
@Security("keycloak")
async getEmployeeStats(@Query() customerBranchId?: string) { async getEmployeeStats(@Query() customerBranchId?: string) {
return await prisma.employee.count({ return await prisma.employee.count({
where: { customerBranchId }, where: { customerBranchId },
@ -210,6 +219,7 @@ export class EmployeeController extends Controller {
} }
@Get("stats/gender") @Get("stats/gender")
@Security("keycloak")
async getEmployeeStatsGender( async getEmployeeStatsGender(
@Query() customerBranchId?: string, @Query() customerBranchId?: string,
@Query() status?: Status, @Query() status?: Status,
@ -245,6 +255,7 @@ export class EmployeeController extends Controller {
} }
@Get() @Get()
@Security("keycloak")
async list( async list(
@Query() zipCode?: string, @Query() zipCode?: string,
@Query() gender?: string, @Query() gender?: string,
@ -305,6 +316,7 @@ export class EmployeeController extends Controller {
} }
@Get("{employeeId}") @Get("{employeeId}")
@Security("keycloak")
async getById(@Path() employeeId: string) { async getById(@Path() employeeId: string) {
const record = await prisma.employee.findFirst({ const record = await prisma.employee.findFirst({
include: { include: {
@ -325,6 +337,7 @@ export class EmployeeController extends Controller {
} }
@Post() @Post()
@Security("keycloak", MANAGE_ROLES)
async create(@Request() req: RequestWithUser, @Body() body: EmployeeCreate) { async create(@Request() req: RequestWithUser, @Body() body: EmployeeCreate) {
const [province, district, subDistrict, customerBranch] = await prisma.$transaction([ const [province, district, subDistrict, customerBranch] = await prisma.$transaction([
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }), prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
@ -483,6 +496,7 @@ export class EmployeeController extends Controller {
} }
@Put("{employeeId}") @Put("{employeeId}")
@Security("keycloak", MANAGE_ROLES)
async editById( async editById(
@Request() req: RequestWithUser, @Request() req: RequestWithUser,
@Body() body: EmployeeUpdate, @Body() body: EmployeeUpdate,
@ -709,6 +723,7 @@ export class EmployeeController extends Controller {
} }
@Delete("{employeeId}") @Delete("{employeeId}")
@Security("keycloak", MANAGE_ROLES)
async delete(@Path() employeeId: string) { async delete(@Path() employeeId: string) {
const record = await prisma.employee.findFirst({ where: { id: employeeId } }); const record = await prisma.employee.findFirst({ where: { id: employeeId } });

View file

@ -17,6 +17,16 @@ import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status"; import HttpStatus from "../interfaces/http-status";
import { RequestWithUser } from "../interfaces/user"; import { RequestWithUser } from "../interfaces/user";
const MANAGE_ROLES = [
"system",
"head_of_admin",
"admin",
"branch_admin",
"branch_manager",
"head_of_sale",
"sale",
];
type EmployeeOtherInfoPayload = { type EmployeeOtherInfoPayload = {
citizenId?: string | null; citizenId?: string | null;
fatherFirstName?: string | null; fatherFirstName?: string | null;
@ -34,9 +44,9 @@ type EmployeeOtherInfoPayload = {
@Route("api/v1/employee/{employeeId}/other-info") @Route("api/v1/employee/{employeeId}/other-info")
@Tags("Employee Other Info") @Tags("Employee Other Info")
@Security("keycloak")
export class EmployeeOtherInfo extends Controller { export class EmployeeOtherInfo extends Controller {
@Get() @Get()
@Security("keycloak")
async list(@Path() employeeId: string) { async list(@Path() employeeId: string) {
return prisma.employeeOtherInfo.findFirst({ return prisma.employeeOtherInfo.findFirst({
include: { include: {
@ -49,6 +59,7 @@ export class EmployeeOtherInfo extends Controller {
} }
@Post() @Post()
@Security("keycloak", MANAGE_ROLES)
async create( async create(
@Request() req: RequestWithUser, @Request() req: RequestWithUser,
@Path() employeeId: string, @Path() employeeId: string,
@ -76,6 +87,7 @@ export class EmployeeOtherInfo extends Controller {
} }
@Put("{otherInfoId}") @Put("{otherInfoId}")
@Security("keycloak", MANAGE_ROLES)
async editById( async editById(
@Request() req: RequestWithUser, @Request() req: RequestWithUser,
@Path() employeeId: string, @Path() employeeId: string,
@ -105,6 +117,7 @@ export class EmployeeOtherInfo extends Controller {
} }
@Delete("{otherInfoId}") @Delete("{otherInfoId}")
@Security("keycloak", MANAGE_ROLES)
async deleteById(@Path() employeeId: string, @Path() otherInfoId: string) { async deleteById(@Path() employeeId: string, @Path() otherInfoId: string) {
const record = await prisma.employeeOtherInfo.findFirst({ const record = await prisma.employeeOtherInfo.findFirst({
where: { id: otherInfoId, employeeId }, where: { id: otherInfoId, employeeId },

View file

@ -16,6 +16,16 @@ import prisma from "../db";
import HttpStatus from "../interfaces/http-status"; import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
const MANAGE_ROLES = [
"system",
"head_of_admin",
"admin",
"branch_admin",
"branch_manager",
"head_of_sale",
"sale",
];
type EmployeeWorkPayload = { type EmployeeWorkPayload = {
ownerName?: string | null; ownerName?: string | null;
positionName?: string | null; positionName?: string | null;
@ -30,9 +40,9 @@ type EmployeeWorkPayload = {
@Route("api/v1/employee/{employeeId}/work") @Route("api/v1/employee/{employeeId}/work")
@Tags("Employee Work") @Tags("Employee Work")
@Security("keycloak")
export class EmployeeWorkController extends Controller { export class EmployeeWorkController extends Controller {
@Get() @Get()
@Security("keycloak")
async list(@Path() employeeId: string) { async list(@Path() employeeId: string) {
return prisma.employeeWork.findMany({ return prisma.employeeWork.findMany({
include: { include: {
@ -45,6 +55,7 @@ export class EmployeeWorkController extends Controller {
} }
@Get("{workId}") @Get("{workId}")
@Security("keycloak")
async getById(@Path() employeeId: string, @Path() workId: string) { async getById(@Path() employeeId: string, @Path() workId: string) {
const record = await prisma.employeeWork.findFirst({ const record = await prisma.employeeWork.findFirst({
include: { include: {
@ -64,6 +75,7 @@ export class EmployeeWorkController extends Controller {
} }
@Post() @Post()
@Security("keycloak", MANAGE_ROLES)
async create( async create(
@Request() req: RequestWithUser, @Request() req: RequestWithUser,
@Path() employeeId: string, @Path() employeeId: string,
@ -91,6 +103,7 @@ export class EmployeeWorkController extends Controller {
} }
@Put("{workId}") @Put("{workId}")
@Security("keycloak", MANAGE_ROLES)
async editById( async editById(
@Request() req: RequestWithUser, @Request() req: RequestWithUser,
@Path() employeeId: string, @Path() employeeId: string,
@ -120,6 +133,7 @@ export class EmployeeWorkController extends Controller {
} }
@Delete("{workId}") @Delete("{workId}")
@Security("keycloak", MANAGE_ROLES)
async deleteById(@Path() employeeId: string, @Path() workId: string) { async deleteById(@Path() employeeId: string, @Path() workId: string) {
const record = await prisma.employeeWork.findFirst({ const record = await prisma.employeeWork.findFirst({
include: { include: {