From a830633c7a6ffff6daa111b0e4f6f3393379ac8e Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Fri, 12 Jun 2026 09:05:11 +0700 Subject: [PATCH] fix script move --- src/controllers/OrganizationController.ts | 20 ++++++++++---- src/services/PositionService.ts | 32 ++++++++++++++--------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index b324a1c0..7edc5998 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -8985,7 +8985,16 @@ export class OrganizationController extends Controller { // Fetch draft PosMasters with relations for history tracking const draftPosMasters = await queryRunner.manager.find(PosMaster, { where: { id: In(draftPosMasterIds) }, - relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4", "next_holder"], + relations: [ + "orgRoot", + "orgChild1", + "orgChild2", + "orgChild3", + "orgChild4", + "next_holder", + "next_holder.posType", + "next_holder.posLevel", + ], }); // Fetch ALL positions for ALL posMasters in just 2 queries @@ -9150,10 +9159,11 @@ export class OrganizationController extends Controller { prefix: draftPosMaster.next_holder?.prefix ?? null, firstName: draftPosMaster.next_holder?.firstName ?? null, lastName: draftPosMaster.next_holder?.lastName ?? null, - position: selectedPos.positionName ?? null, - posType: (selectedPos as any).posType?.posTypeName ?? null, - posLevel: (selectedPos as any).posLevel?.posLevelName ?? null, - posExecutive: (selectedPos as any).posExecutive?.posExecutiveName ?? null, + // isSit = true ดึงค่าจาก profile + position: draftPosMaster?.isSit ? draftPosMaster.next_holder?.position ?? null : selectedPos.positionName ?? null, + posType: draftPosMaster?.isSit ? draftPosMaster.next_holder?.posType?.posTypeName ?? null : (selectedPos as any).posType?.posTypeName ?? null, + posLevel: draftPosMaster?.isSit ? draftPosMaster.next_holder?.posLevel?.posLevelName ?? null : (selectedPos as any).posLevel?.posLevelName ?? null, + posExecutive: draftPosMaster?.isSit ? draftPosMaster.next_holder?.posExecutive ?? null : (selectedPos as any).posExecutive?.posExecutiveName ?? null, profileId: nextHolderId, rootDnaId: draftPosMaster.orgRoot?.ancestorDNA ?? null, child1DnaId: draftPosMaster.orgChild1?.ancestorDNA ?? null, diff --git a/src/services/PositionService.ts b/src/services/PositionService.ts index 2c454a39..45b3c1b0 100644 --- a/src/services/PositionService.ts +++ b/src/services/PositionService.ts @@ -19,9 +19,7 @@ import { Profile } from "../entities/Profile"; * - ถ้า posType = "อำนวยการ" หรือ "บริหาร" ใช้ posExecutiveName * - ถ้า posType อื่นๆ ใช้ positionName + posLevel */ -export async function getPosMasterPositions( - posMasterIds: string[] -): Promise> { +export async function getPosMasterPositions(posMasterIds: string[]): Promise> { if (posMasterIds.length === 0) { return new Map(); } @@ -62,7 +60,9 @@ export async function getPosMasterPositions( let positionText = ""; if (posTypeName === "อำนวยการ" || posTypeName === "บริหาร") { - positionText = pos.posExecutive?.posExecutiveName || `${pos.positionName || ""}ระดับ${pos.posLevel?.posLevelName || ""}`.trim(); + positionText = + pos.posExecutive?.posExecutiveName || + `${pos.positionName || ""}ระดับ${pos.posLevel?.posLevelName || ""}`.trim(); } else { positionText = `${pos.positionName || ""}${pos.posLevel?.posLevelName || ""}`.trim(); } @@ -73,7 +73,6 @@ export async function getPosMasterPositions( return positionMap; } - export async function CreatePosMasterHistoryOfficer( posMasterId: string, request: RequestWithUser | null, @@ -141,7 +140,7 @@ export async function CreatePosMasterHistoryOfficer( if (pm.isSit && pm.current_holderId) { const profile = await repoProfile.findOne({ where: { id: pm.current_holderId }, - relations: ["posType", "posLevel"] + relations: ["posType", "posLevel"], }); position = profile?.position ?? _null; posTypeName = profile?.posType?.posTypeName ?? _null; @@ -476,8 +475,15 @@ export async function BatchSavePosMasterHistoryOfficer( const existing = historyByDna.get(op.posMasterDnaId)?.[0]; const shouldInsert = !existing && op.profileId && op.pm; const profileChanged = existing && existing.profileId !== op.profileId; + const positionChanged = + existing && + existing.position !== op.pm?.position && + existing.posType !== op.pm?.posType && + existing.posLevel !== op.pm?.posLevel && + existing.posExecutive !== op.pm?.posExecutive; - if (shouldInsert || profileChanged) { + // ถ้าไม่มี record เดิม หรือ profile เปลี่ยน หรือ position เปลี่ยน ให้สร้าง record ใหม่ + if (shouldInsert || profileChanged || positionChanged) { const newPmh = new PosMasterHistory(); newPmh.ancestorDNA = op.posMasterDnaId; newPmh.prefix = op.pm?.prefix ?? _null; @@ -542,11 +548,11 @@ export async function updateHolderProfileHistory( orgRevision: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false, - } + }, }, relations: { - orgRevision : true - } + orgRevision: true, + }, }); if (posMaster) { @@ -560,11 +566,11 @@ export async function updateHolderProfileHistory( orgRevision: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false, - } + }, }, relations: { - orgRevision : true - } + orgRevision: true, + }, }); if (employeePosMaster) {