From 7d463806a9bb84a2f49202af32e1404dceed34a5 Mon Sep 17 00:00:00 2001 From: harid Date: Wed, 27 May 2026 16:41:56 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=9B=E0=B8=B1=E0=B9=8A=E0=B8=A1=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=B8=84?= =?UTF-8?q?=E0=B8=99=E0=B8=84=E0=B8=A3=E0=B8=AD=E0=B8=87=E0=B8=81=E0=B8=A3?= =?UTF-8?q?=E0=B8=93=E0=B8=B5=E0=B8=A1=E0=B8=B5=E0=B9=81=E0=B8=81=E0=B9=89?= =?UTF-8?q?=E0=B9=84=E0=B8=82=E0=B8=8A=E0=B8=B7=E0=B9=88=E0=B8=AD=E0=B9=81?= =?UTF-8?q?=E0=B8=A5=E0=B8=B0=E0=B8=99=E0=B8=B2=E0=B8=A1=E0=B8=AA=E0=B8=81?= =?UTF-8?q?=E0=B8=B8=E0=B8=A5=20=20#244?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProfileChangeNameController.ts | 4 ++ .../ProfileChangeNameEmployeeController.ts | 4 ++ src/services/PositionService.ts | 58 +++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/src/controllers/ProfileChangeNameController.ts b/src/controllers/ProfileChangeNameController.ts index 77cff634..fa88a252 100644 --- a/src/controllers/ProfileChangeNameController.ts +++ b/src/controllers/ProfileChangeNameController.ts @@ -25,6 +25,7 @@ import { } from "../entities/ProfileChangeName"; import { updateName } from "../keycloak"; import permission from "../interfaces/permission"; +import { updateHolderProfileHistory } from "../services/PositionService"; import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/changeName") @Tags("ProfileChangeName") @@ -127,6 +128,9 @@ export class ProfileChangeNameController extends Controller { } } + // บันทึกประวัติคนครองตำแหน่ง (ถ้า profile นี้ครองตำแหน่งอยู่) + await updateHolderProfileHistory(profile.id, req); + return new HttpSuccess(data.id); } diff --git a/src/controllers/ProfileChangeNameEmployeeController.ts b/src/controllers/ProfileChangeNameEmployeeController.ts index c2df9c8c..0a6f2ff0 100644 --- a/src/controllers/ProfileChangeNameEmployeeController.ts +++ b/src/controllers/ProfileChangeNameEmployeeController.ts @@ -24,6 +24,7 @@ import { } from "../entities/ProfileChangeName"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { updateHolderProfileHistory } from "../services/PositionService"; import { updateName } from "../keycloak"; import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/changeName") @@ -133,6 +134,9 @@ export class ProfileChangeNameEmployeeController extends Controller { } } + // บันทึกประวัติคนครองตำแหน่ง (ถ้า profile นี้ครองตำแหน่งอยู่) + await updateHolderProfileHistory(profile.id, req, "EMPLOYEE"); + return new HttpSuccess(data.id); } diff --git a/src/services/PositionService.ts b/src/services/PositionService.ts index b6514eca..37c5f083 100644 --- a/src/services/PositionService.ts +++ b/src/services/PositionService.ts @@ -501,3 +501,61 @@ export async function BatchSavePosMasterHistoryOfficer( return false; } } + +/** + * อัพเดทประวัติคนครองตำแหน่งเมื่อมีการเปลี่ยนแปลงข้อมูล profile + * เช่น เปลี่ยนชื่อ - นามสกุล + * ใช้สำหรับบันทึกประวัติเมื่อ profile ที่ครองตำแหน่งมีการเปลี่ยนแปลง + * + * @param profileId ID ของ profile ที่ต้องการตรวจสอบ + * @param request RequestWithUser สำหรับบันทึกข้อมูลผู้ดำเนินการ + * @param type "OFFICER" สำหรับข้าราชการ | "EMPLOYEE" สำหรับลูกจ้างประจำ (default: "OFFICER") + */ +export async function updateHolderProfileHistory( + profileId: string, + request: RequestWithUser, + type: "OFFICER" | "EMPLOYEE" = "OFFICER", +): Promise { + try { + if (type === "OFFICER") { + const posMasterRepo = AppDataSource.getRepository(PosMaster); + const posMaster = await posMasterRepo.findOne({ + where: { + current_holderId: profileId, + orgRevision: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false, + } + }, + relations: { + orgRevision : true + } + }); + + if (posMaster) { + await CreatePosMasterHistoryOfficer(posMaster.id, request); + } + } else if (type === "EMPLOYEE") { + const empPosMasterRepo = AppDataSource.getRepository(EmployeePosMaster); + const employeePosMaster = await empPosMasterRepo.findOne({ + where: { + current_holderId: profileId, + orgRevision: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false, + } + }, + relations: { + orgRevision : true + } + }); + + if (employeePosMaster) { + await CreatePosMasterHistoryEmployee(employeePosMaster.id, request); + } + } + } catch (error) { + console.error("updateHolderProfileHistory error:", error); + throw error; + } +}