diff --git a/backend/src/main/java/gov/cdc/usds/simplereport/api/apiuser/UserResolver.java b/backend/src/main/java/gov/cdc/usds/simplereport/api/apiuser/UserResolver.java index add7db7d80..02d5860e5f 100644 --- a/backend/src/main/java/gov/cdc/usds/simplereport/api/apiuser/UserResolver.java +++ b/backend/src/main/java/gov/cdc/usds/simplereport/api/apiuser/UserResolver.java @@ -42,10 +42,15 @@ public List usersWithStatus() { } @QueryMapping - public Page usersWithStatusPage(@Argument int pageNumber) { + public Page usersWithStatusPage( + @Argument int pageNumber, @Argument String searchQuery) { if (pageNumber < 0) { pageNumber = ApiUserService.DEFAULT_OKTA_USER_PAGE_OFFSET; } + if (!searchQuery.isBlank()) { + return _userService.searchUsersAndStatusInCurrentOrgPaged( + pageNumber, DEFAULT_OKTA_USER_PAGE_SIZE, searchQuery); + } return _userService.getPagedUsersAndStatusInCurrentOrg(pageNumber, DEFAULT_OKTA_USER_PAGE_SIZE); } diff --git a/backend/src/main/java/gov/cdc/usds/simplereport/service/ApiUserService.java b/backend/src/main/java/gov/cdc/usds/simplereport/service/ApiUserService.java index 8a73e5f21e..71fdb81f32 100644 --- a/backend/src/main/java/gov/cdc/usds/simplereport/service/ApiUserService.java +++ b/backend/src/main/java/gov/cdc/usds/simplereport/service/ApiUserService.java @@ -638,6 +638,33 @@ public Page getPagedUsersAndStatusInCurrentOrg(int pageNumber return new PageImpl<>(userWithStatusList, pageRequest, userCountInOrg); } + public Page searchUsersAndStatusInCurrentOrgPaged( + int pageNumber, int pageSize, String searchQuery) { + List allUsers = getUsersAndStatusInCurrentOrg(); + + List filteredUsersList = + allUsers.stream() + .filter( + u -> { + String firstName = + u.getFirstName() == null ? "" : String.format("%s ", u.getFirstName()); + String middleName = + u.getMiddleName() == null ? "" : String.format("%s ", u.getMiddleName()); + String fullName = firstName + middleName + u.getLastName(); + return fullName.toLowerCase().contains(searchQuery.toLowerCase()); + }) + .toList(); + + int totalResults = filteredUsersList.size(); + int startIndex = pageNumber * pageSize; + int endIndex = Math.min((startIndex + pageSize), filteredUsersList.size()); + + List pageContent = filteredUsersList.subList(startIndex, endIndex); + PageRequest pageRequest = PageRequest.of(pageNumber, pageSize); + + return new PageImpl<>(pageContent, pageRequest, totalResults); + } + // To be addressed in #8108 @AuthorizationConfiguration.RequirePermissionManageUsers public List getUsersAndStatusInCurrentOrg() { diff --git a/backend/src/main/resources/graphql/main.graphqls b/backend/src/main/resources/graphql/main.graphqls index c37dbdc40f..3ed7da1808 100644 --- a/backend/src/main/resources/graphql/main.graphqls +++ b/backend/src/main/resources/graphql/main.graphqls @@ -550,7 +550,7 @@ type Query { users: [ApiUser] @requiredPermissions(allOf: ["MANAGE_USERS"]) usersWithStatus: [ApiUserWithStatus!] @requiredPermissions(allOf: ["MANAGE_USERS"]) - usersWithStatusPage(pageNumber: Int): ApiUserWithStatusPage! @requiredPermissions(allOf: ["MANAGE_USERS"]) + usersWithStatusPage(pageNumber: Int, searchQuery: String): ApiUserWithStatusPage! @requiredPermissions(allOf: ["MANAGE_USERS"]) user(id: ID, email: String): User @requiredPermissions(allOf: ["MANAGE_USERS"]) whoami: User! diff --git a/backend/src/test/java/gov/cdc/usds/simplereport/service/ApiUserServiceTest.java b/backend/src/test/java/gov/cdc/usds/simplereport/service/ApiUserServiceTest.java index d985d10727..0f537b9905 100644 --- a/backend/src/test/java/gov/cdc/usds/simplereport/service/ApiUserServiceTest.java +++ b/backend/src/test/java/gov/cdc/usds/simplereport/service/ApiUserServiceTest.java @@ -173,6 +173,17 @@ void getPagedUsersAndStatusInCurrentOrg_success() { users2ndList.get(2), "notruby@example.com", "Reynolds", UserStatus.ACTIVE); } + @Test + @WithSimpleReportOrgAdminUser + void searchUsersAndStatusInCurrentOrgPaged_success() { + initSampleData(); + Page usersPage = + _service.searchUsersAndStatusInCurrentOrgPaged(0, 10, "Bob"); + List users = usersPage.stream().toList(); + assertEquals(1, users.size()); + checkApiUserWithStatus(users.get(0), "bobbity@example.com", "Bobberoo", UserStatus.ACTIVE); + } + @Test @WithSimpleReportOrgAdminUser void getUser_withAdminUser_withOktaMigrationDisabled_success() {