Skip to content

Commit

Permalink
tickets/wenichats: open ticket customfields is body or contact fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rasoro committed Aug 4, 2023
1 parent 830c092 commit 0598641
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 14 deletions.
26 changes: 14 additions & 12 deletions services/tickets/wenichats/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,20 @@ func (s *service) Open(session flows.Session, topic *flows.Topic, body string, a
roomData.FlowUUID = session.Runs()[0].Flow().UUID()
roomData.Contact.Groups = groups

extra := &struct {
CustomFields map[string]interface{} `json:"custom_fields,omitempty"`
}{}

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

for k, v := range contact.Fields() {
if v != nil {
roomData.CustomFields[k] = v.Text.Render()
// if body is empty send contact fields to customfields or if body is not empty send fields defined in body
if len(body) == 0 {
for k, v := range contact.Fields() {
if v != nil {
roomData.CustomFields[k] = v.Text.Render()
}
}
} else {
extra := &struct {
CustomFields map[string]interface{} `json:"custom_fields,omitempty"`
}{}
err := jsonx.Unmarshal([]byte(body), extra)
if err == nil {
roomData.CustomFields = extra.CustomFields
}
}

Expand Down
100 changes: 100 additions & 0 deletions services/tickets/wenichats/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,41 @@ func TestOpenAndForward(t *testing.T) {
},
"callback_url": "http://example.com"
}`),
httpx.NewMockResponse(201, nil, `{
"uuid": "e5cbc781-4e0e-4954-b078-0373308e11c3",
"user": {
"first_name": "John",
"last_name": "Doe",
"email": "[email protected]"
},
"contact": {
"external_id": "095be615-a8ad-4c33-8e9c-c7612fbf6c9f",
"name": "Foo Bar",
"email": "[email protected]",
"status": "string",
"phone": "+250788123123",
"custom_fields": {},
"created_on": "2019-08-24T14:15:22Z"
},
"queue": {
"uuid": "449f48d9-4905-4d6f-8abf-f1ff6afb803e",
"created_on": "2019-08-24T14:15:22Z",
"modified_on": "2019-08-24T14:15:22Z",
"name": "CHATS",
"sector": "f3d496ff-c154-4a96-a678-6a8879583ddb"
},
"created_on": "2019-08-24T14:15:22Z",
"modified_on": "2019-08-24T14:15:22Z",
"is_active": true,
"custom_fields": {
"country": "brazil",
"mood": "angry",
"age": 23,
"join_date": "2017-12-02",
"gender": "male"
},
"callback_url": "http://example.com"
}`),
},
fmt.Sprintf("%s/rooms/8ecb1e4a-b457-4645-a161-e2b02ddffa88/", baseURL): {
httpx.NewMockResponse(200, nil, `{
Expand Down Expand Up @@ -112,6 +147,40 @@ func TestOpenAndForward(t *testing.T) {
"callback_url": "http://example.com"
}`),
},
fmt.Sprintf("%s/rooms/e5cbc781-4e0e-4954-b078-0373308e11c3/", baseURL): {
httpx.NewMockResponse(200, nil, `{
"uuid": "e5cbc781-4e0e-4954-b078-0373308e11c3",
"user": {
"first_name": "John",
"last_name": "Doe",
"email": "[email protected]"
},
"contact": {
"external_id": "095be615-a8ad-4c33-8e9c-c7612fbf6c9f",
"name": "Foo Bar",
"email": "[email protected]",
"status": "string",
"phone": "+250788123123",
"custom_fields": {},
"created_on": "2019-08-24T14:15:22Z"
},
"queue": {
"uuid": "449f48d9-4905-4d6f-8abf-f1ff6afb803e",
"created_on": "2019-08-24T14:15:22Z",
"modified_on": "2019-08-24T14:15:22Z",
"name": "CHATS",
"sector": "f3d496ff-c154-4a96-a678-6a8879583ddb"
},
"created_on": "2019-08-24T14:15:22Z",
"modified_on": "2019-08-24T14:15:22Z",
"is_active": true,
"custom_fields": {
"country": "brazil",
"mood": "angry"
},
"callback_url": "http://example.com"
}`),
},
fmt.Sprintf("%s/msgs/", baseURL): {
httpx.MockConnectionError,
httpx.NewMockResponse(200, nil, `{
Expand Down Expand Up @@ -261,6 +330,37 @@ func TestOpenAndForward(t *testing.T) {
err = svc.Forward(dbTicket2, flows.MsgUUID("5ga340ae-1fb0-4666-98db-2177fe9bf31c"), "It's urgent", attachments, logger.Log)
assert.NoError(t, err)
assert.Equal(t, 1, len(logger.Logs))

// test open with body empty

logger2 := &flows.HTTPLogger{}

wenichats.SetDB(rt.DB)
svc, err = wenichats.NewService(
rt.Config,
http.DefaultClient,
nil,
ticketer,
map[string]string{
"project_auth": authToken,
"sector_uuid": "1a4bae05-993c-4f3b-91b5-80f4e09951f2",
},
)
assert.NoError(t, err)

oa, err = models.GetOrgAssets(ctx, rt, testdata.Org1.ID)
require.NoError(t, err)
defaultTopic = oa.SessionAssets().Topics().FindByName("General")

ticket, err = svc.Open(session, defaultTopic, "", nil, logger2.Log)

assert.NoError(t, err)
assert.Equal(t, flows.TicketUUID("59d74b86-3e2f-4a93-aece-b05d2fdcde0c"), ticket.UUID())
assert.Equal(t, "General", ticket.Topic().Name())
assert.Equal(t, "", ticket.Body())
assert.Equal(t, "e5cbc781-4e0e-4954-b078-0373308e11c3", ticket.ExternalID())
assert.Equal(t, 2, len(logger2.Logs))
test.AssertSnapshot(t, "open_ticket_empty_body", logger2.Logs[0].Request)
}

func TestCloseAndReopen(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
POST /v1/external/rooms/ HTTP/1.1
Host: chats-engine.dev.cloud.weni.ai
User-Agent: Go-http-client/1.1
Content-Length: 583
Content-Length: 503
Authorization: Bearer ****************
Content-Type: application/json
Accept-Encoding: gzip

{"queue_uuid":"ffc903f7-8cbb-443f-9627-87106842d1aa","sector_uuid":"1a4bae05-993c-4f3b-91b5-80f4e09951f2","contact":{"external_id":"5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f","name":"Ryan Lewis","urn":"tel:+12024561111?channel=57f1078f-88aa-46f4-a59a-948a5739c03d","groups":[{"uuid":"b7cf0d83-f1c9-411c-96fd-c511a4cfa86d","name":"Testers"},{"uuid":"4f1f98fc-27a7-4a69-bbdb-24744ba739a9","name":"Males"}]},"custom_fields":{"activation_****************":"AACC55","age":"23","country":"brazil","gender":"Male","join_date":"2017-12-02","mood":"angry"},"flow_uuid":"50c3706e-fedb-42c0-8eab-dda3335714b7"}
{"queue_uuid":"ffc903f7-8cbb-443f-9627-87106842d1aa","sector_uuid":"1a4bae05-993c-4f3b-91b5-80f4e09951f2","contact":{"external_id":"5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f","name":"Ryan Lewis","urn":"tel:+12024561111?channel=57f1078f-88aa-46f4-a59a-948a5739c03d","groups":[{"uuid":"b7cf0d83-f1c9-411c-96fd-c511a4cfa86d","name":"Testers"},{"uuid":"4f1f98fc-27a7-4a69-bbdb-24744ba739a9","name":"Males"}]},"custom_fields":{"country":"brazil","mood":"angry"},"flow_uuid":"50c3706e-fedb-42c0-8eab-dda3335714b7"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
POST /v1/external/rooms/ HTTP/1.1
Host: chats-engine.dev.cloud.weni.ai
User-Agent: Go-http-client/1.1
Content-Length: 549
Authorization: Bearer ****************
Content-Type: application/json
Accept-Encoding: gzip

{"queue_uuid":"ffc903f7-8cbb-443f-9627-87106842d1aa","sector_uuid":"1a4bae05-993c-4f3b-91b5-80f4e09951f2","contact":{"external_id":"5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f","name":"Ryan Lewis","urn":"tel:+12024561111?channel=57f1078f-88aa-46f4-a59a-948a5739c03d","groups":[{"uuid":"b7cf0d83-f1c9-411c-96fd-c511a4cfa86d","name":"Testers"},{"uuid":"4f1f98fc-27a7-4a69-bbdb-24744ba739a9","name":"Males"}]},"custom_fields":{"activation_****************":"AACC55","age":"23","gender":"Male","join_date":"2017-12-02"},"flow_uuid":"50c3706e-fedb-42c0-8eab-dda3335714b7"}

0 comments on commit 0598641

Please sign in to comment.