Skip to content

Commit

Permalink
Merge pull request #138 from jlsalmon/resource/organization
Browse files Browse the repository at this point in the history
Organization support
  • Loading branch information
nukosuke committed Nov 4, 2019
2 parents 8c46bb8 + 320981f commit e2af82d
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 0 deletions.
19 changes: 19 additions & 0 deletions fixture/GET/organization.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"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"
]
}
}
19 changes: 19 additions & 0 deletions fixture/POST/organization.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"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"
]
}
}
19 changes: 19 additions & 0 deletions fixture/PUT/organization.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"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"
]
}
}
1 change: 1 addition & 0 deletions zendesk/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type API interface {
TriggerAPI
TargetAPI
UserAPI
OrganizationAPI
}

var _ API = (*Client)(nil)
59 changes: 59 additions & 0 deletions zendesk/mock/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions zendesk/organization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package zendesk

import (
"context"
"encoding/json"
"fmt"
"time"
)

// Organization is struct for organization payload
// https://developer.zendesk.com/rest_api/docs/support/organizations
type Organization struct {
ID int64 `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Name string `json:"name"`
DomainNames []string `json:"domain_names"`
GroupID int64 `json:"group_id"`
SharedTickets bool `json:"shared_tickets"`
SharedComments bool `json:"shared_comments"`
Tags []string `json:"tags"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}

// OrganizationAPI an interface containing all methods associated with zendesk organizations
type OrganizationAPI interface {
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
}

// 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) {
var data, result struct {
Organization Organization `json:"organization"`
}

data.Organization = org

body, err := z.post(ctx, "/organizations.json", data)
if err != nil {
return Organization{}, err
}

err = json.Unmarshal(body, &result)
if err != nil {
return Organization{}, err
}

return result.Organization, nil
}

// GetOrganization gets a specified organization
// ref: https://developer.zendesk.com/rest_api/docs/support/organizations#show-organization
func (z *Client) GetOrganization(ctx context.Context, orgID int64) (Organization, error) {
var result struct {
Organization Organization `json:"organization"`
}

body, err := z.get(ctx, fmt.Sprintf("/organizations/%d.json", orgID))

if err != nil {
return Organization{}, err
}

err = json.Unmarshal(body, &result)
if err != nil {
return Organization{}, err
}

return result.Organization, err
}

// UpdateOrganization updates a organization with the specified organization
// ref: https://developer.zendesk.com/rest_api/docs/support/organizations#update-organization
func (z *Client) UpdateOrganization(ctx context.Context, orgID int64, org Organization) (Organization, error) {
var result, data struct {
Organization Organization `json:"organization"`
}

data.Organization = org

body, err := z.put(ctx, fmt.Sprintf("/organizations/%d.json", orgID), data)

if err != nil {
return Organization{}, err
}

err = json.Unmarshal(body, &result)
if err != nil {
return Organization{}, err
}

return result.Organization, err
}

// DeleteOrganization deletes the specified organization
// ref: https://developer.zendesk.com/rest_api/docs/support/organizations#delete-organization
func (z *Client) DeleteOrganization(ctx context.Context, orgID int64) error {
err := z.delete(ctx, fmt.Sprintf("/organizations/%d.json", orgID))

if err != nil {
return err
}

return nil
}
63 changes: 63 additions & 0 deletions zendesk/organization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package zendesk

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestCreateOrganization(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodPost, "organization.json", http.StatusCreated)
client := newTestClient(mockAPI)
defer mockAPI.Close()

_, err := client.CreateOrganization(ctx, Organization{})
if err != nil {
t.Fatalf("Failed to send request to create organization: %s", err)
}
}

func TestGetOrganization(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "organization.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()

org, err := client.GetOrganization(ctx, 123)
if err != nil {
t.Fatalf("Failed to get organization: %s", err)
}

expectedID := int64(361898904439)
if org.ID != expectedID {
t.Fatalf("Returned organization does not have the expected ID %d. Organization ID is %d", expectedID, org.ID)
}
}

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

updatedOrg, err := client.UpdateOrganization(ctx, int64(1234), Organization{})
if err != nil {
t.Fatalf("Failed to send request to create organization: %s", err)
}

expectedID := int64(361898904439)
if updatedOrg.ID != expectedID {
t.Fatalf("Updated organization %v did not have expected id %d", updatedOrg, expectedID)
}
}

func TestDeleteOrganization(t *testing.T) {
mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
w.Write(nil)
}))

c := newTestClient(mockAPI)
err := c.DeleteOrganization(ctx, 1234)
if err != nil {
t.Fatalf("Failed to delete organization: %s", err)
}
}

0 comments on commit e2af82d

Please sign in to comment.