From f41acafafdd04f84df2d57e1de7171c34c195de4 Mon Sep 17 00:00:00 2001 From: Paolo Romolini Date: Thu, 5 Oct 2023 15:38:31 +0200 Subject: [PATCH] add GetUsersByGroupID Signed-off-by: Paolo Romolini --- fixture/GET/users_by_group.json | 72 +++++++++++++++++++++++++++++++++ zendesk/mock/client.go | 17 ++++++++ zendesk/user.go | 31 +++++++++++++- zendesk/user_test.go | 15 +++++++ 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 fixture/GET/users_by_group.json diff --git a/fixture/GET/users_by_group.json b/fixture/GET/users_by_group.json new file mode 100644 index 00000000..90fe71e7 --- /dev/null +++ b/fixture/GET/users_by_group.json @@ -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 +} \ No newline at end of file diff --git a/zendesk/mock/client.go b/zendesk/mock/client.go index fe8720fb..2dcd78b2 100644 --- a/zendesk/mock/client.go +++ b/zendesk/mock/client.go @@ -2258,6 +2258,7 @@ func (mr *ClientMockRecorder) GetUsers(ctx, opts any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUsers", reflect.TypeOf((*Client)(nil).GetUsers), ctx, opts) } +<<<<<<< HEAD // GetUsersCBP mocks base method. func (m *Client) GetUsersCBP(ctx context.Context, opts *zendesk.CBPOptions) ([]zendesk.User, zendesk.CursorPaginationMeta, error) { m.ctrl.T.Helper() @@ -2298,12 +2299,28 @@ func (m *Client) GetUsersOBP(ctx context.Context, opts *zendesk.OBPOptions) ([]z return ret0, ret1, ret2 } +// GetUsersByGroupID mocks base method. +func (m *Client) GetUsersByGroupID(arg0 context.Context, arg1 int64, arg2 *zendesk.UserListOptions) ([]zendesk.User, zendesk.Page, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetUsersByGroupID", arg0, arg1, arg2) + ret0, _ := ret[0].([]zendesk.User) + ret1, _ := ret[1].(zendesk.Page) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + // GetUsersOBP indicates an expected call of GetUsersOBP. func (mr *ClientMockRecorder) GetUsersOBP(ctx, opts any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUsersOBP", reflect.TypeOf((*Client)(nil).GetUsersOBP), ctx, opts) } +// GetUsersByGroupID indicates an expected call of GetUsersByGroupID. +func (mr *ClientMockRecorder) GetUsersByGroupID(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUsersByGroupID", reflect.TypeOf((*Client)(nil).GetUsersByGroupID), arg0, arg1, arg2) +} + // GetView mocks base method. func (m *Client) GetView(arg0 context.Context, arg1 int64) (zendesk.View, error) { m.ctrl.T.Helper() diff --git a/zendesk/user.go b/zendesk/user.go index 7dacf0e8..0a2fa020 100644 --- a/zendesk/user.go +++ b/zendesk/user.go @@ -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) @@ -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 diff --git a/zendesk/user_test.go b/zendesk/user_test.go index 20ccce0b..60a46918 100644 --- a/zendesk/user_test.go +++ b/zendesk/user_test.go @@ -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)