import { Controller, Get, Post, Put, Delete, Patch, Route, Security, Tags, Body, Path, Request, Example, SuccessResponse, Response, Query, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; import { PosLevel, CreatePosLevel, UpdatePosLevel } from "../entities/PosLevel"; import HttpError from "../interfaces/http-error"; import { In, Like, Not } from "typeorm"; import { CreateSalaryFormulaEmployee, SalaryFormulaEmployee, UpdateSalaryFormulaEmployee, } from "../entities/SalaryFormulaEmployee"; import { EmployeePosLevel } from "../entities/EmployeePosLevel"; import { EmployeePosType } from "../entities/EmployeePosType"; import { SalaryEmployee } from "../entities/SalaryEmployee"; @Route("api/v1/salary/formula") @Tags("SalaryFormula") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class SalaryFormulaEmployeeController extends Controller { private salaryFormulaEmployeeRepository = AppDataSource.getRepository(SalaryFormulaEmployee); private employeePosLevelRepository = AppDataSource.getRepository(EmployeePosLevel); private employeePosTypeRepository = AppDataSource.getRepository(EmployeePosType); private salaryEmployeeRepository = AppDataSource.getRepository(SalaryEmployee); /** * API เพิ่มหลักเกณฑ์ * * */ @Post() async createFormula( @Body() requestBody: CreateSalaryFormulaEmployee, @Request() request: { user: Record }, ) { const formula = Object.assign(new SalaryFormulaEmployee(), requestBody); const chkPosLevel = await this.employeePosLevelRepository.findOne({ where: { id: requestBody.posLevelId, }, }); if (!chkPosLevel) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานนี้"); const chkPosType = await this.employeePosTypeRepository.findOne({ where: { id: requestBody.posTypeId, }, }); if (!chkPosType) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานตำแหน่งนี้"); const chkSalaryEmployee = await this.salaryEmployeeRepository.findOne({ where: { id: requestBody.salaryEmployeeId, }, }); if (!chkSalaryEmployee) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังค่าจ้างนี้"); const chkFormula = await this.salaryFormulaEmployeeRepository.findOne({ where: { position: requestBody.position, posTypeId: requestBody.posTypeId, posLevelId: requestBody.posLevelId, }, }); if (chkFormula) throw new HttpError(HttpStatusCode.NOT_FOUND, "หลักเกณฑ์นี้มีอยู่ในระบบแล้ว"); const chkSalaryEmployeeMin = await this.salaryEmployeeRepository.find({ where: { id: In(requestBody.salaryEmployeeMinIds), }, }); formula.salaryEmployeeMins = chkSalaryEmployeeMin; formula.createdUserId = request.user.sub; formula.createdFullName = request.user.name; formula.lastUpdateUserId = request.user.sub; formula.lastUpdateFullName = request.user.name; await this.salaryFormulaEmployeeRepository.save(formula); return new HttpSuccess(formula); } /** * API แก้ไขหลักเกณฑ์ * * * @param {string} id Id หลักเกณฑ์ */ @Put("{id}") async editFormula( @Path() id: string, @Body() requestBody: UpdateSalaryFormulaEmployee, @Request() request: { user: Record }, ) { const formula = await this.salaryFormulaEmployeeRepository.findOne({ where: { id } }); if (!formula) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลหลักเกณฑ์นี้"); const chkFormula = await this.salaryFormulaEmployeeRepository.findOne({ where: { id: Not(id), position: requestBody.position, posTypeId: requestBody.posTypeId, posLevelId: requestBody.posLevelId, }, }); if (chkFormula) throw new HttpError(HttpStatusCode.NOT_FOUND, "หลักเกณฑ์นี้มีอยู่ในระบบแล้ว"); const chkPosLevel = await this.employeePosLevelRepository.findOne({ where: { id: requestBody.posLevelId, }, }); if (!chkPosLevel) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานนี้"); const chkPosType = await this.employeePosTypeRepository.findOne({ where: { id: requestBody.posTypeId, }, }); if (!chkPosType) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานตำแหน่งนี้"); const chkSalaryEmployee = await this.salaryEmployeeRepository.findOne({ where: { id: requestBody.salaryEmployeeId, }, }); if (!chkSalaryEmployee) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังค่าจ้างนี้"); const chkSalaryEmployeeMin = await this.salaryEmployeeRepository.find({ where: { id: In(requestBody.salaryEmployeeMinIds), }, }); formula.position = requestBody.position; formula.salaryMin = requestBody.salaryMin; formula.salary = requestBody.salary; formula.salaryMax = requestBody.salaryMax; formula.details = requestBody.details; formula.salaryEmployeeId = requestBody.salaryEmployeeId; formula.posTypeId = requestBody.posTypeId; formula.posLevelId = requestBody.posLevelId; formula.salaryEmployeeMins = chkSalaryEmployeeMin; formula.lastUpdateUserId = request.user.sub; formula.lastUpdateFullName = request.user.name; await this.salaryFormulaEmployeeRepository.save(formula); return new HttpSuccess(formula.id); } /** * API ลบหลักเกณฑ์ * * * @param {string} id Id หลักเกณฑ์ */ @Delete("{id}") async deleteFormula(@Path() id: string) { const delFormula = await this.salaryFormulaEmployeeRepository.findOne({ where: { id } }); if (!delFormula) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังค่าจ้างนี้"); await this.salaryFormulaEmployeeRepository.remove(delFormula); return new HttpSuccess(); } /** * API รายละเอียดหลักเกณฑ์ * * * @param {string} id Id หลักเกณฑ์ */ @Get("{id}") async getFormulaDetail(@Path() id: string) { const getFormula = await this.salaryFormulaEmployeeRepository.findOne({ relations: ["salaryEmployee", "posType", "posLevel", "salaryEmployeeMins"], where: { id: id }, }); if (!getFormula) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังค่าจ้างนี้"); const mapFormula = { id: getFormula.id, posLevelId: getFormula.posLevelId, position: getFormula.position, posTypeId: getFormula.posTypeId, details: getFormula.details, salaryMin: getFormula.salaryMin, salary: getFormula.salary, salaryMax: getFormula.salaryMax, salaryEmployeeId: getFormula.salaryEmployeeId, salaryEmployeeMinIds: getFormula.salaryEmployeeMins.map((x) => x.id), }; return new HttpSuccess(mapFormula); } /** * API รายการหลักเกณฑ์ * * */ @Get() async getFormula( @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, @Query("posTypeId") posTypeId?: string, ) { const [getFormula, total] = await AppDataSource.getRepository(SalaryFormulaEmployee) .createQueryBuilder("salaryFormulaEmployee") .leftJoinAndSelect("salaryFormulaEmployee.salaryEmployee", "salaryEmployee") .leftJoinAndSelect("salaryFormulaEmployee.posType", "posType") .leftJoinAndSelect("salaryFormulaEmployee.posLevel", "posLevel") .leftJoinAndSelect("salaryFormulaEmployee.salaryEmployeeMins", "salaryEmployeeMins") .andWhere( keyword == undefined ? "1=1" : [ { details: Like(`%${keyword}%`) }, { position: Like(`%${keyword}%`) }, { posLevel: { posLevelName: Like(`%${keyword}%`) } }, ], ) .andWhere(posTypeId == undefined ? "1=1" : { posTypeId: Like(`%${posTypeId}%`) }) .orderBy("salaryFormulaEmployee.lastUpdatedAt", "DESC") .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); const mapFormula = getFormula.map((item) => ({ id: item.id, posLevel: item.posLevel != null && item.posType != null ? `${item.posType.posTypeShortName} ${item.posLevel.posLevelName}` : null, position: item.position, posType: item.posType != null ? item.posType.posTypeName : null, details: item.details, salaryMin: item.salaryMin, salary: item.salary, salaryMax: item.salaryMax, group: item.salaryEmployee != null ? item.salaryEmployee.group : null, salaryEmployeeMin: item.salaryEmployeeMins != null ? item.salaryEmployeeMins.map((x) => x.group) : null, })); return new HttpSuccess({ data: mapFormula, total }); } }