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) {
let _workflow = await new permission().Workflow(request, id, "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) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้");
}
const posMasters = await this.posMasterRepository.find({
where: {
ancestorDNA:
posMaster.ancestorDNA == null || posMaster.ancestorDNA == ""
? "123"
: posMaster.ancestorDNA,
},
order: { lastUpdatedAt: "DESC" },
relations: [
"orgRoot",
"orgChild1",
"orgChild2",
"orgChild3",
"orgChild4",
"current_holder",
"positions",
"positions.posLevel",
"positions.posType",
"positions.posExecutive",
],
});
// สร้าง conditions
const ancestorDNA = posMaster.ancestorDNA || "123";
let historyQuery = queryBuilder.where("pm.ancestorDNA = :ancestorDNA", { ancestorDNA });
// เพิ่มเงื่อนไข draft
if (
posMaster.orgRevision?.orgRevisionIsCurrent != false &&
posMaster.orgRevision?.orgRevisionIsDraft != true
) {
historyQuery = historyQuery.andWhere("orgRevision.orgRevisionIsDraft != :isDraft", {
isDraft: true,
});
}
const posMasters = await historyQuery.orderBy("pm.lastUpdatedAt", "DESC").getMany();
const _data = posMasters.map((item) => ({
id: item.id,
orgShortName:
item.orgRoot == null
? null
: item.orgChild1 == null
? item.orgRoot.orgRootShortName
: item.orgChild2 == null
? item.orgChild1.orgChild1ShortName
: item.orgChild3 == null
? item.orgChild2.orgChild2ShortName
: item.orgChild4 == null
? item.orgChild3.orgChild3ShortName
: item.orgChild4.orgChild4ShortName,
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,
item.orgChild4?.orgChild4ShortName ||
item.orgChild3?.orgChild3ShortName ||
item.orgChild2?.orgChild2ShortName ||
item.orgChild1?.orgChild1ShortName ||
item.orgRoot?.orgRootShortName ||
null,
lastUpdatedAt: item.lastUpdatedAt,
posMasterNoPrefix: item.posMasterNoPrefix,
posMasterNo: item.posMasterNo,
posMasterNoSuffix: item.posMasterNoSuffix,
reason: item.reason,
position: item.positions.map((x) => x.positionName).join("/"),
posExecutive: item.positions
.filter((x) => x.posExecutive != null)
.map((x) => x.posExecutive?.posExecutiveName ?? null)
.filter((x) => x.posExecutive)
.map((x) => x.posExecutive!.posExecutiveName)
.join("/"),
posLevel: item.positions.map((x) => x.posLevel.posLevelName).join("/"),
posType: item.positions.map((x) => x.posType.posTypeName).join("/"),
fullname:
(item?.current_holder?.prefix ?? "") +
"" +
(item?.current_holder?.firstName ?? "") +
" " +
(item?.current_holder?.lastName ?? ""),
`${item.current_holder?.prefix || ""}${item.current_holder?.firstName || ""} ${item.current_holder?.lastName || ""}`.trim(),
}));
return new HttpSuccess(_data);
}
/**
* API
*