Skip to content

Commit

Permalink
Fixed Cognito /users, /groups sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed Jul 9, 2024
1 parent bd17e0b commit e5419d5
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.formkiq.aws.services.lambda.ApiPermission;
import com.formkiq.aws.services.lambda.ApiRequestHandlerResponse;
import com.formkiq.module.lambdaservices.AwsServiceCache;
import com.formkiq.stacks.api.transformers.GroupNameComparator;
import com.formkiq.stacks.api.transformers.GroupsResponseToMap;
import software.amazon.awssdk.services.cognitoidentityprovider.model.ListGroupsResponse;

Expand Down Expand Up @@ -72,8 +73,8 @@ public ApiRequestHandlerResponse get(final LambdaLogger logger,

ListGroupsResponse response = service.listGroups(token, limit);

List<Map<String, Object>> groups =
response.groups().stream().map(new GroupsResponseToMap()).toList();
List<Map<String, Object>> groups = response.groups().stream().sorted(new GroupNameComparator())
.map(new GroupsResponseToMap()).toList();

Map<String, Object> map = new HashMap<>();
map.put("groups", groups);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.formkiq.aws.services.lambda.ApiPermission;
import com.formkiq.aws.services.lambda.ApiRequestHandlerResponse;
import com.formkiq.module.lambdaservices.AwsServiceCache;
import com.formkiq.stacks.api.transformers.UserTypeComparator;
import com.formkiq.stacks.api.transformers.UsersResponseToMap;
import software.amazon.awssdk.services.cognitoidentityprovider.model.ListUsersInGroupResponse;

Expand Down Expand Up @@ -68,8 +69,8 @@ public ApiRequestHandlerResponse get(final LambdaLogger logger,

ListUsersInGroupResponse response = service.listUsersInGroup(groupName, token, limit);

List<Map<String, Object>> users =
response.users().stream().map(new UsersResponseToMap()).toList();
List<Map<String, Object>> users = response.users().stream().sorted(new UserTypeComparator())
.map(new UsersResponseToMap()).toList();

Map<String, Object> map = new HashMap<>();
map.put("users", users);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.formkiq.aws.services.lambda.ApiPermission;
import com.formkiq.aws.services.lambda.ApiRequestHandlerResponse;
import com.formkiq.module.lambdaservices.AwsServiceCache;
import com.formkiq.stacks.api.transformers.GroupNameComparator;
import com.formkiq.stacks.api.transformers.GroupsResponseToMap;
import software.amazon.awssdk.services.cognitoidentityprovider.model.AdminListGroupsForUserResponse;

Expand Down Expand Up @@ -69,8 +70,8 @@ public ApiRequestHandlerResponse get(final LambdaLogger logger,

AdminListGroupsForUserResponse response = service.listGroups(username, token, limit);

List<Map<String, Object>> groups =
response.groups().stream().map(new GroupsResponseToMap()).toList();
List<Map<String, Object>> groups = response.groups().stream().sorted(new GroupNameComparator())
.map(new GroupsResponseToMap()).toList();

Map<String, Object> map = new HashMap<>();
map.put("groups", groups);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.formkiq.aws.services.lambda.ApiPermission;
import com.formkiq.aws.services.lambda.ApiRequestHandlerResponse;
import com.formkiq.module.lambdaservices.AwsServiceCache;
import com.formkiq.stacks.api.transformers.UserTypeComparator;
import com.formkiq.stacks.api.transformers.UsersResponseToMap;
import software.amazon.awssdk.services.cognitoidentityprovider.model.ListUsersResponse;

Expand Down Expand Up @@ -64,8 +65,8 @@ public ApiRequestHandlerResponse get(final LambdaLogger logger,
Map<String, Object> map = new HashMap<>();

if (!response.users().isEmpty()) {
List<Map<String, Object>> users =
response.users().stream().map(new UsersResponseToMap()).toList();
List<Map<String, Object>> users = response.users().stream().sorted(new UserTypeComparator())
.map(new UsersResponseToMap()).toList();

map.put("user", users.get(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.formkiq.aws.services.lambda.ApiPermission;
import com.formkiq.aws.services.lambda.ApiRequestHandlerResponse;
import com.formkiq.module.lambdaservices.AwsServiceCache;
import com.formkiq.stacks.api.transformers.UserTypeComparator;
import com.formkiq.stacks.api.transformers.UsersResponseToMap;
import software.amazon.awssdk.services.cognitoidentityprovider.model.ListUsersResponse;

Expand Down Expand Up @@ -66,8 +67,8 @@ public ApiRequestHandlerResponse get(final LambdaLogger logger,

ListUsersResponse response = service.listUsers(token, limit);

List<Map<String, Object>> users =
response.users().stream().map(new UsersResponseToMap()).toList();
List<Map<String, Object>> users = response.users().stream().sorted(new UserTypeComparator())
.map(new UsersResponseToMap()).toList();

Map<String, Object> map = new HashMap<>();
map.put("users", users);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* MIT License
*
* Copyright (c) 2018 - 2020 FormKiQ
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.formkiq.stacks.api.transformers;

import software.amazon.awssdk.services.cognitoidentityprovider.model.GroupType;

import java.io.Serializable;
import java.util.Comparator;

/**
* {@link Comparator} for GroupName.
*/
public class GroupNameComparator implements Comparator<GroupType>, Serializable {
@Override
public int compare(final GroupType o1, final GroupType o2) {
return o1.groupName().compareTo(o2.groupName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* MIT License
*
* Copyright (c) 2018 - 2020 FormKiQ
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.formkiq.stacks.api.transformers;

import software.amazon.awssdk.services.cognitoidentityprovider.model.UserType;

import java.io.Serializable;
import java.util.Comparator;

/**
* {@link Comparator} for {@link UserType}.
*/
public class UserTypeComparator implements Comparator<UserType>, Serializable {
@Override
public int compare(final UserType o1, final UserType o2) {
return o1.username().compareTo(o2.username());
}
}

0 comments on commit e5419d5

Please sign in to comment.