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

@ -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,