1733-ประวัติตำแหน่ง ของโครงสร้างปัจจุบันต้องไม่แสดงของแบบร่าง

This commit is contained in:
mamoss 2025-08-09 20:31:07 +07:00
parent fadd347065
commit 56525e1a0a

View file

@ -2582,69 +2582,72 @@ export class PositionController extends Controller {
async getHistoryPosMater(@Path() id: string, @Request() request: RequestWithUser) { async getHistoryPosMater(@Path() id: string, @Request() request: RequestWithUser) {
let _workflow = await new permission().Workflow(request, id, "SYS_ORG"); let _workflow = await new permission().Workflow(request, id, "SYS_ORG");
if (_workflow == false) await new permission().PermissionGet(request, "SYS_ORG"); if (_workflow == false) await new permission().PermissionGet(request, "SYS_ORG");
const posMaster = await this.posMasterRepository.findOne({
where: { id }, // ใช้ query builder สำหรับประสิทธิภาพที่ดีขึ้น
}); const queryBuilder = this.posMasterRepository
.createQueryBuilder("pm")
.leftJoinAndSelect("pm.orgRevision", "orgRevision")
.leftJoinAndSelect("pm.orgRoot", "orgRoot")
.leftJoinAndSelect("pm.orgChild1", "orgChild1")
.leftJoinAndSelect("pm.orgChild2", "orgChild2")
.leftJoinAndSelect("pm.orgChild3", "orgChild3")
.leftJoinAndSelect("pm.orgChild4", "orgChild4")
.leftJoinAndSelect("pm.current_holder", "current_holder")
.leftJoinAndSelect("pm.positions", "positions")
.leftJoinAndSelect("positions.posLevel", "posLevel")
.leftJoinAndSelect("positions.posType", "posType")
.leftJoinAndSelect("positions.posExecutive", "posExecutive");
// หาข้อมูลหลัก
const posMaster = await queryBuilder.where("pm.id = :id", { id }).getOne();
if (!posMaster) { if (!posMaster) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้");
} }
const posMasters = await this.posMasterRepository.find({
where: { // สร้าง conditions
ancestorDNA: const ancestorDNA = posMaster.ancestorDNA || "123";
posMaster.ancestorDNA == null || posMaster.ancestorDNA == "" let historyQuery = queryBuilder.where("pm.ancestorDNA = :ancestorDNA", { ancestorDNA });
? "123"
: posMaster.ancestorDNA, // เพิ่มเงื่อนไข draft
}, if (
order: { lastUpdatedAt: "DESC" }, posMaster.orgRevision?.orgRevisionIsCurrent != false &&
relations: [ posMaster.orgRevision?.orgRevisionIsDraft != true
"orgRoot", ) {
"orgChild1", historyQuery = historyQuery.andWhere("orgRevision.orgRevisionIsDraft != :isDraft", {
"orgChild2", isDraft: true,
"orgChild3", });
"orgChild4", }
"current_holder",
"positions", const posMasters = await historyQuery.orderBy("pm.lastUpdatedAt", "DESC").getMany();
"positions.posLevel",
"positions.posType",
"positions.posExecutive",
],
});
const _data = posMasters.map((item) => ({ const _data = posMasters.map((item) => ({
id: item.id, id: item.id,
orgShortName: orgShortName:
item.orgRoot == null item.orgChild4?.orgChild4ShortName ||
? null item.orgChild3?.orgChild3ShortName ||
: item.orgChild1 == null item.orgChild2?.orgChild2ShortName ||
? item.orgRoot.orgRootShortName item.orgChild1?.orgChild1ShortName ||
: item.orgChild2 == null item.orgRoot?.orgRootShortName ||
? item.orgChild1.orgChild1ShortName null,
: item.orgChild3 == null lastUpdatedAt: item.lastUpdatedAt,
? item.orgChild2.orgChild2ShortName posMasterNoPrefix: item.posMasterNoPrefix,
: item.orgChild4 == null posMasterNo: item.posMasterNo,
? item.orgChild3.orgChild3ShortName posMasterNoSuffix: item.posMasterNoSuffix,
: item.orgChild4.orgChild4ShortName, reason: item.reason,
lastUpdatedAt: item.lastUpdatedAt ? item.lastUpdatedAt : null,
posMasterNoPrefix: item.posMasterNoPrefix ? item.posMasterNoPrefix : null,
posMasterNo: item.posMasterNo ? item.posMasterNo : null,
posMasterNoSuffix: item.posMasterNoSuffix ? item.posMasterNoSuffix : null,
reason: item.reason ? item.reason : null,
position: item.positions.map((x) => x.positionName).join("/"), position: item.positions.map((x) => x.positionName).join("/"),
posExecutive: item.positions posExecutive: item.positions
.filter((x) => x.posExecutive != null) .filter((x) => x.posExecutive)
.map((x) => x.posExecutive?.posExecutiveName ?? null) .map((x) => x.posExecutive!.posExecutiveName)
.join("/"), .join("/"),
posLevel: item.positions.map((x) => x.posLevel.posLevelName).join("/"), posLevel: item.positions.map((x) => x.posLevel.posLevelName).join("/"),
posType: item.positions.map((x) => x.posType.posTypeName).join("/"), posType: item.positions.map((x) => x.posType.posTypeName).join("/"),
fullname: fullname:
(item?.current_holder?.prefix ?? "") + `${item.current_holder?.prefix || ""}${item.current_holder?.firstName || ""} ${item.current_holder?.lastName || ""}`.trim(),
"" +
(item?.current_holder?.firstName ?? "") +
" " +
(item?.current_holder?.lastName ?? ""),
})); }));
return new HttpSuccess(_data); return new HttpSuccess(_data);
} }
/** /**
* API * API
* *