Skip to content

Commit

Permalink
Merge pull request #15 from weni-ai/feat/reopen-zendesk
Browse files Browse the repository at this point in the history
Support for reopening zendesk ticket with body ID
  • Loading branch information
Robi9 authored May 25, 2023
2 parents c77de1b + d3de4ea commit c511a2f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
14 changes: 12 additions & 2 deletions services/tickets/zendesk/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,24 @@ func (s *service) Open(session flows.Session, topic *flows.Topic, body string, a
Description string `json:"description"`
CustomFields []FieldValue `json:"custom_fields"`
Tags []string `json:"tags"`
ID int64 `json:"id"`
}{}

err := jsonx.Unmarshal([]byte(body), extra)
if err != nil {
return nil, err
}

if extra.ID > 0 {
var ids []int64
ids = append(ids, extra.ID)
_, trace, _ := s.restClient.UpdateManyTickets(ids, statusOpen)
if trace != nil {
logHTTP(flows.NewHTTPLog(trace, flows.HTTPStatusFromCode, s.redactor))
return ticket, nil
}
}

v := reflect.ValueOf(extra)
fields := reflect.Indirect(v)
if fields.NumField() > 0 {
Expand All @@ -134,12 +145,11 @@ func (s *service) Open(session flows.Session, topic *flows.Topic, body string, a
} else {
msg.Message = extra.Subject
}
}

}
if err := s.push(msg, logHTTP); err != nil {
return nil, err
}

return ticket, nil
}

Expand Down
20 changes: 19 additions & 1 deletion services/tickets/zendesk/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import (
"github.com/stretchr/testify/require"
)

const fieldTicket = `{"message":"Cookies","priority":"high","subject":"Where are my cookies?","description":"I want to know where is my cookie.","tags": ["TAG_01","TAG_02"],"custom_fields":[{"id":"21938362","value":"hd_3000"}]}`
const (
fieldTicket = `{"message":"Cookies","priority":"high","subject":"Where are my cookies?","description":"I want to know where is my cookie.","tags": ["TAG_01","TAG_02"],"custom_fields":[{"id":"21938362","value":"hd_3000"}]}`
fieldTicket1 = `{"id":10}`
)

func TestOpenAndForward(t *testing.T) {
ctx, rt, _, _ := testsuite.Get()
Expand Down Expand Up @@ -66,6 +69,14 @@ func TestOpenAndForward(t *testing.T) {
}
]}`),
},
"https://nyaruka.zendesk.com/api/v2/tickets/update_many.json?ids=10": {
httpx.NewMockResponse(200, nil, `{
"job_status": {
"id": "1234-abcd",
"url": "http://zendesk.com",
"status": "queued"
}}`),
},
}))

ticketer := flows.NewTicketer(static.NewTicketer(assets.TicketerUUID(uuids.New()), "Support", "zendesk"))
Expand Down Expand Up @@ -132,6 +143,13 @@ func TestOpenAndForward(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 1, len(logger.Logs))
test.AssertSnapshot(t, "forward_message", logger.Logs[0].Request)

logger = &flows.HTTPLogger{}
ticket1, err := svc.Open(session, defaultTopic, fieldTicket1, nil, logger.Log)
assert.NoError(t, err)
assert.Equal(t, fieldTicket1, ticket1.Body())
assert.Equal(t, 1, len(logger.Logs))
test.AssertSnapshot(t, "open", logger.Logs[0].Request)
}

func TestCloseAndReopen(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PUT /api/v2/tickets/update_many.json?ids=10 HTTP/1.1
Host: nyaruka.zendesk.com
User-Agent: Go-http-client/1.1
Content-Length: 28
Authorization: Bearer ****************
Content-Type: application/json
Accept-Encoding: gzip

{"ticket":{"status":"open"}}

0 comments on commit c511a2f

Please sign in to comment.