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

feat: add support for listing organizations #187

Merged
merged 1 commit into from
Dec 12, 2020
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
37 changes: 37 additions & 0 deletions fixture/GET/organizations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"organizations": [
{
"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"
]
},
{
"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) != 2 {
t.Fatalf("expected length of organizations is , but got %d", len(orgs))
}
}

func TestUpdateOrganization(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodPut, "organization.json", http.StatusOK)
client := newTestClient(mockAPI)
Expand Down