|
| 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 | +} |
0 commit comments