Skip to content

Commit

Permalink
Add custom organization fields (list and create) methods
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloromolini committed Feb 23, 2023
1 parent b3a7d08 commit 2e654ad
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 0 deletions.
58 changes: 58 additions & 0 deletions fixture/GET/organization_fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"organization_fields": [
{
"id": 1110988024701,
"url": "https://example.zendesk.com/api/v2/organization_fields/1110988024701.json",
"title": "Title",
"type": "lookup",
"relationship_target_type": "zen:user",
"relationship_filter": {
"all": [
{
"field": "role",
"operator": "is",
"value": "4"
},
{
"field": "tags",
"operator": "includes",
"value": "goofy"
}
],
"any": [
{
"field": "role",
"operator": "is_not",
"value": "0"
}
]
},
"active": true,
"description": "Test description",
"key": "test_key",
"raw_description": "This is just at test description",
"raw_title": "Raw test title",
"created_at": "2023-02-02T15:36:25Z",
"updated_at": "2023-02-23T10:24:49Z"
},
{
"id": 9170294642017,
"url": "https://example.zendesk.com/api/v2/organization_fields/9170294642017.json",
"title": "External Test ID",
"type": "text",
"relationship_target_type": "",
"relationship_filter": {
"all": null,
"any": null
},
"active": true,
"description": "This field contains an external testing ID",
"key": "test_external_id",
"position": 1,
"raw_description": "This field contains an external testing ID",
"raw_title": "External testing ID",
"created_at": "2023-02-06T14:43:05Z",
"updated_at": "2023-02-06T14:43:05Z"
}
]
}
38 changes: 38 additions & 0 deletions fixture/POST/organization_fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"organization_field":
{
"id": 1110988024701,
"url": "https://example.zendesk.com/api/v2/organization_fields/1110988024701.json",
"title": "Title",
"type": "lookup",
"relationship_target_type": "zen:user",
"relationship_filter": {
"all": [
{
"field": "role",
"operator": "is",
"value": "4"
},
{
"field": "tags",
"operator": "includes",
"value": "goofy"
}
],
"any": [
{
"field": "role",
"operator": "is_not",
"value": "0"
}
]
},
"active": true,
"description": "Test description",
"key": "test_key",
"raw_description": "This is just at test description",
"raw_title": "Raw test title",
"created_at": "2023-02-02T15:36:25Z",
"updated_at": "2023-02-23T10:24:49Z"
}
}
12 changes: 12 additions & 0 deletions zendesk/custom_field_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ type CustomFieldOption struct {
URL string `json:"url,omitempty"`
Value string `json:"value"`
}

type relationshipFilterObject struct {
Field string `json:"field"`
Operator string `json:"operator"`
Value string `json:"value"`
}

// RelationshipFilter is struct for value of `relationship_filter`
type RelationshipFilter struct {
All []relationshipFilterObject `json:"all"`
Any []relationshipFilterObject `json:"any"`
}
75 changes: 75 additions & 0 deletions zendesk/organization_field.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package zendesk

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

// OrganizationField represents the Organization Custom field structure
type OrganizationField struct {
ID int64 `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Title string `json:"title"`
Type string `json:"type"`
RelationshipTargetType string `json:"relationship_target_type"`
RelationshipFilter RelationshipFilter `json:"relationship_filter"`
Active bool `json:"active,omitempty"`
CustomFieldOptions []CustomFieldOption `json:"custom_field_options,omitempty"`
Description string `json:"description,omitempty"`
Key string `json:"key"`
Position int64 `json:"position,omitempty"`
RawDescription string `json:"raw_description,omitempty"`
RawTitle string `json:"raw_title,omitempty"`
RegexpForValidation string `json:"regexp_for_validation,omitempty"`
System bool `json:"system,omitempty"`
Tag string `json:"tag,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}

// OrganizationFieldAPI an interface containing all the organization field related zendesk methods
type OrganizationFieldAPI interface {
GetOrganizationFields(ctx context.Context) ([]OrganizationField, Page, error)
CreateOrganizationField(ctx context.Context, organizationField OrganizationField) (OrganizationField, error)
}

// GetOrganizationFields fetches organization field list
// ref: https://developer.zendesk.com/api-reference/ticketing/organizations/organization_fields/#list-organization-fields
func (z *Client) GetOrganizationFields(ctx context.Context) ([]OrganizationField, Page, error) {
var data struct {
OrganizationFields []OrganizationField `json:"organization_fields"`
Page
}

body, err := z.get(ctx, "/organization_fields.json")
if err != nil {
return []OrganizationField{}, Page{}, err
}

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

// CreateOrganizationField creates new organization field
// ref: https://developer.zendesk.com/api-reference/ticketing/organizations/organization_fields/#create-organization-field
func (z *Client) CreateOrganizationField(ctx context.Context, organizationField OrganizationField) (OrganizationField, error) {
var data, result struct {
OrganizationField OrganizationField `json:"organization_field"`
}
data.OrganizationField = organizationField

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

err = json.Unmarshal(body, &result)
if err != nil {
return OrganizationField{}, err
}
return result.OrganizationField, nil
}
32 changes: 32 additions & 0 deletions zendesk/organization_field_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package zendesk

import (
"net/http"
"testing"
)

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

ticketFields, _, err := client.GetOrganizationFields(ctx)
if err != nil {
t.Fatalf("Failed to get organization fields: %s", err)
}

if len(ticketFields) != 2 {
t.Fatalf("expected length of organization fields is , but got %d", len(ticketFields))
}
}

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

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

0 comments on commit 2e654ad

Please sign in to comment.