From 4b7acf9a4341aaa5268c684b50d55ccc649e55af Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Fri, 7 Jun 2024 14:49:11 +0700 Subject: [PATCH] feat: employee work endpoints --- src/controllers/employee-work-controller.ts | 122 ++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/controllers/employee-work-controller.ts diff --git a/src/controllers/employee-work-controller.ts b/src/controllers/employee-work-controller.ts new file mode 100644 index 0000000..c3cde09 --- /dev/null +++ b/src/controllers/employee-work-controller.ts @@ -0,0 +1,122 @@ +import { + Body, + Controller, + Delete, + Get, + 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 HttpError from "../interfaces/http-error"; + +type EmployeeWorkCreate = { + ownerName: string; + positionName: string; + jobType: string; + workplace: string; + workPermitNo: string; + workPermitIssuDate: Date; + workPermitExpireDate: Date; + workEndDate: Date; +}; + +type EmployeeWorkUpdate = { + ownerName?: string; + positionName?: string; + jobType?: string; + workplace?: string; + workPermitNo?: string; + workPermitIssuDate?: Date; + workPermitExpireDate?: Date; + workEndDate?: Date; +}; + +@Route("api/v1/employee/{employeeId}/work") +@Tags("Employee Work") +@Security("keycloak") +export class EmployeeWorkController extends Controller { + @Get() + async list(@Path() employeeId: string) { + return prisma.employeeWork.findMany({ + orderBy: { createdAt: "asc" }, + where: { employeeId }, + }); + } + + @Get("{workId}") + async getById(@Path() employeeId: string, @Path() workId: string) { + const record = await prisma.employeeWork.findFirst({ + where: { id: workId, employeeId }, + }); + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "Employee work cannot be found.", "data_not_found"); + } + return record; + } + + @Post() + async create( + @Request() req: RequestWithUser, + @Path() employeeId: string, + @Body() body: EmployeeWorkCreate, + ) { + if (!(await prisma.employee.findUnique({ where: { id: employeeId } }))) + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Employee cannot be found.", + "missing_or_invalid_parameter", + ); + + const record = await prisma.employeeWork.create({ + data: { + ...body, + employee: { connect: { id: employeeId } }, + createdBy: req.user.name, + updateBy: req.user.name, + }, + }); + + this.setStatus(HttpStatus.CREATED); + + return record; + } + + @Put("{workId}") + async editById( + @Request() req: RequestWithUser, + @Path() employeeId: string, + @Path() workId: string, + @Body() body: EmployeeWorkUpdate, + ) { + if (!(await prisma.employeeWork.findUnique({ where: { id: workId, employeeId } }))) { + throw new HttpError(HttpStatus.NOT_FOUND, "Employee work cannot be found.", "data_not_found"); + } + + const record = await prisma.employeeWork.update({ + where: { id: workId, employeeId }, + data: { ...body, createdBy: req.user.name, updateBy: req.user.name }, + }); + + this.setStatus(HttpStatus.CREATED); + + return record; + } + + @Delete("{workId}") + async deleteById(@Path() employeeId: string, @Path() workId: string) { + const record = await prisma.employeeWork.findFirst({ where: { id: workId, employeeId } }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "Employee work cannot be found.", "data_not_found"); + } + + return await prisma.employeeWork.delete({ where: { id: workId, employeeId } }); + } +}