diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 24505bac..18960fbc 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -2818,57 +2818,57 @@ export class PositionController extends Controller { typeCondition = { orgRootId: body.nodeId, orgChild1Id: IsNull(), - current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + current_holderId: body.isBlank == true ?? IsNull(), }; } else if (body.node === 1) { typeCondition = { orgChild1Id: body.nodeId, orgChild2Id: IsNull(), - current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + current_holderId: body.isBlank == true ?? IsNull(), }; } else if (body.node === 2) { typeCondition = { orgChild2Id: body.nodeId, orgChild3Id: IsNull(), - current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + current_holderId: body.isBlank == true ?? IsNull(), }; } else if (body.node === 3) { typeCondition = { orgChild3Id: body.nodeId, orgChild4Id: IsNull(), - current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + current_holderId: body.isBlank == true ?? IsNull(), }; } else if (body.node === 4) { typeCondition = { orgChild4Id: body.nodeId, - current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + current_holderId: body.isBlank == true ?? IsNull(), }; } } else { if (body.node === 0) { typeCondition = { orgRootId: body.nodeId, - current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + current_holderId: body.isBlank == true ?? IsNull(), }; } else if (body.node === 1) { typeCondition = { orgChild1Id: body.nodeId, - current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + current_holderId: body.isBlank == true ?? IsNull(), }; } else if (body.node === 2) { typeCondition = { orgChild2Id: body.nodeId, - current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + current_holderId: body.isBlank == true ?? IsNull(), }; } else if (body.node === 3) { typeCondition = { orgChild3Id: body.nodeId, - current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + current_holderId: body.isBlank == true ?? IsNull(), }; } else if (body.node === 4) { typeCondition = { orgChild4Id: body.nodeId, - current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + current_holderId: body.isBlank == true ?? IsNull(), }; } } diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index c42e6f81..ad746e38 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -573,30 +573,187 @@ export class ProfileController extends Controller { where: { keycloak: request.user.sub }, }); - const _caregiver = await this.profileRepo + 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 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") - .andWhere({ keycloak: Not(IsNull()) }) - .skip((1 - 1) * 20) - .take(20) - .getMany(); - const _commander = await this.profileRepo + .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 _caregiver = await caregiverQueryBuilder.getMany(); + + const commanderQueryBuilder = this.profileRepo .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") .leftJoinAndSelect("profile.posLevel", "posLevel") .leftJoinAndSelect("profile.posType", "posType") - .andWhere({ keycloak: Not(IsNull()) }) - .skip((1 - 1) * 20) - .take(20) - .getMany(); - const _chairman = await this.profileRepo + .where("current_holders.current_holderId IS NOT NULL") + .andWhere(profileConditions); + + if (orgRoot) { + commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { + orgRootId: orgRoot, + }); + } + 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") - .andWhere({ keycloak: Not(IsNull()) }) - .skip((1 - 1) * 20) - .take(20) - .getMany(); + .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,