From bf8f0c207d5b1bcc63cb7c6170ff87f1f754b5a0 Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 15 Feb 2024 17:35:10 +0700 Subject: [PATCH] =?UTF-8?q?API=20=E0=B9=81=E0=B8=81=E0=B9=89=E0=B9=84?= =?UTF-8?q?=E0=B8=82=E0=B8=9C=E0=B8=B1=E0=B8=87=E0=B9=80=E0=B8=87=E0=B8=B4?= =?UTF-8?q?=E0=B8=99=E0=B9=80=E0=B8=94=E0=B8=B7=E0=B8=AD=E0=B8=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/SalaryController.ts | 88 ++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/src/controllers/SalaryController.ts b/src/controllers/SalaryController.ts index 0a81924..26c4001 100644 --- a/src/controllers/SalaryController.ts +++ b/src/controllers/SalaryController.ts @@ -50,7 +50,7 @@ export class Salary extends Controller { detail: "string", //คำอธิบาย } ) - async create( + async create_salary( @Body() requestBody: CreateSalary, @Request() request: { user: Record }, ){ @@ -102,6 +102,92 @@ export class Salary extends Controller { } } + /** + * API แก้ไขผังเงินเดือน + * + * @summary SLR_002 - แก้ไขผังเงินเดือน #2 + * + * @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", //คำอธิบาย + }, + ) + 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; + } + } + }