import { Controller, Post, Put, Delete, Route, Security, Tags, Body, Path, Request, SuccessResponse, Response, Get, Query, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { CreateSalaryRankEmployee, SalaryRankEmployees, UpdateSalaryRankEmployee, } from "../entities/SalaryRankEmployees"; import { Salarys } from "../entities/Salarys"; @Route("api/v1/salary/rate") @Tags("SalaryRankEmployee") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class SalaryRankEmployeesController extends Controller { private salaryRankEmployeeRepository = AppDataSource.getRepository(SalaryRankEmployees); private salaryRepository = AppDataSource.getRepository(Salarys); /** * API สร้างอัตราเงินเดือนลูกจ้าง * * */ @Post() async CreateSalaryRankEmployee( @Body() requestBody: CreateSalaryRankEmployee, @Request() request: { user: Record }, ) { try { const checkSalary = await this.salaryRepository.findOne({ where: { id: requestBody.salaryEmployeeId }, }); if (!checkSalary) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้"); } const salaryRankEmployee = Object.assign(new SalaryRankEmployees(), requestBody); salaryRankEmployee.createdUserId = request.user.sub; salaryRankEmployee.createdFullName = request.user.name; salaryRankEmployee.lastUpdateUserId = request.user.sub; salaryRankEmployee.lastUpdateFullName = request.user.name; await this.salaryRankEmployeeRepository.save(salaryRankEmployee); return new HttpSuccess(); } catch (error: any) { throw new Error(error); } } /** * API แก้ไขอัตราเงินเดือนลูกจ้าง * * * @param {string} id Id อัตราเงินเดือน */ @Put("{id}") async updateSalaryRankEmployees( @Path() id: string, @Body() requestBody: UpdateSalaryRankEmployee, @Request() request: { user: Record }, ) { const salaryRankEmployee = await this.salaryRankEmployeeRepository.findOne({ where: { id: id }, }); if (!salaryRankEmployee) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับผังเงินเดือนนี้"); } salaryRankEmployee.lastUpdateUserId = request.user.sub; salaryRankEmployee.lastUpdateFullName = request.user.name; this.salaryRankEmployeeRepository.merge(salaryRankEmployee, requestBody); await this.salaryRankEmployeeRepository.save(salaryRankEmployee); return new HttpSuccess(); } /** * API ลบอัตราเงินเดือนลูกจ้าง * * * @param {string} id Id อัตราเงินเดือน */ @Delete("{id}") async deleteSalaryRankEmployees(@Path() id: string) { const delSalaryRankEmployees = await this.salaryRankEmployeeRepository.findOne({ where: { id }, }); if (!delSalaryRankEmployees) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับผังเงินเดือนนี้"); } await this.salaryRankEmployeeRepository.delete({ id: id }); return new HttpSuccess(); } /** * API รายการอัตราเงินเดือนลูกจ้าง * * * @param {string} id Id ผังเงินเดือน */ @Get("{id}") async listSalaryRankEmployees( @Path() id: string, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { const [salaryRankEmployee, total] = await this.salaryRankEmployeeRepository.findAndCount({ where: { salaryEmployeeId: id, }, select: ["id", "step", "salaryMounth", "salaryDay"], order: { step: "ASC", }, ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), }); if (keyword != undefined && keyword !== "") { const filteredSalaryRankEmployee = salaryRankEmployee.filter( (x) => x.step?.toString().includes(keyword) || x.salaryMounth?.toString().includes(keyword) || x.salaryDay?.toString().includes(keyword), ); const slicedData = filteredSalaryRankEmployee.slice((page - 1) * pageSize, page * pageSize); return new HttpSuccess({ data: slicedData, total: filteredSalaryRankEmployee.length }); } return new HttpSuccess({ data: salaryRankEmployee, total }); } }