Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add limit on returning number of spaces on dashboard; retrieve invitations as count #4907

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/services/api/me/me.resolver.fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ export class MeResolverFields {
return this.userService.getUserOrFail(agentInfo.userID);
}

@UseGuards(GraphqlGuard)
@ResolveField('communityInvitationsCount', () => Number, {
description:
'The number of invitations the current authenticated user can act on.',
})
public async communityInvitationsCount(
@CurrentUser() agentInfo: AgentInfo,
@Args({
name: 'states',
nullable: true,
type: () => [String],
description: 'The state names you want to filter on',
})
states: string[]
): Promise<number> {
if (agentInfo.userID === '') {
throw new ValidationException(
'Unable to retrieve invitations as no userID provided.',
LogContext.COMMUNITY
);
}
return this.meService.getCommunityInvitationsCountForUser(
agentInfo.userID,
states
);
}

@UseGuards(GraphqlGuard)
@ResolveField('communityInvitations', () => [CommunityInvitationResult], {
description: 'The invitations the current authenticated user can act on.',
Expand Down Expand Up @@ -113,9 +140,17 @@ export class MeResolverFields {
description: 'The hierarchy of the Spaces the current user is a member.',
})
public spaceMembershipsHierarchical(
@CurrentUser() agentInfo: AgentInfo
@CurrentUser() agentInfo: AgentInfo,
@Args({
name: 'limit',
type: () => Float,
description:
'The number of Spaces to return; if omitted return all journeys',
nullable: true,
})
limit: number
): Promise<CommunityMembershipResult[]> {
return this.meService.getSpaceMembershipsHierarchical(agentInfo);
return this.meService.getSpaceMembershipsHierarchical(agentInfo, limit);
}
techsmyth marked this conversation as resolved.
Show resolved Hide resolved

@UseGuards(GraphqlGuard)
Expand Down
21 changes: 18 additions & 3 deletions src/services/api/me/me.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export class MeService {
private readonly logger: LoggerService
) {}

public async getCommunityInvitationsCountForUser(
userId: string,
states?: string[]
): Promise<number> {
const invitations = await this.rolesService.getCommunityInvitationsForUser(
userId,
states
);
return invitations.length;
}
Comment on lines +33 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Optimize count retrieval for better performance.

The current implementation fetches all invitations just to get the count. Consider adding a dedicated count method in rolesService to avoid fetching unnecessary data.

 public async getCommunityInvitationsCountForUser(
   userId: string,
   states?: string[]
 ): Promise<number> {
-  const invitations = await this.rolesService.getCommunityInvitationsForUser(
+  return this.rolesService.getCommunityInvitationsCountForUser(
     userId,
     states
   );
-  return invitations.length;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public async getCommunityInvitationsCountForUser(
userId: string,
states?: string[]
): Promise<number> {
const invitations = await this.rolesService.getCommunityInvitationsForUser(
userId,
states
);
return invitations.length;
}
public async getCommunityInvitationsCountForUser(
userId: string,
states?: string[]
): Promise<number> {
return this.rolesService.getCommunityInvitationsCountForUser(
userId,
states
);
}


public async getCommunityInvitationsForUser(
userId: string,
states?: string[]
Expand Down Expand Up @@ -172,15 +183,19 @@ export class MeService {
}

public async getSpaceMembershipsHierarchical(
agentInfo: AgentInfo
agentInfo: AgentInfo,
limit?: number
): Promise<CommunityMembershipResult[]> {
const sortedFlatListSpacesWithMembership =
await this.getSpaceMembershipsForAgentInfo(agentInfo);

const levelZeroSpaces = this.filterSpacesByLevel(
const levelZeroSpacesRaw = this.filterSpacesByLevel(
sortedFlatListSpacesWithMembership,
SpaceLevel.L0
);
if (limit) {
levelZeroSpacesRaw.splice(limit);
}
const levelOneSpaces = this.filterSpacesByLevel(
sortedFlatListSpacesWithMembership,
SpaceLevel.L1
Expand All @@ -190,7 +205,7 @@ export class MeService {
SpaceLevel.L2
);

const levelZeroMemberships = levelZeroSpaces.map(levelZeroSpace => {
const levelZeroMemberships = levelZeroSpacesRaw.map(levelZeroSpace => {
const levelZeroMembership: CommunityMembershipResult = {
id: levelZeroSpace.id,
space: levelZeroSpace,
Expand Down