Skip to content

Commit

Permalink
Add paginated search backend
Browse files Browse the repository at this point in the history
  • Loading branch information
mpbrown committed Dec 24, 2024
1 parent 8750659 commit a9dec54
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ public List<ApiUserWithStatus> usersWithStatus() {
}

@QueryMapping
public Page<ApiUserWithStatus> usersWithStatusPage(@Argument int pageNumber) {
public Page<ApiUserWithStatus> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,33 @@ public Page<ApiUserWithStatus> getPagedUsersAndStatusInCurrentOrg(int pageNumber
return new PageImpl<>(userWithStatusList, pageRequest, userCountInOrg);
}

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

List<ApiUserWithStatus> 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<ApiUserWithStatus> 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<ApiUserWithStatus> getUsersAndStatusInCurrentOrg() {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/resources/graphql/main.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ void getPagedUsersAndStatusInCurrentOrg_success() {
users2ndList.get(2), "[email protected]", "Reynolds", UserStatus.ACTIVE);
}

@Test
@WithSimpleReportOrgAdminUser
void searchUsersAndStatusInCurrentOrgPaged_success() {
initSampleData();
Page<ApiUserWithStatus> usersPage =
_service.searchUsersAndStatusInCurrentOrgPaged(0, 10, "Bob");
List<ApiUserWithStatus> users = usersPage.stream().toList();
assertEquals(1, users.size());
checkApiUserWithStatus(users.get(0), "[email protected]", "Bobberoo", UserStatus.ACTIVE);
}

@Test
@WithSimpleReportOrgAdminUser
void getUser_withAdminUser_withOktaMigrationDisabled_success() {
Expand Down

0 comments on commit a9dec54

Please sign in to comment.