Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support position for macros and views #7

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions zendesk/client/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type MacroAPI interface {
DeleteMacro(ctx context.Context, id int64) error
UpdateMacro(ctx context.Context, id int64, form models.Macro) (models.Macro, error)
GetMacro(ctx context.Context, id int64) (models.Macro, error)
UpdateMacroPosition(ctx context.Context, id int64, macro models.MacroPosition) error
}

// GetMacros fetches macros
Expand Down Expand Up @@ -95,7 +96,13 @@ func (z *Client) UpdateMacro(ctx context.Context, id int64, form models.Macro) (
Macro models.Macro `json:"macro"`
}

z.UpdateMacroPosition(ctx, id, models.MacroPosition{
ID: id,
Position: form.Position,
})

data.Macro = form
fmt.Printf("\nUpdating Macro position to %d\n", data.Macro.Position)
body, err := z.Put(ctx, fmt.Sprintf("/macros/%d.json", id), data)
if err != nil {
return models.Macro{}, err
Expand All @@ -109,6 +116,28 @@ func (z *Client) UpdateMacro(ctx context.Context, id int64, form models.Macro) (
return result.Macro, nil
}

func (z *Client) UpdateMacroPosition(ctx context.Context, id int64, macro models.MacroPosition) error {
var data, result struct {
Macros []models.MacroPosition `json:"macros"`
}

data.Macros = append(data.Macros, macro)

body, err := z.Put(ctx, "/macros/update_many", data)
if err != nil {
return err
}

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

fmt.Sprintf("\nUpdated position to %d for macro %d", macro.Position, macro.ID)

return nil
}

// DeleteMacro deletes the specified macro
// ref: https://developer.zendesk.com/rest_api/docs/support/macro#delete-macro
func (z *Client) DeleteMacro(ctx context.Context, id int64) error {
Expand Down
163 changes: 163 additions & 0 deletions zendesk/client/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package client

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

"github.com/nukosuke/terraform-provider-zendesk/zendesk/models"
)

type ViewAPI interface {
CreateView(ctx context.Context, view models.View) (models.Macro, error)
DeleteView(ctx context.Context, id int64) error
UpdateView(ctx context.Context, id int64, form models.Macro) (models.Macro, error)
GetView(ctx context.Context, id int64) (models.Macro, error)
UpdateViewPosition(ctx context.Context, id int64, view models.ViewPosition) error
}

func mapViewToViewCreateOrUpdate(view models.View) models.ViewCreateOrUpdate {
var viewCreateOrUpdate models.ViewCreateOrUpdate

// Map properties from view to viewCreateOrUpdate
viewCreateOrUpdate.ID = view.ID
viewCreateOrUpdate.Active = view.Active
viewCreateOrUpdate.Description = view.Description
viewCreateOrUpdate.Position = view.Position
viewCreateOrUpdate.Title = view.Title
viewCreateOrUpdate.CreatedAt = view.CreatedAt
viewCreateOrUpdate.UpdatedAt = view.UpdatedAt
viewCreateOrUpdate.All = view.Conditions.All
viewCreateOrUpdate.Any = view.Conditions.Any
viewCreateOrUpdate.URL = view.URL

// Rename "Execution" to "Output" in ViewCreateOrUpdate
viewCreateOrUpdate.Output.GroupBy = view.Execution.GroupBy
viewCreateOrUpdate.Output.SortBy = view.Execution.SortBy
viewCreateOrUpdate.Output.GroupOrder = view.Execution.GroupOrder
viewCreateOrUpdate.Output.SortOrder = view.Execution.SortOrder

viewCreateOrUpdate.Restriction = view.Restriction

var columns []interface{}
for _, col := range view.Execution.Columns {
columns = append(columns, col.ID)
}
viewCreateOrUpdate.Output.Columns = columns

return viewCreateOrUpdate
}

func (z *Client) CreateView(ctx context.Context, view models.View) (models.View, error) {
var result struct {
View models.View `json:"view"`
}
var data struct {
View models.ViewCreateOrUpdate `json:"view"`
}
data.View = mapViewToViewCreateOrUpdate(view)

body, err := z.Post(ctx, "/views.json", data)

if err != nil {
return models.View{}, err
}

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

func (z *Client) GetView(ctx context.Context, id int64) (models.View, error) {
var result struct {
View models.View `json:"view"`
}

body, err := z.Get(ctx, fmt.Sprintf("/views/%d.json", id))
fmt.Println("GET bar")
fmt.Println(string(body))

if err != nil {
return models.View{}, err
}

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

return result.View, err
}

// UpdateView updates a field with the specified ticket field
// ref: https://developer.zendesk.com/rest_api/docs/support/user_fields#update-ticket-field
func (z *Client) UpdateView(ctx context.Context, id int64, view models.View) (models.View, error) {
var result struct {
View models.View `json:"view"`
}
var data struct {
View models.ViewCreateOrUpdate `json:"view"`
}

data.View = mapViewToViewCreateOrUpdate(view)

jsonData, err := json.Marshal(data)
fmt.Println("Update Processed payload: JSON")
fmt.Println(string(jsonData))

z.UpdateViewPosition(ctx, id, models.ViewPosition{
ID: id,
Position: view.Position,
})

body, err := z.Put(ctx, fmt.Sprintf("/views/%d.json", id), data)

if err != nil {
fmt.Println("Printing Error")
fmt.Println(fmt.Sprintf("%+v\n", err))
return models.View{}, err
}

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

return result.View, err
}

func (z *Client) UpdateViewPosition(ctx context.Context, id int64, view models.ViewPosition) error {
var data, result struct {
Views []models.ViewPosition `json:"views"`
}

data.Views = append(data.Views, view)

body, err := z.Put(ctx, "/views/update_many", data)
if err != nil {
return err
}

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

fmt.Sprintf("\nUpdated position to %d for view %d", view.Position, view.ID)

return nil
}

// DeleteView deletes the specified ticket field
// ref: https://developer.zendesk.com/rest_api/docs/support/user_fields#Delete-ticket-field
func (z *Client) DeleteView(ctx context.Context, viewID int64) error {
err := z.Delete(ctx, fmt.Sprintf("/views/%d.json", viewID))

if err != nil {
return err
}

return nil
}
5 changes: 5 additions & 0 deletions zendesk/models/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package models

import "time"

type MacroPosition struct {
ID int64 `json:"id,omitempty"`
Position int `json:"position,omitempty"`
}

type Macro struct {
Actions []MacroAction `json:"actions"`
Active bool `json:"active"`
Expand Down
75 changes: 75 additions & 0 deletions zendesk/models/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package models

import "time"

type (
// View is struct for group membership payload
// https://developer.zendesk.com/api-reference/ticketing/business-rules/views/

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

Column struct {
ID interface{} `json:"id"` // is string for normal fields & number for custom fields
Title string `json:"title"`
}

Restriction struct {
IDs []int `json:"ids"`
Type string `json:"type"`
}

// View has a certain structure in Get & Different structure in
// Put/Post
View struct {
ID int64 `json:"id,omitempty"`
Active bool `json:"active"`
Description string `json:"description"`
Position int `json:"position"`
Title string `json:"title"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Restriction *Restriction `json:"restriction"`
Conditions struct {
All []ViewCondition `json:"all"`
Any []ViewCondition `json:"any"`
} `json:"conditions"`
URL string `json:"url,omitempty"`
Execution struct {
Columns []Column `json:"columns"`
GroupBy string `json:"group_by,omitempty"`
SortBy string `json:"sort_by,omitempty"`
GroupOrder string `json:"group_order,omitempty"`
SortOrder string `json:"sort_order,omitempty"`
} `json:"execution"`
}
ViewCreateOrUpdate struct {
ID int64 `json:"id,omitempty"`
Active bool `json:"active"`
Description string `json:"description"`
Position int `json:"position"`
Title string `json:"title"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
All []ViewCondition `json:"all"`
Any []ViewCondition `json:"any"`
URL string `json:"url,omitempty"`
Restriction *Restriction `json:"restriction"`

Output struct {
Columns []interface{} `json:"columns"` // number for custom fields, string otherwise
GroupBy string `json:"group_by,omitempty"`
SortBy string `json:"sort_by,omitempty"`
GroupOrder string `json:"group_order,omitempty"`
SortOrder string `json:"sort_order,omitempty"`
} `json:"output"`
}

ViewPosition struct {
ID int64 `json:"id,omitempty"`
Position int `json:"position,omitempty"`
}
)
7 changes: 2 additions & 5 deletions zendesk/resource_zendesk_macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
newClient "github.com/nukosuke/terraform-provider-zendesk/zendesk/client"
"github.com/nukosuke/terraform-provider-zendesk/zendesk/models"
)
Expand Down Expand Up @@ -63,12 +62,10 @@ func resourceZendeskMacro() *schema.Resource {
Computed: true,
},
"position": {
Description: "The relative position of the user field on a ticket. Note that for accounts with ticket forms, positions are controlled by the different forms.",
Description: "IMPORTANT! in order for this to take affect an update on the resource is necessary, since only that triggers a call to update position",
Type: schema.TypeInt,
Optional: true,
// positions 0 to 7 are reserved for system fields
ValidateFunc: validation.IntAtLeast(8),
Computed: true,
Computed: true,
},
"active": {
Description: "Whether this field is available.",
Expand Down
1 change: 0 additions & 1 deletion zendesk/resource_zendesk_ticket_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ func resourceZendeskTicketField() *schema.Resource {
Required: true,
},
"value": {
Description: "Custom field option value.",
Type: schema.TypeString,
Required: true,
},
Expand Down
Loading
Loading