API เรียงลำดับทะเบียนประวัติและเงินเดือนที่กำลังแก้ไขตามวันที่คำสั่งมีผล #2509
This commit is contained in:
parent
4c9ed3d317
commit
ce114cf769
1 changed files with 52 additions and 0 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue