diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 1077fa51..1b9a7435 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -436,57 +436,61 @@ export class KeycloakController extends Controller { let condition: any = {}; if (req.user.role.includes("ADMIN")) { - const profile = await this.profileRepo.findOne({ - relations:[ - "current_holders", - "current_holders.orgRevision", - ], - where: { + const profile = await this.profileRepo.findOne({ + relations: ["current_holders", "current_holders.orgRevision"], + where: { keycloak: req.user.sub, - current_holders:{ - orgRevision:{ + current_holders: { + orgRevision: { orgRevisionIsCurrent: true, - orgRevisionIsDraft: false - } - } - } + orgRevisionIsDraft: false, + }, + }, + }, }); - if(profile?.current_holders[0].orgRootId && profile?.current_holders[0].orgChild1Id == null){ - condition = - `current_holders.orgRootId = '${profile?.current_holders[0].orgRootId}' + if ( + profile?.current_holders[0]?.orgRootId && + profile?.current_holders[0]?.orgChild1Id == null + ) { + condition = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' and current_holders.orgChild1Id IS NULL and current_holders.orgChild2Id IS NULL and current_holders.orgChild3Id IS NULL and current_holders.orgChild4Id IS NULL`; - }else if(profile?.current_holders[0].orgChild1Id && profile?.current_holders[0].orgChild2Id == null){ - condition = - `current_holders.orgRootId = '${profile?.current_holders[0].orgRootId}' - and current_holders.orgChild1Id '${profile?.current_holders[0].orgChild1Id}' + } else if ( + profile?.current_holders[0]?.orgChild1Id && + profile?.current_holders[0]?.orgChild2Id == null + ) { + condition = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' + and current_holders.orgChild1Id = '${profile?.current_holders[0]?.orgChild1Id}' and current_holders.orgChild2Id IS NULL and current_holders.orgChild3Id IS NULL and current_holders.orgChild4Id IS NULL`; - }else if(profile?.current_holders[0].orgChild2Id && profile?.current_holders[0].orgChild3Id == null){ - condition = - `current_holders.orgRootId = '${profile?.current_holders[0].orgRootId}' - and current_holders.orgChild1Id '${profile?.current_holders[0].orgChild1Id}' - and current_holders.orgChild2Id '${profile?.current_holders[0].orgChild2Id}' + } else if ( + profile?.current_holders[0]?.orgChild2Id && + profile?.current_holders[0]?.orgChild3Id == null + ) { + condition = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' + and current_holders.orgChild1Id = '${profile?.current_holders[0]?.orgChild1Id}' + and current_holders.orgChild2Id = '${profile?.current_holders[0]?.orgChild2Id}' and current_holders.orgChild3Id IS NULL and current_holders.orgChild4Id IS NULL`; - }else if(profile?.current_holders[0].orgChild3Id && profile?.current_holders[0].orgChild4Id == null){ - condition = - `current_holders.orgRootId = '${profile?.current_holders[0].orgRootId}' - and current_holders.orgChild1Id '${profile?.current_holders[0].orgChild1Id}' - and current_holders.orgChild2Id '${profile?.current_holders[0].orgChild2Id}' - and current_holders.orgChild3Id '${profile?.current_holders[0].orgChild3Id}' + } else if ( + profile?.current_holders[0]?.orgChild3Id && + profile?.current_holders[0]?.orgChild4Id == null + ) { + condition = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' + and current_holders.orgChild1Id = '${profile?.current_holders[0]?.orgChild1Id}' + and current_holders.orgChild2Id = '${profile?.current_holders[0]?.orgChild2Id}' + and current_holders.orgChild3Id = '${profile?.current_holders[0]?.orgChild3Id}' and current_holders.orgChild4Id IS NULL`; - }else if(profile?.current_holders[0].orgChild4Id){ - condition = - `current_holders.orgRootId = '${profile?.current_holders[0].orgRootId}' - and current_holders.orgChild1Id '${profile?.current_holders[0].orgChild1Id}' - and current_holders.orgChild2Id '${profile?.current_holders[0].orgChild2Id}' - and current_holders.orgChild3Id '${profile?.current_holders[0].orgChild3Id}' - and current_holders.orgChild4Id '${profile?.current_holders[0].orgChild4Id}'`; + } else if (profile?.current_holders[0]?.orgChild4Id) { + condition = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' + and current_holders.orgChild1Id = '${profile?.current_holders[0]?.orgChild1Id}' + and current_holders.orgChild2Id = '${profile?.current_holders[0]?.orgChild2Id}' + and current_holders.orgChild3Id = '${profile?.current_holders[0]?.orgChild3Id}' + and current_holders.orgChild4Id = '${profile?.current_holders[0]?.orgChild4Id}'`; } } let profiles: any = [];