fix แสดงกรณีรักษาการแทนผิด #2472
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m0s

This commit is contained in:
harid 2026-05-12 11:51:57 +07:00
parent 760fef5c2f
commit 60191a23d7
3 changed files with 144 additions and 8 deletions

View file

@ -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")

View file

@ -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<string, string> = 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 });
}

View file

@ -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<Map<string, string>> {
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<string, string>();
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,