diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index 8f1dc9c0..a1ac4736 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -60,16 +60,103 @@ export class OrganizationDotnetController extends Controller { citizenId?: string | null; firstName?: string | null; lastName?: string | null; + role?: string | null; + nodeId?: string | null; + node?: number | null; }, ) { - const profileRepository = AppDataSource.getRepository(Profile); - const queryBuilder = profileRepository + // const profileRepository = AppDataSource.getRepository(Profile); + // const queryBuilder = profileRepository + // .createQueryBuilder("profile") + // .leftJoinAndSelect("profile.posLevel", "posLevel") + // .leftJoinAndSelect("profile.posType", "posType") + // if (body.citizenId || body.firstName || body.lastName) { + // queryBuilder.where( + // new Brackets((qb) => { + // if (body.citizenId) { + // qb.orWhere("profile.citizenId LIKE :citizenId", { citizenId: `%${body.citizenId}%` }); + // } + // if (body.firstName) { + // qb.orWhere("profile.firstName LIKE :firstName", { firstName: `%${body.firstName}%` }); + // } + // if (body.lastName) { + // qb.orWhere("profile.lastName LIKE :lastName", { lastName: `%${body.lastName}%` }); + // } + // }), + // ); + // } + // const profiles = await queryBuilder.getMany(); + let condition = "1=1"; + let conditionParams = {}; + if (body.role === "CHILD") { + switch (body.node) { + case 0: + condition = "orgRoot.ancestorDNA = :nodeId"; + break; + case 1: + condition = "orgChild1.ancestorDNA = :nodeId"; + break; + case 2: + condition = "orgChild2.ancestorDNA = :nodeId"; + break; + case 3: + condition = "orgChild3.ancestorDNA = :nodeId"; + break; + case 4: + condition = "orgChild4.ancestorDNA = :nodeId"; + break; + default: + condition = "1=1"; + break; + } + conditionParams = { nodeId: body.nodeId }; + } + else if (body.role === "ROOT") { + condition = "orgRoot.ancestorDNA = :nodeId"; + conditionParams = { nodeId: body.nodeId }; + } + else if (body.role === "NORMAL") { + switch (body.node) { + case 0: + condition = "orgRoot.ancestorDNA = :nodeId AND current_holders.orgChild1 IS NULL"; + break; + case 1: + condition = "orgChild1.ancestorDNA = :nodeId AND current_holders.orgChild2 IS NULL"; + break; + case 2: + condition = "orgChild2.ancestorDNA = :nodeId AND current_holders.orgChild3 IS NULL"; + break; + case 3: + condition = "orgChild3.ancestorDNA = :nodeId AND current_holders.orgChild4 IS NULL"; + break; + case 4: + condition = "orgChild4.ancestorDNA = :nodeId"; + break; + default: + condition = "1=1"; + break; + } + conditionParams = { nodeId: body.nodeId }; + } + + const findRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false } + }); + if (!findRevision) { + throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); + } + const profiles = await this.profileRepo .createQueryBuilder("profile") .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType"); - - if (body.citizenId || body.firstName || body.lastName) { - queryBuilder.where( + .leftJoinAndSelect("profile.posType", "posType") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.orgRoot", "orgRoot") + .leftJoinAndSelect("current_holders.orgChild1", "orgChild1") + .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") + .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") + .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") + .where("current_holders.orgRevisionId = :orgRevisionId", { orgRevisionId: findRevision.id }) + .andWhere( new Brackets((qb) => { if (body.citizenId) { qb.orWhere("profile.citizenId LIKE :citizenId", { citizenId: `%${body.citizenId}%` }); @@ -81,9 +168,10 @@ export class OrganizationDotnetController extends Controller { qb.orWhere("profile.lastName LIKE :lastName", { lastName: `%${body.lastName}%` }); } }), - ); - } - const profiles = await queryBuilder.getMany(); + ) + .andWhere(condition, conditionParams) + .getMany() + return new HttpSuccess(profiles); }