diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index b7fd94dd..40409989 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -967,6 +967,213 @@ export class ProfileController extends Controller { return new HttpSuccess({ caregiver, commander, chairman }); } + /** + * API เลือกผู้ประเมินสำหรับ User (ADMIN) + * + * @summary เลือกผู้ประเมินสำหรับ User (ADMIN) + * + * @param {string} profileId Id ทะเบียนประวัติ User + */ + @Get("commander/{profileId}") + async getProfileCommanderUser(@Request() request: RequestWithUser, @Path() profileId: string) { + const profile = await this.profileRepo.findOne({ + where: { id: profileId }, + }); + + const posMaster = await this.posMasterRepo.findOne({ + where: { current_holderId: profile?.id }, + }); + if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลการครองตำแหน่ง"); + const orgRoot = posMaster.orgRootId; + const orgChild1 = posMaster.orgChild1Id; + const orgChild2 = posMaster.orgChild2Id; + const orgChild3 = posMaster.orgChild3Id; + const orgChild4 = posMaster.orgChild4Id; + + const findChild4 = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .getMany(); + const findChild3 = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .andWhere("current_holders.orgChild3Id = :orgChild3Id", { orgChild3Id: orgChild3 }) + .andWhere("current_holders.orgChild4Id IS NULL") + .getMany(); + const findChild2 = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .andWhere("current_holders.orgChild2Id = :orgChild2Id", { orgChild2Id: orgChild2 }) + .andWhere("current_holders.orgChild3Id IS NULL") + .getMany(); + const findChild1 = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .andWhere("current_holders.orgChild1Id = :orgChild1Id", { orgChild1Id: orgChild1 }) + .andWhere("current_holders.orgChild2Id IS NULL") + .getMany(); + const findRoot = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .andWhere({ keycloak: Not(IsNull()) }) + .where("current_holders.current_holderId IS NOT NULL") + .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }) + .andWhere("current_holders.current_holderId <> :current_holderId", { + current_holderId: posMaster.current_holderId, + }) + .andWhere("current_holders.orgChild1Id IS NULL") + .getMany(); + + let _caregiver: any; + let caregiver: any; + let _commander: any; + let commander: any; + let _chairman: any; + let chairman: any; + if (posMaster) { + if (orgChild4) { + if (findChild4) { + _caregiver = findChild4; + _commander = findChild3; + _chairman = findChild2; + } else if (findChild3) { + _caregiver = findChild3; + _commander = findChild2; + _chairman = findChild1; + } else if (findChild2) { + _caregiver = findChild2; + _commander = findChild1; + _chairman = findRoot; + } else if (findChild1) { + _caregiver = findChild1; + _commander = findRoot; + _chairman = findRoot; + } else if (findRoot) { + _caregiver = findRoot; + _commander = findRoot; + _chairman = findRoot; + } + } else if (orgChild3) { + if (findChild3) { + _caregiver = findChild3; + _commander = findChild2; + _chairman = findChild1; + } else if (findChild2) { + _caregiver = findChild2; + _commander = findChild1; + _chairman = findRoot; + } else if (findChild1) { + _caregiver = findChild1; + _commander = findRoot; + _chairman = findRoot; + } else if (findRoot) { + _caregiver = findRoot; + _commander = findRoot; + _chairman = findRoot; + } + } else if (orgChild2) { + if (findChild2) { + _caregiver = findChild2; + _commander = findChild1; + _chairman = findRoot; + } else if (findChild1) { + _caregiver = findChild1; + _commander = findRoot; + _chairman = findRoot; + } else if (findRoot) { + _caregiver = findRoot; + _commander = findRoot; + _chairman = findRoot; + } + } else if (orgChild1) { + if (findChild1) { + _caregiver = findChild1; + _commander = findRoot; + _chairman = findRoot; + } else if (findRoot) { + _caregiver = findRoot; + _commander = findRoot; + _chairman = findRoot; + } + } else if (orgRoot) { + if (findRoot) { + _caregiver = findRoot; + _commander = findRoot; + _chairman = findRoot; + } + } + caregiver = _caregiver.map((_data: any) => ({ + id: _data.id, + prefix: _data.prefix, + firstName: _data.firstName, + lastName: _data.lastName, + citizenId: _data.citizenId, + position: _data.position, + posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, + posType: _data.posType == null ? null : _data.posType.posTypeName, + isDirector: true, + })); + commander = _commander.map((_data: any) => ({ + id: _data.id, + prefix: _data.prefix, + firstName: _data.firstName, + lastName: _data.lastName, + citizenId: _data.citizenId, + position: _data.position, + posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, + posType: _data.posType == null ? null : _data.posType.posTypeName, + isDirector: true, + })); + chairman = _chairman.map((_data: any) => ({ + id: _data.id, + prefix: _data.prefix, + firstName: _data.firstName, + lastName: _data.lastName, + citizenId: _data.citizenId, + position: _data.position, + posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, + posType: _data.posType == null ? null : _data.posType.posTypeName, + isDirector: true, + })); + } + + return new HttpSuccess({ caregiver, commander, chairman }); + } + /** * API สร้างทะเบียนประวัติ *