From cf3ef00b7f61385fe0823c436b01fd860666903c Mon Sep 17 00:00:00 2001 From: Adisak Date: Mon, 11 May 2026 13:01:48 +0700 Subject: [PATCH 1/2] #2473 --- src/controllers/CommandController.ts | 27 +++++++++++++++++--- src/controllers/ProfileEmployeeController.ts | 5 +++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index f7e68083..97e39cbf 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -4543,9 +4543,7 @@ export class CommandController extends Controller { profile.lastUpdateUserId = req.user.sub; profile.lastUpdateFullName = req.user.name; profile.lastUpdatedAt = new Date(); - if (item.isLeave == true) { - await removeProfileInOrganize(profile.id, "EMPLOYEE"); - } + // บันทึกประวัติก่อนลบตำแหน่ง const clearProfile = await checkCommandType(String(item.commandId)); const curRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, @@ -4574,6 +4572,14 @@ export class CommandController extends Controller { orgChild2Ref = curPosMaster?.orgChild2 ?? null; orgChild3Ref = curPosMaster?.orgChild3 ?? null; orgChild4Ref = curPosMaster?.orgChild4 ?? null; + if (curPosMaster) { + await CreatePosMasterHistoryEmployee(curPosMaster.id, req); + } + } + + // ลบตำแหน่ง + if (item.isLeave == true) { + await removeProfileInOrganize(profile.id, "EMPLOYEE"); } if (clearProfile.status) { @@ -5817,6 +5823,21 @@ export class CommandController extends Controller { _profile.leaveDate = item.commandDateAffect ?? _null; _profile.leaveType = exceptClear.LeaveType ?? _null; } else { + // บันทึกประวัติก่อนลบตำแหน่ง + const curRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + }); + if (curRevision) { + const curPosMaster = await this.employeePosMasterRepository.findOne({ + where: { + current_holderId: _profile.id, + orgRevisionId: curRevision.id, + }, + }); + if (curPosMaster) { + await CreatePosMasterHistoryEmployee(curPosMaster.id, req); + } + } await removeProfileInOrganize(_profile.id, "EMPLOYEE"); } } diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 8ae134c1..cfae2d53 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -81,7 +81,7 @@ import { ProfileAssistance } from "../entities/ProfileAssistance"; import { ProfileChangeName } from "../entities/ProfileChangeName"; import { ProfileChildren } from "../entities/ProfileChildren"; import { ProfileDuty } from "../entities/ProfileDuty"; -import { getTopDegrees } from "../services/PositionService"; +import { CreatePosMasterHistoryEmployee, getTopDegrees } from "../services/PositionService"; import { ProfileLeaveService } from "../services/ProfileLeaveService"; import { PostRetireToExprofile } from "./ExRetirementController"; import { CommandCode } from "../entities/CommandCode"; @@ -5778,6 +5778,9 @@ export class ProfileEmployeeController extends Controller { } await this.profileRepo.save(profile); if (requestBody.isLeave == true) { + if (orgRevisionRef) { + await CreatePosMasterHistoryEmployee(orgRevisionRef.id, request); + } await removeProfileInOrganize(profile.id, "EMPLOYEE"); } let organizeName = ""; From 7a6cf119bd8590eefd6d5384a4af669c98f9b8bd Mon Sep 17 00:00:00 2001 From: Adisak Date: Mon, 11 May 2026 13:25:26 +0700 Subject: [PATCH 2/2] update 2473 --- src/controllers/CommandController.ts | 4 ++-- src/controllers/ProfileEmployeeController.ts | 2 +- src/services/PositionService.ts | 15 +++++++++------ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 69262992..ca82faa3 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -4565,7 +4565,7 @@ export class CommandController extends Controller { orgChild3Ref = curPosMaster?.orgChild3 ?? null; orgChild4Ref = curPosMaster?.orgChild4 ?? null; if (curPosMaster) { - await CreatePosMasterHistoryEmployee(curPosMaster.id, req); + await CreatePosMasterHistoryEmployee(curPosMaster.id, req, "DELETE"); } } @@ -5792,7 +5792,7 @@ export class CommandController extends Controller { }, }); if (curPosMaster) { - await CreatePosMasterHistoryEmployee(curPosMaster.id, req); + await CreatePosMasterHistoryEmployee(curPosMaster.id, req, "DELETE"); } } await removeProfileInOrganize(_profile.id, "EMPLOYEE"); diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index a57fd209..bbf8d14a 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -5784,7 +5784,7 @@ export class ProfileEmployeeController extends Controller { await this.profileRepo.save(profile); if (requestBody.isLeave == true) { if (orgRevisionRef) { - await CreatePosMasterHistoryEmployee(orgRevisionRef.id, request); + await CreatePosMasterHistoryEmployee(orgRevisionRef.id, request, "DELETE"); } await removeProfileInOrganize(profile.id, "EMPLOYEE"); } diff --git a/src/services/PositionService.ts b/src/services/PositionService.ts index 357ec2af..7b104e15 100644 --- a/src/services/PositionService.ts +++ b/src/services/PositionService.ts @@ -137,6 +137,7 @@ export async function CreatePosMasterHistoryOfficer( export async function CreatePosMasterHistoryEmployee( posMasterId: string, request: RequestWithUser | null, + type?: string | null, ): Promise { try { await AppDataSource.transaction(async (manager) => { @@ -167,15 +168,17 @@ export async function CreatePosMasterHistoryEmployee( ? pm.positions.find((p) => p.positionIsSelected === true) ?? null : null; h.ancestorDNA = pm.ancestorDNA; - h.prefix = pm.current_holder?.prefix || _null; - h.firstName = pm.current_holder?.firstName || _null; - h.lastName = pm.current_holder?.lastName || _null; + if (!type || type != "DELETE") { + h.prefix = pm.current_holder?.prefix || _null; + h.firstName = pm.current_holder?.firstName || _null; + h.lastName = pm.current_holder?.lastName || _null; + h.position = selectedPosition?.positionName ?? _null; + h.posType = selectedPosition?.posType?.posTypeName ?? _null; + h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null; + } h.posMasterNoPrefix = pm.posMasterNoPrefix ?? _null; h.posMasterNo = pm.posMasterNo ?? _null; h.posMasterNoSuffix = pm.posMasterNoSuffix ?? _null; - h.position = selectedPosition?.positionName ?? _null; - h.posType = selectedPosition?.posType?.posTypeName ?? _null; - h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null; h.shortName = [ pm.orgChild4?.orgChild4ShortName,