-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from paoloromolini/organization_field
Add custom organization fields (list and create) methods
- Loading branch information
Showing
7 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |