diff --git a/src/controllers/SalaryController.ts b/src/controllers/SalaryController.ts index 9c72317..09e1d83 100644 --- a/src/controllers/SalaryController.ts +++ b/src/controllers/SalaryController.ts @@ -12,6 +12,7 @@ import { Path, Request, Example, + Query, } from "tsoa"; import { Salarys, CreateSalary, UpdateSalary } from "../entities/Salarys"; import { PosType } from "../entities/PosType"; @@ -26,11 +27,10 @@ import HttpStatusCode from "../interfaces/http-status"; @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 สร้างผังเงินเดือน * @@ -38,22 +38,20 @@ export class Salary extends Controller { * */ @Post() - @Example( - { - salaryType: "string", //*ประเภทผัง (OFFICER->"ข้าราชการกรุงเทพมหานครสามัญ",EMPLOYEE->"ลูกจ้างประจำกรุงเทพมหานคร") - posType: "string", //*ระดับของตำแหน่ง - posLevel: "string", //*ประเภทของตำแหน่ง - isActive: "boolean", //*สถานะการใช้งาน - date: "datetime", //ให้ไว้ ณ วันที่ - startDate: "datetime", //วันที่มีผลบังคับใช้ - endDate: "datetime", //วันที่สิ้นสุดบังคับใช้ - detail: "string", //คำอธิบาย - } - ) + @Example({ + salaryType: "string", //*ประเภทผัง (OFFICER->"ข้าราชการกรุงเทพมหานครสามัญ",EMPLOYEE->"ลูกจ้างประจำกรุงเทพมหานคร") + posType: "string", //*ระดับของตำแหน่ง + posLevel: "string", //*ประเภทของตำแหน่ง + 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, "ไม่พบข้อมูล"); @@ -65,16 +63,16 @@ export class Salary extends Controller { } const chk_posTypeId = await this.poTypeRepository.findOne({ - where: { id: salarys.posTypeId } - }) - if(!chk_posTypeId){ + where: { id: salarys.posTypeId }, + }); + if (!chk_posTypeId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทของตำแหน่ง ไม่ถูกต้อง"); } const chk_posLevelId = await this.posLevelRepository.findOne({ - where: { id: salarys.posLevelId } + where: { id: salarys.posLevelId }, }); - if(!chk_posLevelId){ + if (!chk_posLevelId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ระดับของตำแหน่ง ไม่ถูกต้อง"); } @@ -82,13 +80,13 @@ export class Salary extends Controller { where: { salaryType: salarys.salaryType, posTypeId: salarys.posTypeId, - posLevelId: salarys.posLevelId - } - }) - if(chk_3fields && salarys.isActive){ + posLevelId: salarys.posLevelId, + }, + }); + if (chk_3fields && salarys.isActive) { salarys.isActive = false; } - + try { salarys.salaryType = salarys.salaryType.toUpperCase(); salarys.createdUserId = request.user.sub; @@ -110,46 +108,46 @@ export class Salary extends Controller { * @param {string} id Guid, *Id ผังเงินเดือน */ @Put("{id}") - @Example( - { - salaryType: "string", //*ประเภทผัง (OFFICER->"ข้าราชการกรุงเทพมหานครสามัญ",EMPLOYEE->"ลูกจ้างประจำกรุงเทพมหานคร") - posType: "string", //*ระดับของตำแหน่ง - posLevel: "string", //*ประเภทของตำแหน่ง - isActive: "boolean", //*สถานะการใช้งาน - date: "datetime", //ให้ไว้ ณ วันที่ - startDate: "datetime", //วันที่มีผลบังคับใช้ - endDate: "datetime", //วันที่สิ้นสุดบังคับใช้ - detail: "string", //คำอธิบาย - }, - ) + @Example({ + salaryType: "string", //*ประเภทผัง (OFFICER->"ข้าราชการกรุงเทพมหานครสามัญ",EMPLOYEE->"ลูกจ้างประจำกรุงเทพมหานคร") + posType: "string", //*ระดับของตำแหน่ง + posLevel: "string", //*ประเภทของตำแหน่ง + 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); + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); } - if(chk_Salary.isActive && !requestBody.isActive){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถแก้ไขสถานะการใช้งานที่เป็น Default ได้"); + 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) - } + id: Not(id), + }, }); - if(chk_3fields != null && requestBody.isActive) { + if (chk_3fields != null && requestBody.isActive) { chk_3fields.isActive = false; chk_3fields.lastUpdateUserId = request.user.sub; chk_3fields.lastUpdateFullName = request.user.name; @@ -163,16 +161,16 @@ export class Salary extends Controller { } const chk_posTypeId = await this.poTypeRepository.findOne({ - where: { id: requestBody.posTypeId } - }) - if(!chk_posTypeId){ + where: { id: requestBody.posTypeId }, + }); + if (!chk_posTypeId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทของตำแหน่ง ไม่ถูกต้อง"); } const chk_posLevelId = await this.posLevelRepository.findOne({ - where: { id: requestBody.posLevelId } + where: { id: requestBody.posLevelId }, }); - if(!chk_posLevelId){ + if (!chk_posLevelId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ระดับของตำแหน่ง ไม่ถูกต้อง"); } @@ -201,10 +199,13 @@ export class Salary extends Controller { where: { id: id }, }); if (!chk_Salary) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: "+ id); + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); } if (chk_Salary.isActive) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบไอดี: " + id + " ได้ เนื่องสถานะการใช้งานที่เป็น Default"); + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่สามารถลบไอดี: " + id + " ได้ เนื่องสถานะการใช้งานที่เป็น Default", + ); } try { @@ -215,6 +216,74 @@ export class Salary extends Controller { } } + /** + * 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; + } + } } - -