1111import inha .gdgoc .global .exception .GlobalErrorCode ;
1212import lombok .RequiredArgsConstructor ;
1313import org .springframework .data .domain .Page ;
14+ import org .springframework .data .domain .PageRequest ;
1415import org .springframework .data .domain .Pageable ;
16+ import org .springframework .data .domain .Sort ;
17+ import org .springframework .data .jpa .domain .JpaSort ;
1518import org .springframework .stereotype .Service ;
1619import org .springframework .transaction .annotation .Transactional ;
1720
@@ -25,7 +28,43 @@ public class UserAdminService {
2528
2629 @ Transactional (readOnly = true )
2730 public Page <UserSummaryResponse > listUsers (String q , Pageable pageable ) {
28- return userRepository .findSummaries (q , pageable );
31+ Pageable fixed = rewriteSort (pageable );
32+ return userRepository .findSummaries (q , fixed );
33+ }
34+
35+ private Pageable rewriteSort (Pageable pageable ) {
36+ Sort original = pageable .getSort ();
37+ if (original .isUnsorted ()) return pageable ;
38+
39+ Sort composed = Sort .unsorted ();
40+ boolean hasUserRoleOrder = false ;
41+
42+ for (Sort .Order o : original ) {
43+ String prop = o .getProperty ();
44+ Sort .Direction dir = o .getDirection ();
45+
46+ if ("userRole" .equals (prop )) {
47+ hasUserRoleOrder = true ;
48+ String roleRankCase = "CASE u.userRole " +
49+ "WHEN 'GUEST' THEN 0 " +
50+ "WHEN 'MEMBER' THEN 1 " +
51+ "WHEN 'CORE' THEN 2 " +
52+ "WHEN 'LEAD' THEN 3 " +
53+ "WHEN 'ORGANIZER' THEN 4 " +
54+ "WHEN 'ADMIN' THEN 5 " +
55+ "ELSE -1 END" ;
56+ composed = composed .and (JpaSort .unsafe (dir , roleRankCase ));
57+ } else {
58+ composed = composed .and (Sort .by (new Sort .Order (dir , prop )));
59+ }
60+ }
61+
62+ // ROLE 정렬 요청이 있었다면, 같은 권한 내에서 name ASC로 안정화
63+ if (hasUserRoleOrder ) {
64+ composed = composed .and (Sort .by ("name" ).ascending ());
65+ }
66+
67+ return PageRequest .of (pageable .getPageNumber (), pageable .getPageSize (), composed );
2968 }
3069
3170 /**
0 commit comments