Repository: nukosuke/go-zendesk
Pull request #266 (2023-02-28 13:21:29)

Add list users by Organization
Add function to fetch Users for an organization:

https://developer.zendesk.com/api-reference/ticketing/users/users/#list-users

GET /api/v2/organizations/{organization_id}/users



Patch:
diff --git a/zendesk/mock/client.go b/zendesk/mock/client.go
index 19f76d3..7c8100b 100644
--- a/zendesk/mock/client.go
+++ b/zendesk/mock/client.go
@@ -869,6 +869,22 @@ func (mr *ClientMockRecorder) GetOrganizationTags(arg0, arg1 interface{}) *gomoc
 	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOrganizationTags", reflect.TypeOf((*Client)(nil).GetOrganizationTags), arg0, arg1)
 }
 
+// GetOrganizationUsers mocks base method.
+func (m *Client) GetOrganizationUsers(arg0 context.Context, arg1 int64, arg2 *zendesk.UserListOptions) ([]zendesk.User, zendesk.Page, error) {
+	m.ctrl.T.Helper()
+	ret := m.ctrl.Call(m, "GetOrganizationUsers", arg0, arg1, arg2)
+	ret0, _ := ret[0].([]zendesk.User)
+	ret1, _ := ret[1].(zendesk.Page)
+	ret2, _ := ret[2].(error)
+	return ret0, ret1, ret2
+}
+
+// GetOrganizationUsers indicates an expected call of GetOrganizationUsers.
+func (mr *ClientMockRecorder) GetOrganizationUsers(arg0, arg1, arg2 interface{}) *gomock.Call {
+	mr.mock.ctrl.T.Helper()
+	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOrganizationUsers", reflect.TypeOf((*Client)(nil).GetOrganizationUsers), arg0, arg1, arg2)
+}
+
 // GetOrganizations mocks base method.
 func (m *Client) GetOrganizations(arg0 context.Context, arg1 *zendesk.OrganizationListOptions) ([]zendesk.Organization, zendesk.Page, error) {
 	m.ctrl.T.Helper()
diff --git a/zendesk/user.go b/zendesk/user.go
index ec16d6c..d297b2d 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)
+	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)
@@ -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) {


Test patch:
diff --git a/zendesk/user_test.go b/zendesk/user_test.go
index ad20426..20ccce0 100644
--- a/zendesk/user_test.go
+++ b/zendesk/user_test.go
@@ -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)