Skip to content

Commit

Permalink
Handle zero users in org
Browse files Browse the repository at this point in the history
  • Loading branch information
mpbrown committed Jan 10, 2025
1 parent d3166f1 commit 89e4c6b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package gov.cdc.usds.simplereport.api.apiuser;

import gov.cdc.usds.simplereport.api.model.ApiUserWithStatus;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.Page;

/**
* When the page content contains zero entries, this could be due to either: - zero search results
* for the query string - OR due to zero users in the entire organization. The value for total users
* in the org helps differentiate this.
*/
@Getter
@Setter
@AllArgsConstructor
public class ManageUsersPageWrapper {

Page<ApiUserWithStatus> pageContent;

int totalUsersInOrg;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -42,7 +41,7 @@ public List<ApiUserWithStatus> usersWithStatus() {
}

@QueryMapping
public Page<ApiUserWithStatus> usersWithStatusPage(
public ManageUsersPageWrapper usersWithStatusPage(
@Argument int pageNumber, @Argument String searchQuery) {
if (pageNumber < 0) {
pageNumber = ApiUserService.DEFAULT_OKTA_USER_PAGE_OFFSET;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gov.cdc.usds.simplereport.api.ApiUserContextHolder;
import gov.cdc.usds.simplereport.api.CurrentAccountRequestContextHolder;
import gov.cdc.usds.simplereport.api.WebhookContextHolder;
import gov.cdc.usds.simplereport.api.apiuser.ManageUsersPageWrapper;
import gov.cdc.usds.simplereport.api.model.ApiUserWithStatus;
import gov.cdc.usds.simplereport.api.model.Role;
import gov.cdc.usds.simplereport.api.model.errors.ConflictingUserException;
Expand Down Expand Up @@ -621,7 +622,7 @@ public List<ApiUser> getUsersInCurrentOrg() {
}

@AuthorizationConfiguration.RequirePermissionManageUsers
public Page<ApiUserWithStatus> getPagedUsersAndStatusInCurrentOrg(int pageNumber, int pageSize) {
public ManageUsersPageWrapper getPagedUsersAndStatusInCurrentOrg(int pageNumber, int pageSize) {
Organization org = _orgService.getCurrentOrganization();

final Map<String, UserStatus> emailsToStatus =
Expand All @@ -634,15 +635,18 @@ public Page<ApiUserWithStatus> getPagedUsersAndStatusInCurrentOrg(int pageNumber

Integer userCountInOrg = _oktaRepo.getUsersCountInOrganization(org);
PageRequest pageRequest = PageRequest.of(pageNumber, pageSize);
Page<ApiUserWithStatus> pageContent =
new PageImpl<>(userWithStatusList, pageRequest, userCountInOrg);

return new PageImpl<>(userWithStatusList, pageRequest, userCountInOrg);
return new ManageUsersPageWrapper(pageContent, userCountInOrg);
}

public Page<ApiUserWithStatus> searchUsersAndStatusInCurrentOrgPaged(
@AuthorizationConfiguration.RequirePermissionManageUsers
public ManageUsersPageWrapper searchUsersAndStatusInCurrentOrgPaged(
int pageNumber, int pageSize, String searchQuery) {
List<ApiUserWithStatus> allUsers = getUsersAndStatusInCurrentOrg();

List<ApiUserWithStatus> filteredUsersList =
List<ApiUserWithStatus> totalFilteredUsersList =
allUsers.stream()
.filter(
u -> {
Expand All @@ -655,14 +659,19 @@ public Page<ApiUserWithStatus> searchUsersAndStatusInCurrentOrgPaged(
})
.toList();

int totalResults = filteredUsersList.size();
int totalSearchResults = totalFilteredUsersList.size();
int startIndex = pageNumber * pageSize;
int endIndex = Math.min((startIndex + pageSize), filteredUsersList.size());
int endIndex = Math.min((startIndex + pageSize), totalFilteredUsersList.size());

Organization org = _orgService.getCurrentOrganization();
Integer userCountInOrg = _oktaRepo.getUsersCountInOrganization(org);

List<ApiUserWithStatus> pageContent = filteredUsersList.subList(startIndex, endIndex);
List<ApiUserWithStatus> filteredSublist = totalFilteredUsersList.subList(startIndex, endIndex);
PageRequest pageRequest = PageRequest.of(pageNumber, pageSize);
Page<ApiUserWithStatus> pageContent =
new PageImpl<>(filteredSublist, pageRequest, totalSearchResults);

return new PageImpl<>(pageContent, pageRequest, totalResults);
return new ManageUsersPageWrapper(pageContent, userCountInOrg);
}

// To be addressed in #8108
Expand Down

0 comments on commit 89e4c6b

Please sign in to comment.