Skip to content

Commit 243fc73

Browse files
author
nukosuke
authored
Merge pull request #266 from paoloromolini/organization_users
Add list users by Organization
2 parents da6c30a + e164d39 commit 243fc73

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

zendesk/mock/client.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zendesk/user.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ type UserAPI interface {
116116
SearchUsers(ctx context.Context, opts *SearchUsersOptions) ([]User, Page, error)
117117
GetManyUsers(ctx context.Context, opts *GetManyUsersOptions) ([]User, Page, error)
118118
GetUsers(ctx context.Context, opts *UserListOptions) ([]User, Page, error)
119+
GetOrganizationUsers(ctx context.Context, orgID int64, opts *UserListOptions) ([]User, Page, error)
119120
GetUser(ctx context.Context, userID int64) (User, error)
120121
CreateUser(ctx context.Context, user User) (User, error)
121122
CreateOrUpdateUser(ctx context.Context, user User) (User, error)
@@ -153,6 +154,38 @@ func (z *Client) GetUsers(ctx context.Context, opts *UserListOptions) ([]User, P
153154
return data.Users, data.Page, nil
154155
}
155156

157+
// GetOrganizationUsers fetch organization users list
158+
// https://developer.zendesk.com/api-reference/ticketing/users/users/#list-users
159+
// /api/v2/organizations/{organization_id}/users
160+
func (z *Client) GetOrganizationUsers(ctx context.Context, orgID int64, opts *UserListOptions) ([]User, Page, error) {
161+
var data struct {
162+
Users []User `json:"users"`
163+
Page
164+
}
165+
166+
tmp := opts
167+
if tmp == nil {
168+
tmp = &UserListOptions{}
169+
}
170+
apiURL := fmt.Sprintf("/organizations/%d/users.json", orgID)
171+
172+
u, err := addOptions(apiURL, tmp)
173+
if err != nil {
174+
return nil, Page{}, err
175+
}
176+
177+
body, err := z.get(ctx, u)
178+
if err != nil {
179+
return nil, Page{}, err
180+
}
181+
182+
err = json.Unmarshal(body, &data)
183+
if err != nil {
184+
return nil, Page{}, err
185+
}
186+
return data.Users, data.Page, nil
187+
}
188+
156189
// SearchUsers Returns an array of users who meet the search criteria.
157190
// https://developer.zendesk.com/api-reference/ticketing/users/users/#search-users
158191
func (z *Client) SearchUsers(ctx context.Context, opts *SearchUsersOptions) ([]User, Page, error) {

zendesk/user_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ func TestGetUsers(t *testing.T) {
3030
}
3131
}
3232

33+
func TestGetOrganizationUsers(t *testing.T) {
34+
mockAPI := newMockAPI(http.MethodGet, "users.json")
35+
client := newTestClient(mockAPI)
36+
defer mockAPI.Close()
37+
38+
users, _, err := client.GetOrganizationUsers(ctx, 1000006909040, nil)
39+
if err != nil {
40+
t.Fatalf("Failed to get users: %s", err)
41+
}
42+
43+
if len(users) != 2 {
44+
t.Fatalf("expected length of users is 2, but got %d", len(users))
45+
}
46+
}
47+
3348
func TestGetManyUsers(t *testing.T) {
3449
mockAPI := newMockAPI(http.MethodGet, "users.json")
3550
client := newTestClient(mockAPI)

0 commit comments

Comments
 (0)