import { Controller, Get, Post, Put, Delete, Patch, Route, Security, Tags, Body, Path, Request, Example, Query, } from "tsoa"; import { Salarys, CreateSalary, UpdateSalary } from "../entities/Salarys"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import { AppDataSource } from "../database/data-source"; import { In, IsNull, Not } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; @Route("api/v1/salary") @Tags("Salary") @Security("bearerAuth") export class Salary extends Controller { private salaryRepository = AppDataSource.getRepository(Salarys); private poTypeRepository = AppDataSource.getRepository(PosType); private posLevelRepository = AppDataSource.getRepository(PosLevel); /** * API สร้างผังเงินเดือน * * @summary SLR_001 - สร้างผังเงินเดือน #1 * */ @Post() @Example({ salaryType: "string", //*ประเภทผัง (OFFICER->"ข้าราชการกรุงเทพมหานครสามัญ",EMPLOYEE->"ลูกจ้างประจำกรุงเทพมหานคร") posTypeId: "string(Guid)", //*ระดับของตำแหน่ง posLevelId: "string(Guid)", //*ประเภทของตำแหน่ง isActive: "boolean", //*สถานะการใช้งาน date: "datetime", //ให้ไว้ ณ วันที่ startDate: "datetime", //วันที่มีผลบังคับใช้ endDate: "datetime", //วันที่สิ้นสุดบังคับใช้ detail: "string", //คำอธิบาย }) async create_salary( @Body() requestBody: CreateSalary, @Request() request: { user: Record }, ) { const salarys = Object.assign(new Salarys(), requestBody); if (!salarys) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } const chk_salaryType = ["OFFICER", "EMPLOYEE"]; if (!chk_salaryType.includes(salarys.salaryType.toUpperCase())) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทผัง ไม่ถูกต้อง"); } const chk_posTypeId = await this.poTypeRepository.findOne({ where: { id: salarys.posTypeId }, }); if (!chk_posTypeId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทของตำแหน่ง ไม่ถูกต้อง"); } const chk_posLevelId = await this.posLevelRepository.findOne({ where: { id: salarys.posLevelId }, }); if (!chk_posLevelId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ระดับของตำแหน่ง ไม่ถูกต้อง"); } const chk_3fields = await this.salaryRepository.findOne({ where: { salaryType: salarys.salaryType, posTypeId: salarys.posTypeId, posLevelId: salarys.posLevelId, }, }); if (chk_3fields && salarys.isActive) { salarys.isActive = false; } try { salarys.salaryType = salarys.salaryType.toUpperCase(); salarys.createdUserId = request.user.sub; salarys.createdFullName = request.user.name; salarys.lastUpdateUserId = request.user.sub; salarys.lastUpdateFullName = request.user.name; await this.salaryRepository.save(salarys); return new HttpSuccess(salarys.id); } catch (error) { return error; } } /** * API แก้ไขผังเงินเดือน * * @summary SLR_002 - แก้ไขผังเงินเดือน #2 * * @param {string} id Guid, *Id ผังเงินเดือน */ @Put("{id}") @Example({ salaryType: "string", //*ประเภทผัง (OFFICER->"ข้าราชการกรุงเทพมหานครสามัญ",EMPLOYEE->"ลูกจ้างประจำกรุงเทพมหานคร") posTypeId: "string(Guid)", //*ระดับของตำแหน่ง posLevelId: "string(Guid)", //*ประเภทของตำแหน่ง isActive: "boolean", //*สถานะการใช้งาน date: "datetime", //ให้ไว้ ณ วันที่ startDate: "datetime", //วันที่มีผลบังคับใช้ endDate: "datetime", //วันที่สิ้นสุดบังคับใช้ detail: "string", //คำอธิบาย }) async update_salary( @Path() id: string, @Body() requestBody: UpdateSalary, @Request() request: { user: Record }, ) { const chk_Salary = await this.salaryRepository.findOne({ where: { id: id }, }); if (!chk_Salary) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); } if (chk_Salary.isActive && !requestBody.isActive) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถแก้ไขสถานะการใช้งานที่เป็น Default ได้", ); } const chk_3fields = await this.salaryRepository.findOne({ where: { salaryType: requestBody.salaryType, posTypeId: requestBody.posTypeId, posLevelId: requestBody.posLevelId, isActive: true, id: Not(id), }, }); if (chk_3fields != null && requestBody.isActive) { chk_3fields.isActive = false; chk_3fields.lastUpdateUserId = request.user.sub; chk_3fields.lastUpdateFullName = request.user.name; chk_3fields.lastUpdatedAt = new Date(); await this.salaryRepository.save(chk_3fields); } const chk_salaryType = ["OFFICER", "EMPLOYEE"]; if (!chk_salaryType.includes(String(requestBody.salaryType).toUpperCase())) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทผัง ไม่ถูกต้อง"); } const chk_posTypeId = await this.poTypeRepository.findOne({ where: { id: requestBody.posTypeId }, }); if (!chk_posTypeId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทของตำแหน่ง ไม่ถูกต้อง"); } const chk_posLevelId = await this.posLevelRepository.findOne({ where: { id: requestBody.posLevelId }, }); if (!chk_posLevelId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ระดับของตำแหน่ง ไม่ถูกต้อง"); } try { chk_Salary.lastUpdateUserId = request.user.sub; chk_Salary.lastUpdateFullName = request.user.name; chk_Salary.lastUpdatedAt = new Date(); this.salaryRepository.merge(chk_Salary, requestBody); await this.salaryRepository.save(chk_Salary); return new HttpSuccess(id); } catch (error) { return error; } } /** * API ลบผังเงินเดือน * * @summary SLR_003 - ลบผังเงินเดือน #3 * * @param {string} id Guid, *Id ผังเงินเดือน */ @Delete("{id}") async delete_salary(@Path() id: string) { const chk_Salary = await this.salaryRepository.findOne({ where: { id: id }, }); if (!chk_Salary) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); } if (chk_Salary.isActive) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถลบไอดี: " + id + " ได้ เนื่องสถานะการใช้งานที่เป็น Default", ); } try { await this.salaryRepository.remove(chk_Salary); return new HttpSuccess(); } catch (error) { return error; } } /** * API รายละเอียดผังเงินเดือน * * @summary SLR_004 - รายละเอียดผังเงินเดือน #4 * * @param {string} id Guid, *Id ผังเงินเดือน */ @Get("{id}") @Example({ salaryType: "string", //*ประเภทผัง (OFFICER->"ข้าราชการกรุงเทพมหานครสามัญ",EMPLOYEE->"ลูกจ้างประจำกรุงเทพมหานคร") posTypeId: "string(Guid)", //*ระดับของตำแหน่ง posLevelId: "string(Guid)", //*ประเภทของตำแหน่ง isActive: "boolean", //*สถานะการใช้งาน date: "datetime", //ให้ไว้ ณ วันที่ startDate: "datetime", //วันที่มีผลบังคับใช้ endDate: "datetime", //วันที่สิ้นสุดบังคับใช้ detail: "string", //คำอธิบาย }) async GetSalaryById(@Path() id: string) { try { const salary = await this.salaryRepository.findOne({ where: { id: id }, select: [ "salaryType", "posTypeId", "posLevelId", "isActive", "date", "startDate", "endDate", "details", ], }); if (!salary) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); } return new HttpSuccess(salary); } catch (error) { return error; } } /** * API รายการผังเงินเดือน * * @summary SLR_005 - รายการผังเงินเดือน #5 * */ @Get() async listSalary( @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { const [salary, total] = await this.salaryRepository.findAndCount({ relations: ["posLevel_", "posType_"], order: { startDate: "DESC", }, skip: (page - 1) * pageSize, take: pageSize, }); if (keyword != undefined && keyword !== "") { const filteredSalary = salary.filter( (x) => x.salaryType?.toString().includes(keyword) || x.posLevel_?.posLevelName?.toString().includes(keyword) || x.posType_?.posTypeName?.toString().includes(keyword) || x.isActive?.toString().includes(keyword) || x.date?.toString().includes(keyword) || x.startDate?.toString().includes(keyword) || x.endDate?.toString().includes(keyword) || x.details?.toString().includes(keyword), ); const formattedData = filteredSalary.map((item) => ({ id: item.id, salaryType: item.salaryType, posType: item.posType_?.posTypeName, posLevel: item.posLevel_?.posLevelName, isActive: item.isActive, date: item.date, startDate: item.startDate, endDate: item.endDate, details: item.details, })); return new HttpSuccess({ data: formattedData, total: formattedData.length }); } if (!salary) { return new HttpSuccess([]); } const formattedData = salary.map((item) => ({ id: item.id, salaryType: item.salaryType, posType: item.posType_?.posTypeName, posLevel: item.posLevel_?.posLevelName, isActive: item.isActive, date: item.date, startDate: item.startDate, endDate: item.endDate, details: item.details, })); try { return new HttpSuccess({ data: formattedData, total }); } catch (error) { return error; } } }