Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list users by Organization #266

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

33 changes: 33 additions & 0 deletions 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)
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)
CreateOrUpdateUser(ctx context.Context, user User) (User, error)
Expand Down Expand Up @@ -153,6 +154,38 @@ func (z *Client) GetUsers(ctx context.Context, opts *UserListOptions) ([]User, P
return data.Users, data.Page, nil
}

// GetOrganizationUsers fetch organization users list
// https://developer.zendesk.com/api-reference/ticketing/users/users/#list-users
// /api/v2/organizations/{organization_id}/users
func (z *Client) GetOrganizationUsers(ctx context.Context, orgID int64, opts *UserListOptions) ([]User, Page, error) {
var data struct {
Users []User `json:"users"`
Page
}

tmp := opts
if tmp == nil {
tmp = &UserListOptions{}
}
apiURL := fmt.Sprintf("/organizations/%d/users.json", orgID)

u, err := addOptions(apiURL, 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
}

// SearchUsers Returns an array of users who meet the search criteria.
// https://developer.zendesk.com/api-reference/ticketing/users/users/#search-users
func (z *Client) SearchUsers(ctx context.Context, opts *SearchUsersOptions) ([]User, Page, error) {
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 TestGetOrganizationUsers(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "users.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()

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

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

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