import { Body, Controller, Delete, Get, Middlewares, Path, Post, Put, Request, Route, Security, Tags, } from "tsoa"; import { RequestWithUser } from "../interfaces/user"; import prisma from "../db"; import HttpStatus from "../interfaces/http-status"; import { permissionCheck } from "../middlewares/employee"; import { notFoundError } from "../utils/error"; const MANAGE_ROLES = [ "system", "head_of_admin", "admin", "head_of_accountant", "accountant", "head_of_sale", ]; function globalAllow(user: RequestWithUser["user"]) { const allowList = ["system", "head_of_admin", "head_of_accountant", "head_of_sale"]; return allowList.some((v) => user.roles?.includes(v)); } type EmployeeWorkPayload = { ownerName?: string | null; positionName?: string | null; jobType?: string | null; workplace?: string | null; identityNo?: string | null; workPermitNo?: string | null; workPermitIssueDate?: Date | null; workPermitExpireDate?: Date | null; workPermitAt?: string | null; }; @Route("api/v1/employee/{employeeId}/work") @Tags("Employee Work") @Middlewares(permissionCheck(globalAllow)) export class EmployeeWorkController extends Controller { @Get() @Security("keycloak") async list(@Path() employeeId: string) { return prisma.employeeWork.findMany({ include: { createdBy: true, updatedBy: true, }, orderBy: { createdAt: "asc" }, where: { employeeId }, }); } @Get("{workId}") @Security("keycloak") async getById(@Path() employeeId: string, @Path() workId: string) { const record = await prisma.employeeWork.findFirst({ include: { createdBy: true, updatedBy: true, }, where: { id: workId, employeeId }, }); if (!record) throw notFoundError("Employee Work"); return record; } @Post() @Security("keycloak", MANAGE_ROLES) async create( @Request() req: RequestWithUser, @Path() employeeId: string, @Body() body: EmployeeWorkPayload, ) { const record = await prisma.employeeWork.create({ include: { createdBy: true, updatedBy: true, }, data: { ...body, employee: { connect: { id: employeeId } }, createdBy: { connect: { id: req.user.sub } }, updatedBy: { connect: { id: req.user.sub } }, }, }); this.setStatus(HttpStatus.CREATED); return record; } @Put("{workId}") @Security("keycloak", MANAGE_ROLES) async editById( @Request() req: RequestWithUser, @Path() employeeId: string, @Path() workId: string, @Body() body: EmployeeWorkPayload, ) { const work = await prisma.employeeWork.findUnique({ where: { id: workId, employeeId } }); if (!work) throw notFoundError("Employee Work"); const record = await prisma.employeeWork.update({ include: { createdBy: true, updatedBy: true, }, where: { id: workId, employeeId }, data: { ...body, updatedByUserId: req.user.sub }, }); this.setStatus(HttpStatus.CREATED); return record; } @Delete("{workId}") @Security("keycloak", MANAGE_ROLES) async deleteById(@Path() employeeId: string, @Path() workId: string) { const record = await prisma.employeeWork.findFirst({ include: { createdBy: true, updatedBy: true, }, where: { id: workId, employeeId }, }); if (!record) throw notFoundError("Employee Work"); return await prisma.employeeWork.delete({ where: { id: workId, employeeId } }); } }