diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index e5afb054..3c514b6b 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -89,7 +89,7 @@ import { ProfileAssistance } from "../entities/ProfileAssistance"; import { CommandRecive } from "../entities/CommandRecive"; import { CommandCode } from "../entities/CommandCode"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; -import { CreatePosMasterHistoryOfficer, getTopDegrees } from "../services/PositionService"; +import { CreatePosMasterHistoryOfficer, getTopDegrees, getPosMasterPositions } from "../services/PositionService"; import { ProfileLeaveService } from "../services/ProfileLeaveService"; // import { PostRetireToExprofile } from "./ExRetirementController"; import { getPosNumCodeSit } from "../services/CommandService"; @@ -3433,7 +3433,44 @@ export class ProfileController extends Controller { .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); - return new HttpSuccess({ data: lists, total }); + + // ดึง posMasterId ทั้งหมด (36 ตัวแรกของ key) เพื่อ query positionName + const posMasterIds = lists + .map((x) => x.key?.substring(0, 36)) + .filter((id) => id && id.length === 36); + const posMasterPositionMap = await getPosMasterPositions(posMasterIds); + + // ปรับ positionSign สำหรับกรณีรักษาการ + const processedLists = lists.map((x: any) => { + let newPositionSign = x.positionSign; + + // ตำแหน่งของคนที่เลือกไปรักษาการ + let childPosition = ""; + if (x.posType === "อำนวยการ" || x.posType === "บริหาร") { + childPosition = x.posExecutiveName || ""; + if (!childPosition) { + childPosition = `${x.position || ""}ระดับ${x.posLevel || ""}`.trim(); + } + } else { + childPosition = `${x.position || ""}${x.posLevel || ""}`.trim(); + } + + // ตำแหน่งที่รักษาการแทน + const posMasterId = x.key?.substring(0, 36); + const targetPosition = x.positionSign + ? x.positionSign + : posMasterPositionMap.get(posMasterId) || ""; + + // สร้าง positionSign ใหม่ + newPositionSign = `${childPosition} รักษาการในตำแหน่ง${targetPosition}`; + + return { + ...x, + positionSign: newPositionSign, + }; + }); + + return new HttpSuccess({ data: processedLists, total }); } else { const [lists, total] = await AppDataSource.getRepository(viewDirector) .createQueryBuilder("viewDirector") diff --git a/src/controllers/WorkflowController.ts b/src/controllers/WorkflowController.ts index 23091552..cb06f1b7 100644 --- a/src/controllers/WorkflowController.ts +++ b/src/controllers/WorkflowController.ts @@ -23,6 +23,7 @@ import { viewDirector } from "../entities/view/viewDirector"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { OrgRoot } from "../entities/OrgRoot"; +import { getPosMasterPositions } from "../services/PositionService"; @Route("api/v1/org/workflow") @Tags("Workflow") @Security("bearerAuth") @@ -1071,12 +1072,49 @@ export class WorkflowController extends Controller { ]); // 8. ปรับ response mapping (ถ้าจำเป็น) - const processedData = data.map((x: any) => ({ - ...x, - posExecutiveNameOrg: - (x.posExecutiveName ?? "") + - (x.orgChild4 ?? x.orgChild3 ?? x.orgChild2 ?? x.orgChild1 ?? x.orgRoot ?? ""), - })); + let posMasterPositionMap: Map = new Map(); + + if (body.isAct) { + // ดึง posMasterId ทั้งหมด (36 ตัวแรกของ key) เพื่อ query positionName + const posMasterIds = data + .map((x) => x.key?.substring(0, 36)) + .filter((id) => id && id.length === 36); + posMasterPositionMap = await getPosMasterPositions(posMasterIds); + } + + const processedData = data.map((x: any) => { + let newPositionSign = x.positionSign; + + if (body.isAct) { + // ตำแหน่งของคนที่เลือกไปรักษาการ + let childPosition = ""; + if (x.posType === "อำนวยการ" || x.posType === "บริหาร") { + childPosition = x.posExecutiveName || ""; + if (!childPosition) { + childPosition = `${x.position || ""}ระดับ${x.posLevel || ""}`.trim(); + } + } else { + childPosition = `${x.position || ""}${x.posLevel || ""}`.trim(); + } + + // ตำแหน่งที่รักษาการแทน + const posMasterId = x.key?.substring(0, 36); + const targetPosition = x.positionSign + ? x.positionSign + : posMasterPositionMap.get(posMasterId) || ""; + + // สร้าง positionSign ใหม่ + newPositionSign = `${childPosition} รักษาการในตำแหน่ง${targetPosition}`; + } + + return { + ...x, + positionSign: newPositionSign, + posExecutiveNameOrg: + (x.posExecutiveName ?? "") + + (x.orgChild4 ?? x.orgChild3 ?? x.orgChild2 ?? x.orgChild1 ?? x.orgRoot ?? ""), + }; + }); return new HttpSuccess({ data: processedData, total }); } diff --git a/src/services/PositionService.ts b/src/services/PositionService.ts index 7b104e15..b1837b99 100644 --- a/src/services/PositionService.ts +++ b/src/services/PositionService.ts @@ -12,6 +12,67 @@ import { Position } from "../entities/Position"; import { ProfileEducation } from "../entities/ProfileEducation"; import { RequestWithUser } from "../middlewares/user"; +/** + * function สำหรับดึงตำแหน่งที่รักษาการแทน + * ใช้กรณี positionSign ว่าง + * - ถ้า posType = "อำนวยการ" หรือ "บริหาร" ใช้ posExecutiveName + * - ถ้า posType อื่นๆ ใช้ positionName + posLevel + */ +export async function getPosMasterPositions( + posMasterIds: string[] +): Promise> { + if (posMasterIds.length === 0) { + return new Map(); + } + + const positionRepo = AppDataSource.getRepository(Position); + + // Query รอบที่ 1: หา position ที่มีคนครอง + const positionsWithHolder = await positionRepo.find({ + where: { + posMasterId: In(posMasterIds), + positionIsSelected: true, + }, + relations: ["posType", "posLevel", "posExecutive"], + }); + + // หา posMasterId ที่ยังไม่ได้ผลลัพธ์ + const foundMasterIds = new Set(positionsWithHolder.map((p) => p.posMasterId)); + const missingMasterIds = posMasterIds.filter((id) => !foundMasterIds.has(id)); + + // Query รอบที่ 2: เฉพาะที่ขาด (กรณีไม่มีคนครอง) + let positionsWithoutHolder: Position[] = []; + if (missingMasterIds.length > 0) { + positionsWithoutHolder = await positionRepo.find({ + where: { + posMasterId: In(missingMasterIds), + }, + order: { createdAt: "ASC" }, + relations: ["posType", "posLevel", "posExecutive"], + }); + } + + // รวม positions และสร้าง Map + const allPositions = [...positionsWithHolder, ...positionsWithoutHolder]; + const positionMap = new Map(); + + for (const pos of allPositions) { + const posTypeName = pos.posType?.posTypeName || ""; + let positionText = ""; + + if (posTypeName === "อำนวยการ" || posTypeName === "บริหาร") { + positionText = pos.posExecutive?.posExecutiveName || `${pos.positionName || ""}ระดับ${pos.posLevel?.posLevelName || ""}`.trim(); + } else { + positionText = `${pos.positionName || ""}${pos.posLevel?.posLevelName || ""}`.trim(); + } + + positionMap.set(pos.posMasterId, positionText); + } + + return positionMap; +} + + export async function CreatePosMasterHistoryOfficer( posMasterId: string, request: RequestWithUser | null,