From 0598641ea5729b9209965633a3f420588cdf0173 Mon Sep 17 00:00:00 2001 From: Rafael Soares Date: Fri, 4 Aug 2023 16:28:20 -0300 Subject: [PATCH] tickets/wenichats: open ticket customfields is body or contact fields --- services/tickets/wenichats/service.go | 26 ++--- services/tickets/wenichats/service_test.go | 100 ++++++++++++++++++ .../TestOpenAndForward_open_ticket.snap | 4 +- ...OpenAndForward_open_ticket_empty_body.snap | 9 ++ 4 files changed, 125 insertions(+), 14 deletions(-) create mode 100644 services/tickets/wenichats/testdata/TestOpenAndForward_open_ticket_empty_body.snap diff --git a/services/tickets/wenichats/service.go b/services/tickets/wenichats/service.go index e00e8fff2..1a7ffdbbb 100644 --- a/services/tickets/wenichats/service.go +++ b/services/tickets/wenichats/service.go @@ -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 } } diff --git a/services/tickets/wenichats/service_test.go b/services/tickets/wenichats/service_test.go index a2b593b7b..840a00303 100644 --- a/services/tickets/wenichats/service_test.go +++ b/services/tickets/wenichats/service_test.go @@ -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": "john.doe@chats.weni.ai" + }, + "contact": { + "external_id": "095be615-a8ad-4c33-8e9c-c7612fbf6c9f", + "name": "Foo Bar", + "email": "FooBar@weni.ai", + "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, `{ @@ -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": "john.doe@chats.weni.ai" + }, + "contact": { + "external_id": "095be615-a8ad-4c33-8e9c-c7612fbf6c9f", + "name": "Foo Bar", + "email": "FooBar@weni.ai", + "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, `{ @@ -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) { diff --git a/services/tickets/wenichats/testdata/TestOpenAndForward_open_ticket.snap b/services/tickets/wenichats/testdata/TestOpenAndForward_open_ticket.snap index 4c0e5d2b2..1f71738a5 100644 --- a/services/tickets/wenichats/testdata/TestOpenAndForward_open_ticket.snap +++ b/services/tickets/wenichats/testdata/TestOpenAndForward_open_ticket.snap @@ -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"} \ No newline at end of file +{"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"} \ No newline at end of file diff --git a/services/tickets/wenichats/testdata/TestOpenAndForward_open_ticket_empty_body.snap b/services/tickets/wenichats/testdata/TestOpenAndForward_open_ticket_empty_body.snap new file mode 100644 index 000000000..db59d6cf9 --- /dev/null +++ b/services/tickets/wenichats/testdata/TestOpenAndForward_open_ticket_empty_body.snap @@ -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"} \ No newline at end of file