Skip to content

Commit

Permalink
add GetUsersByGroupID
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Romolini <paolo.romolini@enterprisedb.com>
  • Loading branch information
paoloromolini committed Oct 27, 2023
1 parent 780ad86 commit 7246525
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 1 deletion.
72 changes: 72 additions & 0 deletions fixture/GET/users_by_group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"users": [
{
"id": 369537351454,
"url": "https://example.zendesk.com/api/v2/users/369537351454.json",
"name": "nukosuke",
"email": "nukosuke@lavabit.com",
"created_at": "2018-11-23T16:05:12Z",
"updated_at": "2018-11-30T19:12:34Z",
"time_zone": "Tokyo",
"iana_time_zone": "Asia/Tokyo",
"phone": null,
"shared_phone_number": null,
"photo": {
"url": "https://example.zendesk.com/api/v2/attachments/360254032333.json",
"id": 360254032333,
"file_name": "profile_image_369537351454_9042965.png",
"content_url": "https://example.zendesk.com/system/photos/3602/5403/2333/profile_image_369537351454_9042965.png",
"mapped_content_url": "https://example.zendesk.com/system/photos/3602/5403/2333/profile_image_369537351454_9042965.png",
"content_type": "image/png",
"size": 4296,
"width": 80,
"height": 80,
"inline": false,
"thumbnails": [
{
"url": "https://example.zendesk.com/api/v2/attachments/360254032353.json",
"id": 360254032353,
"file_name": "profile_image_369537351454_9042965_thumb.png",
"content_url": "https://example.zendesk.com/system/photos/3602/5403/2333/profile_image_369537351454_9042965_thumb.png",
"mapped_content_url": "https://example.zendesk.com/system/photos/3602/5403/2333/profile_image_369537351454_9042965_thumb.png",
"content_type": "image/png",
"size": 1097,
"width": 32,
"height": 32,
"inline": false
}
]
},
"locale_id": 1,
"locale": "en-US",
"organization_id": 360091290114,
"role": "admin",
"verified": true,
"external_id": null,
"tags": [],
"alias": null,
"active": true,
"shared": false,
"shared_agent": false,
"last_login_at": "2018-11-30T19:12:34Z",
"two_factor_auth_enabled": true,
"signature": null,
"details": null,
"notes": null,
"role_type": null,
"custom_role_id": null,
"moderator": true,
"ticket_restriction": null,
"only_private_comments": false,
"restricted_agent": false,
"suspended": false,
"chat_only": false,
"default_group_id": 360002440594,
"report_csv": true,
"user_fields": {}
}
],
"next_page": null,
"previous_page": null,
"count": 1
}
16 changes: 16 additions & 0 deletions zendesk/mock/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion zendesk/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ type UserAPI interface {
SearchUsers(ctx context.Context, opts *SearchUsersOptions) ([]User, Page, error)
GetManyUsers(ctx context.Context, opts *GetManyUsersOptions) ([]User, Page, error)
GetUsers(ctx context.Context, opts *UserListOptions) ([]User, Page, error)
GetUsersByGroupID(ctx context.Context, groupID int64, opts *UserListOptions) ([]User, Page, error)
GetOrganizationUsers(ctx context.Context, orgID int64, opts *UserListOptions) ([]User, Page, error)
GetUser(ctx context.Context, userID int64) (User, error)
CreateUser(ctx context.Context, user User) (User, error)
Expand Down Expand Up @@ -256,7 +257,35 @@ func (z *Client) GetManyUsers(ctx context.Context, opts *GetManyUsersOptions) ([
return data.Users, data.Page, nil
}

//TODO: GetUsersByGroupID, GetUsersByOrganizationID
// GetUsersByGroupID get users by group ID
// ref: https://developer.zendesk.com/api-reference/ticketing/users/users/#list-users
func (z *Client) GetUsersByGroupID(ctx context.Context, groupID int64, opts *UserListOptions) ([]User, Page, error) {
var data struct {
Users []User `json:"users"`
Page
}

tmp := opts
if tmp == nil {
tmp = &UserListOptions{}
}

u, err := addOptions(fmt.Sprintf("/groups/%d/users.json", groupID), tmp)
if err != nil {
return nil, Page{}, err
}

body, err := z.get(ctx, u)
if err != nil {
return nil, Page{}, err
}

err = json.Unmarshal(body, &data)
if err != nil {
return nil, Page{}, err
}
return data.Users, data.Page, nil
}

// CreateUser creates new user
// ref: https://developer.zendesk.com/api-reference/ticketing/users/users/#create-user
Expand Down
15 changes: 15 additions & 0 deletions zendesk/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ func TestGetUsers(t *testing.T) {
}
}

func TestGetUsersByGroupID(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "users_by_group.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()

users, _, err := client.GetUsersByGroupID(ctx, 360002440594, nil)
if err != nil {
t.Fatalf("Failed to get users: %s", err)
}

if len(users) != 1 {
t.Fatalf("expected length of users is 1, but got %d", len(users))
}
}

func TestGetOrganizationUsers(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "users.json")
client := newTestClient(mockAPI)
Expand Down

0 comments on commit 7246525

Please sign in to comment.