From 97251fdd6ecc860fde7b51d0dcd8299cc3dd334b Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Fri, 1 Aug 2025 17:07:14 +0700 Subject: [PATCH] api find org only --- src/controllers/ProfileController.ts | 156 ++++++++++++++++++++++----- 1 file changed, 131 insertions(+), 25 deletions(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 002cedc2..7645a375 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -85,6 +85,7 @@ import { OrgChild4 } from "../entities/OrgChild4"; import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory"; import { ProfileAssistance } from "../entities/ProfileAssistance"; import { CommandRecive } from "../entities/CommandRecive"; +import { EmployeePosMaster } from "../entities/EmployeePosMaster"; @Route("api/v1/org/profile") @Tags("Profile") @Security("bearerAuth") @@ -101,6 +102,7 @@ export class ProfileController extends Controller { private orgChild3Repo = AppDataSource.getRepository(OrgChild3); private orgChild4Repo = AppDataSource.getRepository(OrgChild4); private posMasterRepo = AppDataSource.getRepository(PosMaster); + private employeePosMasterRepo = AppDataSource.getRepository(EmployeePosMaster); private profileRepo = AppDataSource.getRepository(Profile); private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee); private profileHistoryRepo = AppDataSource.getRepository(ProfileHistory); @@ -2053,6 +2055,109 @@ export class ProfileController extends Controller { return new HttpSuccess({ caregiver, commander, chairman }); } + /** + * API ข้อมูลทะเบียนประวัติตาม profileId + * + * @summary ข้อมูลองค์กรของ Profile (ADMIN) + * + * @param {string} profileId Profile ID + */ + @Get("org-user/{profileId}") + async getOrgUserByProfileId(@Path() profileId: string) { + // 1. หาข้อมูล org hierarchy สำหรับ profile นี้ + const posMaster = await this.posMasterRepo.findOne({ + where: { + current_holderId: profileId, + orgRevision: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false, + }, + }, + select: { + id: true, + orgRoot: { + id: true, + orgRootName: true, + }, + orgChild1: { + id: true, + orgChild1Name: true, + }, + orgChild2: { + id: true, + orgChild2Name: true, + }, + orgChild3: { + id: true, + orgChild3Name: true, + }, + orgChild4: { + id: true, + orgChild4Name: true, + }, + }, + relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], + }); + + // 2. ถ้าไม่พบใน PosMaster ให้ลองหาใน EmployeePosMaster + if (!posMaster) { + const employeePosMaster = await this.employeePosMasterRepo.findOne({ + where: { + current_holderId: profileId, + orgRevision: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false, + }, + }, + select: { + id: true, + orgRoot: { + id: true, + orgRootName: true, + }, + orgChild1: { + id: true, + orgChild1Name: true, + }, + orgChild2: { + id: true, + orgChild2Name: true, + }, + orgChild3: { + id: true, + orgChild3Name: true, + }, + orgChild4: { + id: true, + orgChild4Name: true, + }, + }, + relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], + }); + + if (!employeePosMaster) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งสำหรับ Profile นี้"); + } + + return new HttpSuccess({ + root: employeePosMaster.orgRoot?.orgRootName ?? null, + child1: employeePosMaster.orgChild1?.orgChild1Name ?? null, + child2: employeePosMaster.orgChild2?.orgChild2Name ?? null, + child3: employeePosMaster.orgChild3?.orgChild3Name ?? null, + child4: employeePosMaster.orgChild4?.orgChild4Name ?? null, + }); + } + + // 3. Return ข้อมูล org hierarchy + return new HttpSuccess({ + root: posMaster.orgRoot?.orgRootName ?? null, + child1: posMaster.orgChild1?.orgChild1Name ?? null, + child2: posMaster.orgChild2?.orgChild2Name ?? null, + child3: posMaster.orgChild3?.orgChild3Name ?? null, + child4: posMaster.orgChild4?.orgChild4Name ?? null, + }); + } + /** * API * @@ -6257,21 +6362,20 @@ export class ProfileController extends Controller { ] .filter(Boolean) .join("\n"); - - const shortName = - !holder - ? null - : holder.orgChild4 != null - ? `${holder.orgChild4.orgChild4ShortName} ${holder.posMasterNo}` - : holder.orgChild3 != null - ? `${holder.orgChild3.orgChild3ShortName} ${holder.posMasterNo}` - : holder.orgChild2 != null - ? `${holder.orgChild2.orgChild2ShortName} ${holder.posMasterNo}` - : holder.orgChild1 != null - ? `${holder.orgChild1.orgChild1ShortName} ${holder.posMasterNo}` - : holder.orgRoot != null - ? `${holder.orgRoot.orgRootShortName} ${holder.posMasterNo}` - : null; + + const shortName = !holder + ? null + : holder.orgChild4 != null + ? `${holder.orgChild4.orgChild4ShortName} ${holder.posMasterNo}` + : holder.orgChild3 != null + ? `${holder.orgChild3.orgChild3ShortName} ${holder.posMasterNo}` + : holder.orgChild2 != null + ? `${holder.orgChild2.orgChild2ShortName} ${holder.posMasterNo}` + : holder.orgChild1 != null + ? `${holder.orgChild1.orgChild1ShortName} ${holder.posMasterNo}` + : holder.orgRoot != null + ? `${holder.orgRoot.orgRootShortName} ${holder.posMasterNo}` + : null; return { id: _data.id, @@ -8271,15 +8375,16 @@ export class ProfileController extends Controller { body: { fieldName?: string | null; keyword?: string; - system?: string; + system?: string; }, ) { // ค้นหารายชื่อถ้าไม่ส่ง system มาให้ default ตามทะเบียนประวัติ - let _system:string ="SYS_REGISTRY_OFFICER"; - if(body.system) - _system = body.system; + let _system: string = "SYS_REGISTRY_OFFICER"; + if (body.system) _system = body.system; let _data = await new permission().PermissionOrgList(request, _system); - const findRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true } }); + const findRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true }, + }); if (!findRevision) { throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } @@ -8299,14 +8404,15 @@ export class ProfileController extends Controller { // IFNULL(orgRoot.orgRootShortName, '')," ", // IFNULL(current_holders.posMasterNo , '') // ) LIKE :keyword`; - let queryLike = "1=1" + let queryLike = "1=1"; switch (body.fieldName) { case "citizenId": queryLike = "profile.citizenId LIKE :keyword"; break; case "fullName": - queryLike = "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword"; + queryLike = + "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword"; break; case "position": @@ -8314,7 +8420,7 @@ export class ProfileController extends Controller { break; case "posNo": - queryLike = ` + queryLike = ` CASE WHEN current_holders.orgChild4Id IS NOT NULL THEN CONCAT(orgChild4.orgChild4ShortName, " ", current_holders.posMasterNo) WHEN current_holders.orgChild3Id IS NOT NULL THEN CONCAT(orgChild3.orgChild3ShortName, " ", current_holders.posMasterNo) @@ -8338,10 +8444,10 @@ export class ProfileController extends Controller { break; default: - queryLike = "1=1" + queryLike = "1=1"; break; } - + const [findProfile, total] = await this.profileRepo .createQueryBuilder("profile") .leftJoinAndSelect("profile.posType", "posType")