From ce114cf769a18540e469e907fe1782f2df8fdd2e Mon Sep 17 00:00:00 2001 From: harid Date: Fri, 22 May 2026 13:38:21 +0700 Subject: [PATCH] =?UTF-8?q?API=20=E0=B9=80=E0=B8=A3=E0=B8=B5=E0=B8=A2?= =?UTF-8?q?=E0=B8=87=E0=B8=A5=E0=B8=B3=E0=B8=94=E0=B8=B1=E0=B8=9A=E0=B8=97?= =?UTF-8?q?=E0=B8=B0=E0=B9=80=E0=B8=9A=E0=B8=B5=E0=B8=A2=E0=B8=99=E0=B8=9B?= =?UTF-8?q?=E0=B8=A3=E0=B8=B0=E0=B8=A7=E0=B8=B1=E0=B8=95=E0=B8=B4=E0=B9=81?= =?UTF-8?q?=E0=B8=A5=E0=B8=B0=E0=B9=80=E0=B8=87=E0=B8=B4=E0=B8=99=E0=B9=80?= =?UTF-8?q?=E0=B8=94=E0=B8=B7=E0=B8=AD=E0=B8=99=E0=B8=97=E0=B8=B5=E0=B9=88?= =?UTF-8?q?=E0=B8=81=E0=B8=B3=E0=B8=A5=E0=B8=B1=E0=B8=87=E0=B9=81=E0=B8=81?= =?UTF-8?q?=E0=B9=89=E0=B9=84=E0=B8=82=E0=B8=95=E0=B8=B2=E0=B8=A1=E0=B8=A7?= =?UTF-8?q?=E0=B8=B1=E0=B8=99=E0=B8=97=E0=B8=B5=E0=B9=88=E0=B8=84=E0=B8=B3?= =?UTF-8?q?=E0=B8=AA=E0=B8=B1=E0=B9=88=E0=B8=87=E0=B8=A1=E0=B8=B5=E0=B8=9C?= =?UTF-8?q?=E0=B8=A5=20#2509?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProfileSalaryTempController.ts | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/controllers/ProfileSalaryTempController.ts b/src/controllers/ProfileSalaryTempController.ts index 35279fbc..c166d22d 100644 --- a/src/controllers/ProfileSalaryTempController.ts +++ b/src/controllers/ProfileSalaryTempController.ts @@ -1791,4 +1791,56 @@ export class ProfileSalaryTempController extends Controller { await this.salaryRepo.save(sortLevel); return new HttpSuccess(); } + + /** + * API เรียงลำดับทะเบียนประวัติและเงินเดือนที่กำลังแก้ไขตามวันที่คำสั่งมีผล + * @summary API เรียงลำดับทะเบียนประวัติและเงินเดือนที่กำลังแก้ไขตามวันที่คำสั่งมีผล + */ + @Put("sort-order") + public async reorderSalaryByCommandDate( + @Request() req: RequestWithUser, + @Body() body: { profileId: string; type: "OFFICER" | "EMPLOYEE" }, + ) { + const isOfficer = body.type.toUpperCase() === "OFFICER"; + + // Step 1: SELECT ข้อมูลตาม profileId และ type + const salaryTemps = await this.salaryRepo.find({ + where: isOfficer ? { profileId: body.profileId } : { profileEmployeeId: body.profileId }, + }); + + if (salaryTemps.length === 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งเงินเดือน"); + } + + // Step 2: เรียงลำดับตาม commandDateAffect (ASC) + // ถ้า commandDateAffect เท่ากัน ให้ใช้ order เดิมเป็น secondary sort + const sortedSalary = salaryTemps.sort((a, b) => { + // ถ้า commandDateAffect เป็น null ให้ถือว่าเป็นค่าน้อยสุด + const dateA = a.commandDateAffect ? new Date(a.commandDateAffect).getTime() : 0; + const dateB = b.commandDateAffect ? new Date(b.commandDateAffect).getTime() : 0; + + if (dateA !== dateB) { + return dateA - dateB; // เรียงตามวันที่คำสั่งมีผล + } + + // ถ้าวันที่เท่ากัน ให้ใช้ order เดิม + const orderA = a.order ?? 0; + const orderB = b.order ?? 0; + return orderA - orderB; + }); + + // Step 3: UPDATE ฟิลด์ order ตามการเรียงใหม่ + const dateNow = new Date(); + const updatedSalary = sortedSalary.map((item, index) => ({ + ...item, + order: index + 1, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + lastUpdatedAt: dateNow, + })); + + await this.salaryRepo.save(updatedSalary); + + return new HttpSuccess(); + } }