From 56a257e82f195bc6e27a895c2a1ff0ae0a310f1c Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 26 Jun 2024 15:10:07 +0700 Subject: [PATCH] fix api commander --- src/controllers/ProfileController.ts | 372 ++++++++++++--------------- 1 file changed, 169 insertions(+), 203 deletions(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 3dee70b9..0b1e00f3 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -577,217 +577,183 @@ export class ProfileController extends Controller { 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 profileConditions = { - posLevelId: profile?.posLevelId, - posTypeId: profile?.posTypeId, - keycloak: Not(IsNull()), - }; - - const caregiverQueryBuilder = this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.current_holders", "current_holders") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType") - .where("current_holders.current_holderId IS NOT NULL") - .andWhere(profileConditions) - if (orgRoot) { - caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); - } - if (orgChild1) { - caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); - caregiverQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild1Id", { - orgChild1Id: orgChild1, - }); - } - if (orgChild2) { - caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); - caregiverQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild1Id", { - orgChild1Id: orgChild1, - }); - caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild2Id", { - orgChild2Id: orgChild2, - }); - } - if (orgChild3) { - caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); - caregiverQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", { - orgChild1Id: orgChild1, - }); - caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild2Id", { - orgChild2Id: orgChild2, - }); - caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild3Id", { - orgChild3Id: orgChild3, - }); - } - if (orgChild4) { - caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); - caregiverQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", { - orgChild1Id: orgChild1, - }); - caregiverQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild2Id", { - orgChild2Id: orgChild2, - }); - caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild3Id", { - orgChild3Id: orgChild3, - }); - caregiverQueryBuilder.andWhere("current_holders.orgChild4Id = :orgChild4Id", { - orgChild4Id: orgChild4, - }); - } + 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(); - const _caregiver = await caregiverQueryBuilder.getMany(); - - const commanderQueryBuilder = this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.current_holders", "current_holders") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType") - .where("current_holders.current_holderId IS NOT NULL") - .andWhere(profileConditions); - - if (orgRoot) { - commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); + 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, + })); } - if (orgChild1) { - commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); - } - if (orgChild2) { - commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); - commanderQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild1Id", { - orgChild1Id: orgChild1, - }); - } - if (orgChild3) { - commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); - commanderQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", { - orgChild1Id: orgChild1, - }); - commanderQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild2Id", { - orgChild2Id: orgChild2, - }); - } - if (orgChild4) { - commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); - commanderQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", { - orgChild1Id: orgChild1, - }); - commanderQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild2Id", { - orgChild2Id: orgChild2, - }); - commanderQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild3Id", { - orgChild3Id: orgChild3, - }); - } - - const _commander = await commanderQueryBuilder.getMany(); - - const chairManQueryBuilder = this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.current_holders", "current_holders") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType") - .where("current_holders.current_holderId IS NOT NULL") - .andWhere(profileConditions); - - if (orgRoot) { - chairManQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }); - } - if (orgChild1) { - chairManQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); - } - if (orgChild2) { - chairManQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); - } - if (orgChild3) { - chairManQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); - chairManQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", { - orgChild1Id: orgChild1, - }); - } - if (orgChild4) { - chairManQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { - orgRootId: orgRoot, - }); - chairManQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", { - orgChild1Id: orgChild1, - }); - chairManQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild2Id", { - orgChild2Id: orgChild2, - }); - } - - const _chairman = await chairManQueryBuilder.getMany(); - - - - const caregiver = _caregiver.map((_data) => ({ - 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, - })); - const commander = _commander.map((_data) => ({ - 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, - })); - const chairman = _chairman.map((_data) => ({ - 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 }); }