Skip to content

Commit

Permalink
feat: add support for listing organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
jlsalmon committed Dec 11, 2020
1 parent a409e37 commit 7721de2
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
39 changes: 39 additions & 0 deletions fixture/GET/organizations.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
]
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.

39 changes: 39 additions & 0 deletions zendesk/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions zendesk/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7721de2

Please sign in to comment.