Skip to content

Commit 8532250

Browse files
author
nukosuke
authored
Merge pull request #265 from paoloromolini/organization_field
Add custom organization fields (list and create) methods
2 parents 243fc73 + 47df59f commit 8532250

File tree

7 files changed

+247
-0
lines changed

7 files changed

+247
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"organization_fields": [
3+
{
4+
"id": 1110988024701,
5+
"url": "https://example.zendesk.com/api/v2/organization_fields/1110988024701.json",
6+
"title": "Title",
7+
"type": "lookup",
8+
"relationship_target_type": "zen:user",
9+
"relationship_filter": {
10+
"all": [
11+
{
12+
"field": "role",
13+
"operator": "is",
14+
"value": "4"
15+
},
16+
{
17+
"field": "tags",
18+
"operator": "includes",
19+
"value": "goofy"
20+
}
21+
],
22+
"any": [
23+
{
24+
"field": "role",
25+
"operator": "is_not",
26+
"value": "0"
27+
}
28+
]
29+
},
30+
"active": true,
31+
"description": "Test description",
32+
"key": "test_key",
33+
"raw_description": "This is just at test description",
34+
"raw_title": "Raw test title",
35+
"created_at": "2023-02-02T15:36:25Z",
36+
"updated_at": "2023-02-23T10:24:49Z"
37+
},
38+
{
39+
"id": 9170294642017,
40+
"url": "https://example.zendesk.com/api/v2/organization_fields/9170294642017.json",
41+
"title": "External Test ID",
42+
"type": "text",
43+
"relationship_target_type": "",
44+
"relationship_filter": {
45+
"all": null,
46+
"any": null
47+
},
48+
"active": true,
49+
"description": "This field contains an external testing ID",
50+
"key": "test_external_id",
51+
"position": 1,
52+
"raw_description": "This field contains an external testing ID",
53+
"raw_title": "External testing ID",
54+
"created_at": "2023-02-06T14:43:05Z",
55+
"updated_at": "2023-02-06T14:43:05Z"
56+
}
57+
]
58+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"organization_field":
3+
{
4+
"id": 1110988024701,
5+
"url": "https://example.zendesk.com/api/v2/organization_fields/1110988024701.json",
6+
"title": "Title",
7+
"type": "lookup",
8+
"relationship_target_type": "zen:user",
9+
"relationship_filter": {
10+
"all": [
11+
{
12+
"field": "role",
13+
"operator": "is",
14+
"value": "4"
15+
},
16+
{
17+
"field": "tags",
18+
"operator": "includes",
19+
"value": "goofy"
20+
}
21+
],
22+
"any": [
23+
{
24+
"field": "role",
25+
"operator": "is_not",
26+
"value": "0"
27+
}
28+
]
29+
},
30+
"active": true,
31+
"description": "Test description",
32+
"key": "test_key",
33+
"raw_description": "This is just at test description",
34+
"raw_title": "Raw test title",
35+
"created_at": "2023-02-02T15:36:25Z",
36+
"updated_at": "2023-02-23T10:24:49Z"
37+
}
38+
}

zendesk/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type API interface {
1717
LocaleAPI
1818
MacroAPI
1919
OrganizationAPI
20+
OrganizationFieldAPI
2021
OrganizationMembershipAPI
2122
SearchAPI
2223
SLAPolicyAPI

zendesk/custom_field_option.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,15 @@ type CustomFieldOption struct {
99
URL string `json:"url,omitempty"`
1010
Value string `json:"value"`
1111
}
12+
13+
type relationshipFilterObject struct {
14+
Field string `json:"field"`
15+
Operator string `json:"operator"`
16+
Value string `json:"value"`
17+
}
18+
19+
// RelationshipFilter is struct for value of `relationship_filter`
20+
type RelationshipFilter struct {
21+
All []relationshipFilterObject `json:"all"`
22+
Any []relationshipFilterObject `json:"any"`
23+
}

zendesk/mock/client.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zendesk/organization_field.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package zendesk
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"time"
7+
)
8+
9+
// OrganizationField represents the Organization Custom field structure
10+
type OrganizationField struct {
11+
ID int64 `json:"id,omitempty"`
12+
URL string `json:"url,omitempty"`
13+
Title string `json:"title"`
14+
Type string `json:"type"`
15+
RelationshipTargetType string `json:"relationship_target_type"`
16+
RelationshipFilter RelationshipFilter `json:"relationship_filter"`
17+
Active bool `json:"active,omitempty"`
18+
CustomFieldOptions []CustomFieldOption `json:"custom_field_options,omitempty"`
19+
Description string `json:"description,omitempty"`
20+
Key string `json:"key"`
21+
Position int64 `json:"position,omitempty"`
22+
RawDescription string `json:"raw_description,omitempty"`
23+
RawTitle string `json:"raw_title,omitempty"`
24+
RegexpForValidation string `json:"regexp_for_validation,omitempty"`
25+
System bool `json:"system,omitempty"`
26+
Tag string `json:"tag,omitempty"`
27+
CreatedAt *time.Time `json:"created_at,omitempty"`
28+
UpdatedAt *time.Time `json:"updated_at,omitempty"`
29+
}
30+
31+
// OrganizationFieldAPI an interface containing all the organization field related zendesk methods
32+
type OrganizationFieldAPI interface {
33+
GetOrganizationFields(ctx context.Context) ([]OrganizationField, Page, error)
34+
CreateOrganizationField(ctx context.Context, organizationField OrganizationField) (OrganizationField, error)
35+
}
36+
37+
// GetOrganizationFields fetches organization field list
38+
// ref: https://developer.zendesk.com/api-reference/ticketing/organizations/organization_fields/#list-organization-fields
39+
func (z *Client) GetOrganizationFields(ctx context.Context) ([]OrganizationField, Page, error) {
40+
var data struct {
41+
OrganizationFields []OrganizationField `json:"organization_fields"`
42+
Page
43+
}
44+
45+
body, err := z.get(ctx, "/organization_fields.json")
46+
if err != nil {
47+
return []OrganizationField{}, Page{}, err
48+
}
49+
50+
err = json.Unmarshal(body, &data)
51+
if err != nil {
52+
return []OrganizationField{}, Page{}, err
53+
}
54+
return data.OrganizationFields, data.Page, nil
55+
}
56+
57+
// CreateOrganizationField creates new organization field
58+
// ref: https://developer.zendesk.com/api-reference/ticketing/organizations/organization_fields/#create-organization-field
59+
func (z *Client) CreateOrganizationField(ctx context.Context, organizationField OrganizationField) (OrganizationField, error) {
60+
var data, result struct {
61+
OrganizationField OrganizationField `json:"organization_field"`
62+
}
63+
data.OrganizationField = organizationField
64+
65+
body, err := z.post(ctx, "/organization_fields.json", data)
66+
if err != nil {
67+
return OrganizationField{}, err
68+
}
69+
70+
err = json.Unmarshal(body, &result)
71+
if err != nil {
72+
return OrganizationField{}, err
73+
}
74+
return result.OrganizationField, nil
75+
}

zendesk/organization_field_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package zendesk
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
)
7+
8+
func TestGetOrganizationFields(t *testing.T) {
9+
mockAPI := newMockAPI(http.MethodGet, "organization_fields.json")
10+
client := newTestClient(mockAPI)
11+
defer mockAPI.Close()
12+
13+
ticketFields, _, err := client.GetOrganizationFields(ctx)
14+
if err != nil {
15+
t.Fatalf("Failed to get organization fields: %s", err)
16+
}
17+
18+
if len(ticketFields) != 2 {
19+
t.Fatalf("expected length of organization fields is , but got %d", len(ticketFields))
20+
}
21+
}
22+
23+
func TestOrganizationField(t *testing.T) {
24+
mockAPI := newMockAPIWithStatus(http.MethodPost, "organization_fields.json", http.StatusCreated)
25+
client := newTestClient(mockAPI)
26+
defer mockAPI.Close()
27+
28+
_, err := client.CreateOrganizationField(ctx, OrganizationField{})
29+
if err != nil {
30+
t.Fatalf("Failed to send request to create organization field: %s", err)
31+
}
32+
}

0 commit comments

Comments
 (0)