Skip to content

Commit

Permalink
fix(groups): add filters and user fields to groups
Browse files Browse the repository at this point in the history
also filter both by display names
  • Loading branch information
stakach committed May 27, 2024
1 parent 40cf22b commit a923f55
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: office365
version: 1.25.0
version: 1.25.1

crystal: ">= 0.36.1"

Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module SpecHelper
end

def mock_list_users
WebMock.stub(:get, "https://graph.microsoft.com/v1.0/users?$select=id,userPrincipalName,surname,preferredLanguage,officeLocation,mobilePhone,mail,jobTitle,givenName,displayName,businessPhones,accountEnabled,mailNickname&$filter=accountEnabled eq true")
WebMock.stub(:get, "https://graph.microsoft.com/v1.0/users?$select=id,userPrincipalName,surname,preferredLanguage,officeLocation,mobilePhone,mail,jobTitle,givenName,displayName,businessPhones,accountEnabled,mailNickname&$orderby=displayName&$filter=accountEnabled eq true")
.to_return(mock_user_query.to_json)
end

Expand All @@ -142,7 +142,7 @@ module SpecHelper
end

def mock_list_group_members
WebMock.stub(:get, "https://graph.microsoft.com/v1.0/groups/1234-5678-9012/members/microsoft.graph.user?$orderby=displayName&$top=999")
WebMock.stub(:get, "https://graph.microsoft.com/v1.0/groups/1234-5678-9012/members/microsoft.graph.user?$select=id,userPrincipalName,surname,preferredLanguage,officeLocation,mobilePhone,mail,jobTitle,givenName,displayName,businessPhones,accountEnabled,mailNickname&$orderby=displayName&$top=999")
.to_return(mock_user_query.to_json)
end

Expand Down
16 changes: 13 additions & 3 deletions src/groups.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,25 @@ module Office365::Groups
end

# https://docs.microsoft.com/en-us/graph/api/group-list-members?view=graph-rest-1.0&tabs=http
def list_group_members_request(group_id : String, q : String? = nil)
def list_group_members_request(group_id : String, q : String? = nil, limit : Int32? = nil, filter : String? = nil, additional_fields : Array(String)? = nil)
if q.presence
filter_param = %("displayName:#{q}")
elsif filter
filter_param = filter
end

limit = limit || 999

fields = Users::SELECT_FIELDS
if additional_fields
fields += additional_fields
end

query_params = URI::Params.new({
"$select" => fields.join(","),
"$orderby" => "displayName",
"$search" => filter_param,
"$top" => "999",
"$filter" => filter_param,
"$top" => limit.to_s,
}.compact.transform_values { |val| [val] })

# This is required to do searching
Expand Down
9 changes: 5 additions & 4 deletions src/users.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module Office365::Users
SELECT_FIELDS = %w(id userPrincipalName surname preferredLanguage officeLocation mobilePhone mail jobTitle givenName displayName businessPhones accountEnabled mailNickname)

def list_users_request(q : String? = nil, limit : Int32? = nil, filter : String? = nil, additional_fields : Array(String)? = nil)
if q
if q && q.presence
queries = q.split(" ")
filter_params = [] of String

Expand All @@ -69,9 +69,10 @@ module Office365::Users
end

query_params = URI::Params.new({
"$select" => fields.join(","),
"$filter" => filter_param,
"$top" => limit,
"$select" => fields.join(","),
"$orderby" => "displayName",
"$filter" => filter_param,
"$top" => limit,
}.compact.transform_values { |val| [val] })

graph_http_request("GET", "#{USERS_BASE}", query: query_params)
Expand Down

0 comments on commit a923f55

Please sign in to comment.