From 56525e1a0a51b4ae60add2ef37526d19c52b0811 Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Sat, 9 Aug 2025 20:31:07 +0700 Subject: [PATCH] =?UTF-8?q?1733-=E0=B8=9B=E0=B8=A3=E0=B8=B0=E0=B8=A7?= =?UTF-8?q?=E0=B8=B1=E0=B8=95=E0=B8=B4=E0=B8=95=E0=B8=B3=E0=B9=81=E0=B8=AB?= =?UTF-8?q?=E0=B8=99=E0=B9=88=E0=B8=87=20=E0=B8=82=E0=B8=AD=E0=B8=87?= =?UTF-8?q?=E0=B9=82=E0=B8=84=E0=B8=A3=E0=B8=87=E0=B8=AA=E0=B8=A3=E0=B9=89?= =?UTF-8?q?=E0=B8=B2=E0=B8=87=E0=B8=9B=E0=B8=B1=E0=B8=88=E0=B8=88=E0=B8=B8?= =?UTF-8?q?=E0=B8=9A=E0=B8=B1=E0=B8=99=E0=B8=95=E0=B9=89=E0=B8=AD=E0=B8=87?= =?UTF-8?q?=E0=B9=84=E0=B8=A1=E0=B9=88=E0=B9=81=E0=B8=AA=E0=B8=94=E0=B8=87?= =?UTF-8?q?=E0=B8=82=E0=B8=AD=E0=B8=87=E0=B9=81=E0=B8=9A=E0=B8=9A=E0=B8=A3?= =?UTF-8?q?=E0=B9=88=E0=B8=B2=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/PositionController.ts | 99 ++++++++++++++------------- 1 file changed, 51 insertions(+), 48 deletions(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 04c29c98..5b61583e 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -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 ย้ายอัตรากำลัง *