Skip to content

Commit

Permalink
Merge pull request #144 from RWejlgaard/feature/automation-api
Browse files Browse the repository at this point in the history
Automation support
  • Loading branch information
nukosuke authored Nov 25, 2019
2 parents 6bfd3be + bd96400 commit ab8bbce
Show file tree
Hide file tree
Showing 8 changed files with 587 additions and 0 deletions.
33 changes: 33 additions & 0 deletions fixture/GET/automation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"automation": {
"url": "https://example.zendesk.com/api/v2/automations/360017421099.json",
"id": 360017421099,
"title": "Close ticket 4 days after status is set to solved",
"active": true,
"updated_at": "2019-09-17T21:22:20Z",
"created_at": "2019-09-17T21:22:20Z",
"actions": [
{
"field": "status",
"value": "closed"
}
],
"conditions": {
"all": [
{
"field": "status",
"operator": "is",
"value": "solved"
},
{
"field": "SOLVED",
"operator": "greater_than",
"value": "96"
}
],
"any": []
},
"position": 0,
"raw_title": "Close ticket 4 days after status is set to solved"
}
}
108 changes: 108 additions & 0 deletions fixture/GET/automations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"automations": [
{
"url": "https://example.zendesk.com/api/v2/automations/360017421099.json",
"id": 360017421099,
"title": "Close ticket 4 days after status is set to solved",
"active": true,
"updated_at": "2019-09-17T21:22:20Z",
"created_at": "2019-09-17T21:22:20Z",
"actions": [
{
"field": "status",
"value": "closed"
}
],
"conditions": {
"all": [
{
"field": "status",
"operator": "is",
"value": "solved"
},
{
"field": "SOLVED",
"operator": "greater_than",
"value": "96"
}
],
"any": []
},
"position": 0,
"raw_title": "Close ticket 4 days after status is set to solved"
},
{
"url": "https://example.zendesk.com/api/v2/automations/360017421119.json",
"id": 360017421119,
"title": "Pending notification 24 hours",
"active": false,
"updated_at": "2019-09-17T21:22:20Z",
"created_at": "2019-09-17T21:22:20Z",
"actions": [
{
"field": "notification_user",
"value": [
"requester_and_ccs",
"[{{ticket.account}}] Pending request: {{ticket.title}}",
"This is an email to remind you that your request (#{{ticket.id}}) is pending and awaits your feedback.\n\n{{ticket.comments_formatted}}"
]
}
],
"conditions": {
"all": [
{
"field": "PENDING",
"operator": "is",
"value": "24"
},
{
"field": "ticket_is_public",
"operator": "is",
"value": "public"
}
],
"any": []
},
"position": 9998,
"raw_title": "Pending notification 24 hours"
},
{
"url": "https://example.zendesk.com/api/v2/automations/360017421139.json",
"id": 360017421139,
"title": "Pending notification 5 days",
"active": false,
"updated_at": "2019-09-17T21:22:20Z",
"created_at": "2019-09-17T21:22:20Z",
"actions": [
{
"field": "notification_user",
"value": [
"requester_and_ccs",
"[{{ticket.account}}] Pending request: {{ticket.title}}",
"This is an email to remind you that your request (#{{ticket.id}}) has been pending for 5 days and awaits your feedback.\n\n{{ticket.comments_formatted}}"
]
}
],
"conditions": {
"all": [
{
"field": "PENDING",
"operator": "is",
"value": "120"
},
{
"field": "ticket_is_public",
"operator": "is",
"value": "public"
}
],
"any": []
},
"position": 9999,
"raw_title": "Pending notification 5 days"
}
],
"next_page": null,
"previous_page": null,
"count": 3
}
33 changes: 33 additions & 0 deletions fixture/POST/automations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"automation": {
"url": "https://example.zendesk.com/api/v2/automations/360017421099.json",
"id": 360017421099,
"title": "Close ticket 4 days after status is set to solved",
"active": true,
"updated_at": "2019-09-17T21:22:20Z",
"created_at": "2019-09-17T21:22:20Z",
"actions": [
{
"field": "status",
"value": "closed"
}
],
"conditions": {
"all": [
{
"field": "status",
"operator": "is",
"value": "solved"
},
{
"field": "SOLVED",
"operator": "greater_than",
"value": "96"
}
],
"any": []
},
"position": 0,
"raw_title": "Close ticket 4 days after status is set to solved"
}
}
33 changes: 33 additions & 0 deletions fixture/PUT/automations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"automation": {
"url": "https://example.zendesk.com/api/v2/automations/360017421099.json",
"id": 360017421099,
"title": "Close ticket 4 days after status is set to solved",
"active": true,
"updated_at": "2019-09-17T21:22:20Z",
"created_at": "2019-09-17T21:22:20Z",
"actions": [
{
"field": "status",
"value": "closed"
}
],
"conditions": {
"all": [
{
"field": "status",
"operator": "is",
"value": "solved"
},
{
"field": "SOLVED",
"operator": "greater_than",
"value": "96"
}
],
"any": []
},
"position": 0,
"raw_title": "Close ticket 4 days after status is set to solved"
}
}
1 change: 1 addition & 0 deletions zendesk/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package zendesk

// API an interface containing all of the zendesk client methods
type API interface {
AutomationAPI
AttachmentAPI
BrandAPI
DynamicContentAPI
Expand Down
172 changes: 172 additions & 0 deletions zendesk/automation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package zendesk

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

// AutomationCondition zendesk automation condition
//
// ref: https://developer.zendesk.com/rest_api/docs/core/automations#conditions-reference
type AutomationCondition struct {
Field string `json:"field"`
Operator string `json:"operator"`
Value string `json:"value"`
}

// AutomationAction is zendesk automation action
//
// ref: https://developer.zendesk.com/rest_api/docs/core/automations#actions
type AutomationAction struct {
Field string `json:"field"`
Value interface{} `json:"value"`
}

// Automation is zendesk automation JSON payload format
//
// ref: https://developer.zendesk.com/rest_api/docs/core/automations#json-format
type Automation struct {
ID int64 `json:"id,omitempty"`
Title string `json:"title"`
Active bool `json:"active,omitempty"`
Position int64 `json:"position,omitempty"`
Conditions struct {
All []AutomationCondition `json:"all"`
Any []AutomationCondition `json:"any"`
} `json:"conditions"`
Actions []AutomationAction `json:"actions"`
RawTitle string `json:"raw_title,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}

// AutomationListOptions is options for GetAutomations
//
// ref: https://developer.zendesk.com/rest_api/docs/support/automations#list-automations
type AutomationListOptions struct {
PageOptions
Active bool `url:"active,omitempty"`
SortBy string `url:"sort_by,omitempty"`
SortOrder string `url:"sort_order,omitempty"`
}

// AutomationAPI an interface containing all automation related methods
type AutomationAPI interface {
GetAutomations(ctx context.Context, opts *AutomationListOptions) ([]Automation, Page, error)
CreateAutomation(ctx context.Context, automation Automation) (Automation, error)
GetAutomation(ctx context.Context, id int64) (Automation, error)
UpdateAutomation(ctx context.Context, id int64, automation Automation) (Automation, error)
DeleteAutomation(ctx context.Context, id int64) error
}

// GetAutomations fetch automation list
//
// ref: https://developer.zendesk.com/rest_api/docs/support/automations#getting-automations
func (z *Client) GetAutomations(ctx context.Context, opts *AutomationListOptions) ([]Automation, Page, error) {
var data struct {
Automations []Automation `json:"automations"`
Page
}

if opts == nil {
return []Automation{}, Page{}, &OptionsError{opts}
}

u, err := addOptions("/automations.json", opts)
if err != nil {
return []Automation{}, Page{}, err
}

body, err := z.get(ctx, u)
if err != nil {
return []Automation{}, Page{}, err
}

err = json.Unmarshal(body, &data)
if err != nil {
return []Automation{}, Page{}, err
}

return data.Automations, data.Page, nil
}

// CreateAutomation creates new automation
//
// ref: https://developer.zendesk.com/rest_api/docs/support/automations#create-automation
func (z *Client) CreateAutomation(ctx context.Context, automation Automation) (Automation, error) {
var data, result struct {
Automation Automation `json:"automation"`
}

data.Automation = automation
body, err := z.post(ctx, "/automations.json", data)

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

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

return result.Automation, nil
}

// GetAutomation returns the specified automation
//
// ref: https://developer.zendesk.com/rest_api/docs/support/automations#getting-automations
func (z *Client) GetAutomation(ctx context.Context, id int64) (Automation, error) {
var result struct {
Automation Automation `json:"automation"`
}

body, err := z.get(ctx, fmt.Sprintf("/automations/%d.json", id))
if err != nil {
return Automation{}, err
}

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

return result.Automation, nil
}

// UpdateAutomation updates the specified automation and returns the updated one
//
// ref: https://developer.zendesk.com/rest_api/docs/support/automations#update-automation
func (z *Client) UpdateAutomation(ctx context.Context, id int64, automation Automation) (Automation, error) {
var data, result struct {
Automation Automation `json:"automation"`
}

data.Automation = automation
body, err := z.put(ctx, fmt.Sprintf("/automations/%d.json", id), data)

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

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

return result.Automation, nil
}

// DeleteAutomation deletes the specified automation
//
// ref: https://developer.zendesk.com/rest_api/docs/support/automations#delete-automation
func (z *Client) DeleteAutomation(ctx context.Context, id int64) error {
err := z.delete(ctx, fmt.Sprintf("/automations/%d.json", id))
if err != nil {
return err
}

return nil
}
Loading

0 comments on commit ab8bbce

Please sign in to comment.