import { Controller, Get, Post, Put, Delete, 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 { Brackets, Like, Not } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { SalaryRanks } from "../entities/SalaryRanks"; import { randomUUID } from "crypto"; @Route("api/v1/salary") @Tags("Salary") @Security("bearerAuth") export class SalaryController extends Controller { private salaryRepository = AppDataSource.getRepository(Salarys); private salaryRankRepository = AppDataSource.getRepository(SalaryRanks); private poTypeRepository = AppDataSource.getRepository(PosType); private posLevelRepository = AppDataSource.getRepository(PosLevel); /** * API สร้างผังเงินเดือน * * @summary SLR_001 - สร้างผังเงินเดือน #1 * */ @Post() @Example({ name: "string", //*ชื่อผัง 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_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, "ระดับของตำแหน่ง ไม่ถูกต้อง"); } let chk_fields: any; if (salarys.isSpecial) { chk_fields = await this.salaryRepository.findOne({ where: { posTypeId: salarys.posTypeId, posLevelId: salarys.posLevelId, isSpecial: true, }, }); } else { chk_fields = await this.salaryRepository.findOne({ where: { posTypeId: salarys.posTypeId, posLevelId: salarys.posLevelId, isSpecial: false, }, }); } if (chk_fields && salarys.isActive) { salarys.isActive = false; } salarys.name = salarys.name; salarys.isSpecial = salarys.isSpecial; 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); } /** * API แก้ไขผังเงินเดือน * * @summary SLR_002 - แก้ไขผังเงินเดือน #2 * * @param {string} id Guid, *Id ผังเงินเดือน */ @Put("{id}") @Example({ name: "string", //*ชื่อผัง 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, "ไม่พบข้อมูลผังเงินเดือนนี้"); } if (chk_Salary.isActive && !requestBody.isActive) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถแก้ไขสถานะการใช้งานที่เป็น Default ได้", ); } let chk_fields: any; if (chk_Salary.isSpecial) { chk_fields = await this.salaryRepository.find({ where: { posTypeId: requestBody.posTypeId, posLevelId: requestBody.posLevelId, isActive: true, isSpecial: true, id: Not(id), }, }); } else { chk_fields = await this.salaryRepository.find({ where: { posTypeId: requestBody.posTypeId, posLevelId: requestBody.posLevelId, isActive: true, isSpecial: false, id: Not(id), }, }); } if (chk_fields.length > 0 && requestBody.isActive) { chk_fields.forEach(async (item: any) => { item.isActive = false; item.lastUpdateUserId = request.user.sub; item.lastUpdateFullName = request.user.name; item.lastUpdatedAt = new Date(); await this.salaryRepository.save(chk_fields); }); } 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, "ระดับของตำแหน่ง ไม่ถูกต้อง"); } const mergeData = Object.assign(new Salarys(), requestBody); chk_Salary.lastUpdateUserId = request.user.sub; chk_Salary.lastUpdateFullName = request.user.name; chk_Salary.lastUpdatedAt = new Date(); this.salaryRepository.merge(chk_Salary, mergeData); await this.salaryRepository.save(chk_Salary); return new HttpSuccess(id); } /** * 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, "ไม่พบข้อมูลผังเงินเดือนนี้"); } if (chk_Salary.isActive) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถลบข้อมูลนี้ได้ เนื่องจากเปิดใช้งานอยู่", ); } const del_SalaryRank = await this.salaryRankRepository.find({ where: { salaryId: chk_Salary.id }, }); await this.salaryRankRepository.remove(del_SalaryRank); await this.salaryRepository.remove(chk_Salary); return new HttpSuccess(); } /** * API รายละเอียดผังเงินเดือน * * @summary SLR_004 - รายละเอียดผังเงินเดือน #4 * * @param {string} id Guid, *Id ผังเงินเดือน */ @Get("{id}") @Example({ name: "string", //*ชื่อผัง posTypeId: "string(Guid)", //*ระดับของตำแหน่ง posLevelId: "string(Guid)", //*ประเภทของตำแหน่ง isActive: "boolean", //*สถานะการใช้งาน date: "datetime", //ให้ไว้ ณ วันที่ startDate: "datetime", //วันที่มีผลบังคับใช้ endDate: "datetime", //วันที่สิ้นสุดบังคับใช้ detail: "string", //คำอธิบาย }) async GetSalaryById(@Path() id: string) { const salary = await this.salaryRepository.findOne({ relations: ["posType_", "posLevel_"], where: { id: id }, }); const formattedData = { name: salary?.name ?? null, isSpecial: salary?.isSpecial ?? null, posTypeId: salary?.posTypeId ?? null, posTypeName: salary?.posType_.posTypeName ?? null, posLevelId: salary?.posLevelId ?? null, posLevelName: salary?.posLevel_.posLevelName ?? null, isActive: salary?.isActive ?? null, date: salary?.date ?? null, startDate: salary?.startDate ?? null, endDate: salary?.endDate ?? null, details: salary?.details ?? null, }; if (!salary) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้"); } return new HttpSuccess(formattedData); } /** * 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 AppDataSource.getRepository(Salarys) .createQueryBuilder("salary") .leftJoinAndSelect("salary.posType_", "posType_") .leftJoinAndSelect("salary.posLevel_", "posLevel_") .andWhere( new Brackets((qb) => { qb.orWhere("salary.name LIKE :keyword", { keyword: `%${keyword}%` }) .orWhere("posType_.posTypeName LIKE :keyword", { keyword: `%${keyword}%` }) .orWhere("posLevel_.posLevelName LIKE :keyword", { keyword: `%${keyword}%` }); }), ) .orderBy("salary.isActive", "DESC") .addOrderBy("posType_.posTypeRank", "DESC") .addOrderBy("posLevel_.posLevelRank", "DESC") .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); const _salary = salary.map((item) => ({ id: item.id, name: item.name, isSpecial: item.isSpecial, posTypeId: item.posType_?.id, posType: item.posType_?.posTypeName, posLevelId: item.posLevel_?.id, posLevel: item.posLevel_?.posLevelName, isActive: item.isActive, date: item.date, startDate: item.startDate, endDate: item.endDate, details: item.details, })); return new HttpSuccess({ data: _salary, total }); } /** * API copy ผังเงินเดือน * * @summary SLR_008 - copy ผังเงินเดือน #8 * */ @Post("copy") async copySalary( @Body() body: { id: string }, @Request() request: { user: Record }, ) { const salary = await this.salaryRepository.findOne({ relations: ["posLevel_", "posType_", "salaryRanks_"], where: { id: body.id }, }); if (!salary) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผังเงินเดือนนี้"); } const salaryRank = await this.salaryRankRepository.find({ where: { salaryId: salary?.id }, }); const newSalary = { ...salary, id: randomUUID(), isActive: false }; await this.salaryRepository.save(newSalary); await Promise.all( salaryRank.map(async (v) => { const newSalaryRank = { ...v, id: randomUUID() }; await this.salaryRankRepository.save(newSalaryRank); }), ); return new HttpSuccess({ id: newSalary.id }); } }