From 6312b940a3cd98eb4655d9161e22411074f6ff0d Mon Sep 17 00:00:00 2001 From: harid Date: Wed, 14 Jan 2026 17:42:33 +0700 Subject: [PATCH] =?UTF-8?q?API=20=E0=B8=84=E0=B9=89=E0=B8=99=E0=B8=AB?= =?UTF-8?q?=E0=B8=B2=20=E0=B8=81=E0=B8=88.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OrganizationDotnetController.ts | 89 ++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index 75dd9c55..2a4e0063 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -36,6 +36,8 @@ import { calculateRetireLaw } from "../interfaces/utils"; import Extension from "../interfaces/extension"; import { PosMasterHistory } from "../entities/PosMasterHistory"; import { PosMasterEmployeeHistory } from "../entities/PosMasterEmployeeHistory"; +import { PosMasterAssign } from "../entities/PosMasterAssign"; +import { Assign } from "../entities/Assign"; @Route("api/v1/org/dotnet") @Tags("Dotnet") @Security("bearerAuth") @@ -61,7 +63,8 @@ export class OrganizationDotnetController extends Controller { private empPosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); private insigniaRepo = AppDataSource.getRepository(ProfileInsignia); private employeePosDictRepository = AppDataSource.getRepository(EmployeePosDict); - + private posMasterAssignRepo = AppDataSource.getRepository(PosMasterAssign); + private assignRepository = AppDataSource.getRepository(Assign); /** * ทำไว้ให้ service อื่นๆ ภายในระบบ call มาตรวจสอบเลขบัตรประจำตัวประชาชน * @@ -7043,4 +7046,88 @@ export class OrganizationDotnetController extends Controller { return new HttpSuccess(profile_); } + + /** + * API ค้นหา กจ. + * + * @summary API ค้นหา กจ. + * + */ + @Post("find-staff") + async findHigher( + @Body() + requestBody: { + profileId: string; + assignId: string; + }, + @Request() request: RequestWithUser, + ) { + const profile = await this.profileRepo.findOne({ + where: { id: requestBody.profileId }, + relations: [ + "current_holders", + "current_holders.orgRevision" + ], + }); + + if (!profile) + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์"); + + const assign = await this.assignRepository.findOne({ + where: { id: requestBody.assignId.trim().toLocaleUpperCase() }, + }); + + if (!assign) + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลระบบสิทธิ์หน้าที่ความรับผิดชอบ"); + + const currentHolder = profile.current_holders?.find( + h => h.orgRevision?.orgRevisionIsCurrent && !h.orgRevision?.orgRevisionIsDraft, + ); + + if (!currentHolder) + throw new HttpError(HttpStatus.NOT_FOUND, "โปรไฟล์นี้ไม่ได้ครองตำแหน่งในปัจจุบัน"); + + const posMasters = await this.posMasterRepository + .createQueryBuilder("pm") + .select([ + "pm.current_holderId AS profileId", + "profile.keycloak AS keycloak", + "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) AS fullName", + "pm.orgRootId AS rootId", + "pm.orgChild1Id AS orgChild1Id", + "pm.orgChild2Id AS orgChild2Id", + "pm.orgChild3Id AS orgChild3Id", + "pm.orgChild4Id AS orgChild4Id", + "orgRoot.ancestorDNA AS rootDnaId", + "orgChild1.ancestorDNA AS child1DnaId", + "orgChild2.ancestorDNA AS child2DnaId", + "orgChild3.ancestorDNA AS child3DnaId", + "orgChild4.ancestorDNA AS child4DnaId", + ]) + .distinct(true) + // ต้องมี posMasterAssign + .innerJoin("posMasterAssign", "assign", "assign.posMasterId = pm.id") + // join เพื่อเอา ancestorDNA + .leftJoin("pm.orgRoot", "orgRoot") + .leftJoin("pm.orgChild1", "orgChild1") + .leftJoin("pm.orgChild2", "orgChild2") + .leftJoin("pm.orgChild3", "orgChild3") + .leftJoin("pm.orgChild4", "orgChild4") + .leftJoin("pm.current_holder", "profile") + .where("pm.orgRevisionId = :orgRevisionId", { + orgRevisionId: currentHolder.orgRevisionId, + }) + .andWhere("pm.orgRootId = :orgRootId", { + orgRootId: currentHolder.orgRootId, + }) + .andWhere("pm.current_holderId != :profileId", { + profileId: profile.id, + }) + .andWhere("assign.assignId = :assignId", { + assignId: assign.id + }) + .getRawMany(); + + return new HttpSuccess(posMasters); + } }