@@ -35,6 +35,37 @@ def get_user_profile() -> dict[str, Any]:
3535 return dict (response )
3636
3737
38+ def filter_users_by_office (data : dict , office : str ) -> dict :
39+ """
40+ Filter users JSON to only include users that have roles for the specified office.
41+ Each user's roles dict will only contain the entry for that office.
42+
43+ Args:
44+ data: The full users JSON as a Python dict.
45+ office: The office key to filter by (e.g., 'MVP', 'LRL').
46+
47+ Returns:
48+ A new dict with the same structure, filtered to the specified office.
49+ """
50+ filtered_users = []
51+
52+ for user in data .get ("users" , []):
53+ roles = user .get ("roles" , {})
54+
55+ if office in roles :
56+ # Build a copy of the user with only the target office's roles
57+ filtered_user = {k : v for k , v in user .items () if k != "roles" }
58+ filtered_user ["roles" ] = {office : roles [office ]}
59+ filtered_users .append (filtered_user )
60+
61+ return {
62+ "page" : data .get ("page" ),
63+ "page-size" : data .get ("page-size" ),
64+ "total" : len (filtered_users ),
65+ "users" : filtered_users ,
66+ }
67+
68+
3869def get_users (
3970 office_id : Optional [str ] = None ,
4071 username_like : Optional [str ] = None ,
@@ -63,11 +94,7 @@ def get_users(
6394 # currently support filtering by office on the backend. This is a
6495 # temporary workaround until the API supports office filtering.
6596 if office_id :
66- data = response
67- filtered_users = [
68- user for user in data ["users" ] if office_id in user .get ("roles" , {})
69- ]
70- response = {** data , "users" : filtered_users , "total" : len (filtered_users )}
97+ response = filter_users_by_office (response , office_id )
7198 return Data (response , selector = "users" )
7299
73100
0 commit comments