From 7721de26651dfa610f798de9cab9b7a7c5438c5e Mon Sep 17 00:00:00 2001 From: Justin Salmon Date: Fri, 11 Dec 2020 14:39:03 +0000 Subject: [PATCH] feat: add support for listing organizations --- fixture/GET/organizations.json | 39 ++++++++++++++++++++++++++++++++++ zendesk/mock/client.go | 16 ++++++++++++++ zendesk/organization.go | 39 ++++++++++++++++++++++++++++++++++ zendesk/organization_test.go | 15 +++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 fixture/GET/organizations.json diff --git a/fixture/GET/organizations.json b/fixture/GET/organizations.json new file mode 100644 index 00000000..784a1f8d --- /dev/null +++ b/fixture/GET/organizations.json @@ -0,0 +1,39 @@ +[ + { + "organization": { + "url": "https://example.zendesk.com/api/v2/organizations/361898904439.json", + "id": 361898904439, + "name": "Rebel Alliance", + "shared_tickets": true, + "shared_comments": true, + "created_at": "2019-09-17T21:22:18Z", + "updated_at": "2019-09-17T21:22:18Z", + "domain_names": [ + "hoth.com", + "dantooine.com" + ], + "group_id": null, + "tags": [ + "test" + ] + } + }, + { + "organization": { + "url": "https://example.zendesk.com/api/v2/organizations/361898904440.json", + "id": 361898904440, + "name": "Imperial Senate", + "shared_tickets": true, + "shared_comments": true, + "created_at": "2019-09-17T21:22:18Z", + "updated_at": "2019-09-17T21:22:18Z", + "domain_names": [ + "coruscant.com" + ], + "group_id": null, + "tags": [ + "test" + ] + } + } +] \ No newline at end of file diff --git a/zendesk/mock/client.go b/zendesk/mock/client.go index aebf7e4b..edc004d3 100644 --- a/zendesk/mock/client.go +++ b/zendesk/mock/client.go @@ -597,6 +597,22 @@ func (mr *ClientMockRecorder) GetOrganizationTags(arg0, arg1 interface{}) *gomoc return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOrganizationTags", reflect.TypeOf((*Client)(nil).GetOrganizationTags), arg0, arg1) } +// GetOrganizations mocks base method. +func (m *Client) GetOrganizations(arg0 context.Context, arg1 *zendesk.OrganizationListOptions) ([]zendesk.Organization, zendesk.Page, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetOrganizations", arg0, arg1) + ret0, _ := ret[0].([]zendesk.Organization) + ret1, _ := ret[1].(zendesk.Page) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// GetOrganizations indicates an expected call of GetOrganizations. +func (mr *ClientMockRecorder) GetOrganizations(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOrganizations", reflect.TypeOf((*Client)(nil).GetOrganizations), arg0, arg1) +} + // GetSLAPolicies mocks base method. func (m *Client) GetSLAPolicies(arg0 context.Context, arg1 *zendesk.SLAPolicyListOptions) ([]zendesk.SLAPolicy, zendesk.Page, error) { m.ctrl.T.Helper() diff --git a/zendesk/organization.go b/zendesk/organization.go index d9905bfa..3fe1736a 100644 --- a/zendesk/organization.go +++ b/zendesk/organization.go @@ -23,14 +23,53 @@ type Organization struct { OrganizationFields map[string]interface{} `json:"organization_fields,omitempty"` } +// OrganizationListOptions is options for GetOrganizations +// +// ref: https://developer.zendesk.com/rest_api/docs/support/organizations#list-organizations +type OrganizationListOptions struct { + PageOptions +} + // OrganizationAPI an interface containing all methods associated with zendesk organizations type OrganizationAPI interface { + GetOrganizations(ctx context.Context, opts *OrganizationListOptions) ([]Organization, Page, error) CreateOrganization(ctx context.Context, org Organization) (Organization, error) GetOrganization(ctx context.Context, orgID int64) (Organization, error) UpdateOrganization(ctx context.Context, orgID int64, org Organization) (Organization, error) DeleteOrganization(ctx context.Context, orgID int64) error } +// GetOrganizations fetch organization list +// +// ref: https://developer.zendesk.com/rest_api/docs/support/organizations#getting-organizations +func (z *Client) GetOrganizations(ctx context.Context, opts *OrganizationListOptions) ([]Organization, Page, error) { + var data struct { + Organizations []Organization `json:"organizations"` + Page + } + + if opts == nil { + return []Organization{}, Page{}, &OptionsError{opts} + } + + u, err := addOptions("/organizations.json", opts) + if err != nil { + return []Organization{}, Page{}, err + } + + body, err := z.get(ctx, u) + if err != nil { + return []Organization{}, Page{}, err + } + + err = json.Unmarshal(body, &data) + if err != nil { + return []Organization{}, Page{}, err + } + + return data.Organizations, data.Page, nil +} + // CreateOrganization creates new organization // https://developer.zendesk.com/rest_api/docs/support/organizations#create-organization func (z *Client) CreateOrganization(ctx context.Context, org Organization) (Organization, error) { diff --git a/zendesk/organization_test.go b/zendesk/organization_test.go index e2ee85c7..b2e154c2 100644 --- a/zendesk/organization_test.go +++ b/zendesk/organization_test.go @@ -33,6 +33,21 @@ func TestGetOrganization(t *testing.T) { } } +func TestGetOrganizations(t *testing.T) { + mockAPI := newMockAPI(http.MethodGet, "organizations.json") + client := newTestClient(mockAPI) + defer mockAPI.Close() + + orgs, _, err := client.GetOrganizations(ctx, &OrganizationListOptions{}) + if err != nil { + t.Fatalf("Failed to get organizations: %s", err) + } + + if len(orgs) != 3 { + t.Fatalf("expected length of organizationss is , but got %d", len(orgs)) + } +} + func TestUpdateOrganization(t *testing.T) { mockAPI := newMockAPIWithStatus(http.MethodPut, "organization.json", http.StatusOK) client := newTestClient(mockAPI)