diff --git a/archives/archives_test.go b/archives/archives_test.go index 1c4e23c..0fc6d01 100644 --- a/archives/archives_test.go +++ b/archives/archives_test.go @@ -143,9 +143,9 @@ func TestCreateMsgArchive(t *testing.T) { // should have two records, second will have attachments assert.Equal(t, 3, task.RecordCount) - assert.Equal(t, int64(483), task.Size) + assert.Equal(t, int64(528), task.Size) assert.Equal(t, time.Date(2017, 8, 12, 0, 0, 0, 0, time.UTC), task.StartDate) - assert.Equal(t, "6fe9265860425cf1f9757ba3d91b1a05", task.Hash) + assert.Equal(t, "b3bf00bf1234ea47f14ffd0171a8ead0", task.Hash) assertArchiveFile(t, task, "messages1.jsonl") DeleteArchiveFile(task) @@ -163,8 +163,8 @@ func TestCreateMsgArchive(t *testing.T) { // should have one record assert.Equal(t, 1, task.RecordCount) - assert.Equal(t, int64(290), task.Size) - assert.Equal(t, "a719c7ec64c516a6e159d26a70cb4225", task.Hash) + assert.Equal(t, int64(294), task.Size) + assert.Equal(t, "bd163ead077774425aa559e30d48ca87", task.Hash) assertArchiveFile(t, task, "messages2.jsonl") DeleteArchiveFile(task) @@ -341,14 +341,14 @@ func TestArchiveOrgMessages(t *testing.T) { assert.Equal(t, time.Date(2017, 8, 12, 0, 0, 0, 0, time.UTC), created[2].StartDate) assert.Equal(t, DayPeriod, created[2].Period) assert.Equal(t, 3, created[2].RecordCount) - assert.Equal(t, int64(483), created[2].Size) - assert.Equal(t, "6fe9265860425cf1f9757ba3d91b1a05", created[2].Hash) + assert.Equal(t, int64(528), created[2].Size) + assert.Equal(t, "b3bf00bf1234ea47f14ffd0171a8ead0", created[2].Hash) assert.Equal(t, time.Date(2017, 8, 13, 0, 0, 0, 0, time.UTC), created[3].StartDate) assert.Equal(t, DayPeriod, created[3].Period) assert.Equal(t, 1, created[3].RecordCount) - assert.Equal(t, int64(306), created[3].Size) - assert.Equal(t, "7ece4401d3afac9c08a913398f213ffa", created[3].Hash) + assert.Equal(t, int64(312), created[3].Size) + assert.Equal(t, "32e61b1431217b59fca0170f637d78a3", created[3].Hash) assert.Equal(t, time.Date(2017, 10, 10, 0, 0, 0, 0, time.UTC), created[60].StartDate) assert.Equal(t, DayPeriod, created[60].Period) @@ -359,8 +359,8 @@ func TestArchiveOrgMessages(t *testing.T) { assert.Equal(t, time.Date(2017, 8, 1, 0, 0, 0, 0, time.UTC), created[61].StartDate) assert.Equal(t, MonthPeriod, created[61].Period) assert.Equal(t, 4, created[61].RecordCount) - assert.Equal(t, int64(509), created[61].Size) - assert.Equal(t, "9e40be76913bf58655b70ee96dcac25d", created[61].Hash) + assert.Equal(t, int64(553), created[61].Size) + assert.Equal(t, "156e45e29b6587cb85ccf75e03800b00", created[61].Hash) assert.Equal(t, time.Date(2017, 9, 1, 0, 0, 0, 0, time.UTC), created[62].StartDate) assert.Equal(t, MonthPeriod, created[62].Period) diff --git a/archives/messages.go b/archives/messages.go index b957015..8d44d7f 100644 --- a/archives/messages.go +++ b/archives/messages.go @@ -20,6 +20,7 @@ SELECT rec.visibility, row_to_json(rec) FROM ( row_to_json(contact) as contact, CASE WHEN oo.is_anon = False THEN ccu.identity ELSE null END as urn, row_to_json(channel) as channel, + row_to_json(flow) as flow, CASE WHEN direction = 'I' THEN 'in' WHEN direction = 'O' THEN 'out' ELSE NULL @@ -61,6 +62,7 @@ SELECT rec.visibility, row_to_json(rec) FROM ( JOIN LATERAL (select uuid, name from contacts_contact cc where cc.id = mm.contact_id) as contact ON True LEFT JOIN contacts_contacturn ccu ON mm.contact_urn_id = ccu.id LEFT JOIN LATERAL (select uuid, name from channels_channel ch where ch.id = mm.channel_id) as channel ON True + LEFT JOIN LATERAL (select uuid, name from flows_flow f where f.id = mm.flow_id) as flow ON True LEFT JOIN LATERAL (select coalesce(jsonb_agg(label_row), '[]'::jsonb) as data from (select uuid, name from msgs_label ml INNER JOIN msgs_msg_labels mml ON ml.id = mml.label_id AND mml.msg_id = mm.id) as label_row) as labels_agg ON True WHERE mm.org_id = $1 AND mm.created_on >= $2 AND mm.created_on < $3 diff --git a/archives/testdata/messages1.jsonl b/archives/testdata/messages1.jsonl index f1c6bda..6b53f3e 100644 --- a/archives/testdata/messages1.jsonl +++ b/archives/testdata/messages1.jsonl @@ -1,3 +1,3 @@ -{"id":1,"broadcast":null,"contact":{"uuid":"3e814add-e614-41f7-8b5d-a07f670a698f","name":"Ajodinabiff Dane"},"urn":"tel:+12067797777","channel":{"uuid":"60f2ed5b-05f2-4156-9ff0-e44e90da1b85","name":"Channel 2"},"direction":"in","type":"inbox","status":"handled","visibility":"visible","text":"message 1","attachments":[],"labels":[{"name": "Label 1", "uuid": "1d9e3188-b74b-4ae0-a166-0de31aedb34a"}, {"name": "Label 2", "uuid": "c5a69101-8dc3-444f-8b0b-5ab816e46eec"}],"created_on":"2017-08-12T21:11:59.890662+00:00","sent_on":"2017-08-12T21:11:59.890662+00:00","modified_on":"2017-08-12T21:11:59.890662+00:00"} -{"id":3,"broadcast":null,"contact":{"uuid":"3e814add-e614-41f7-8b5d-a07f670a698f","name":"Ajodinabiff Dane"},"urn":"tel:+12067797777","channel":null,"direction":"out","type":"inbox","status":"handled","visibility":"visible","text":"message 3","attachments":[{"url": "https://foo.bar/image1.png", "content_type": "image/png"}, {"url": "https://foo.bar/image2.png", "content_type": "image/png"}],"labels":[{"name": "Label 2", "uuid": "c5a69101-8dc3-444f-8b0b-5ab816e46eec"}],"created_on":"2017-08-12T21:11:59.890662+00:00","sent_on":"2017-08-12T21:11:59.890662+00:00","modified_on":"2017-08-12T21:11:59.890662+00:00"} -{"id":9,"broadcast":null,"contact":{"uuid":"3e814add-e614-41f7-8b5d-a07f670a698f","name":"Ajodinabiff Dane"},"urn":null,"channel":null,"direction":"out","type":"flow","status":"sent","visibility":"visible","text":"message 9","attachments":[],"labels":[],"created_on":"2017-08-12T21:11:59.890662+00:00","sent_on":"2017-08-12T21:11:59.890662+00:00","modified_on":"2017-08-12T21:11:59.890662+00:00"} +{"id":1,"broadcast":null,"contact":{"uuid":"3e814add-e614-41f7-8b5d-a07f670a698f","name":"Ajodinabiff Dane"},"urn":"tel:+12067797777","channel":{"uuid":"60f2ed5b-05f2-4156-9ff0-e44e90da1b85","name":"Channel 2"},"flow":null,"direction":"in","type":"inbox","status":"handled","visibility":"visible","text":"message 1","attachments":[],"labels":[{"name": "Label 1", "uuid": "1d9e3188-b74b-4ae0-a166-0de31aedb34a"}, {"name": "Label 2", "uuid": "c5a69101-8dc3-444f-8b0b-5ab816e46eec"}],"created_on":"2017-08-12T21:11:59.890662+00:00","sent_on":"2017-08-12T21:11:59.890662+00:00","modified_on":"2017-08-12T21:11:59.890662+00:00"} +{"id":3,"broadcast":null,"contact":{"uuid":"3e814add-e614-41f7-8b5d-a07f670a698f","name":"Ajodinabiff Dane"},"urn":"tel:+12067797777","channel":null,"flow":null,"direction":"out","type":"inbox","status":"handled","visibility":"visible","text":"message 3","attachments":[{"url": "https://foo.bar/image1.png", "content_type": "image/png"}, {"url": "https://foo.bar/image2.png", "content_type": "image/png"}],"labels":[{"name": "Label 2", "uuid": "c5a69101-8dc3-444f-8b0b-5ab816e46eec"}],"created_on":"2017-08-12T21:11:59.890662+00:00","sent_on":"2017-08-12T21:11:59.890662+00:00","modified_on":"2017-08-12T21:11:59.890662+00:00"} +{"id":9,"broadcast":null,"contact":{"uuid":"3e814add-e614-41f7-8b5d-a07f670a698f","name":"Ajodinabiff Dane"},"urn":null,"channel":null,"flow":{"uuid":"3914b88e-625b-4603-bd9f-9319dc331c6b","name":"Flow 3"},"direction":"out","type":"flow","status":"sent","visibility":"visible","text":"message 9","attachments":[],"labels":[],"created_on":"2017-08-12T21:11:59.890662+00:00","sent_on":"2017-08-12T21:11:59.890662+00:00","modified_on":"2017-08-12T21:11:59.890662+00:00"} diff --git a/archives/testdata/messages2.jsonl b/archives/testdata/messages2.jsonl index a01bd44..01f5bc6 100644 --- a/archives/testdata/messages2.jsonl +++ b/archives/testdata/messages2.jsonl @@ -1 +1 @@ -{"id":5,"broadcast":null,"contact":{"uuid":"7051dff0-0a27-49d7-af1f-4494239139e6","name":"Joanne Stone"},"urn":null,"channel":{"uuid":"b79e0054-068f-4928-a5f4-339d10a7ad5a","name":"Channel 3"},"direction":"in","type":"inbox","status":"handled","visibility":"visible","text":"message 5","attachments":[],"labels":[],"created_on":"2017-08-11T19:11:59.890662+00:00","sent_on":"2017-08-11T19:11:59.890662+00:00","modified_on":"2017-08-11T19:11:59.890662+00:00"} +{"id":5,"broadcast":null,"contact":{"uuid":"7051dff0-0a27-49d7-af1f-4494239139e6","name":"Joanne Stone"},"urn":null,"channel":{"uuid":"b79e0054-068f-4928-a5f4-339d10a7ad5a","name":"Channel 3"},"flow":null,"direction":"in","type":"inbox","status":"handled","visibility":"visible","text":"message 5","attachments":[],"labels":[],"created_on":"2017-08-11T19:11:59.890662+00:00","sent_on":"2017-08-11T19:11:59.890662+00:00","modified_on":"2017-08-11T19:11:59.890662+00:00"} diff --git a/testdb.sql b/testdb.sql index 8922c0c..ca6868f 100644 --- a/testdb.sql +++ b/testdb.sql @@ -62,6 +62,13 @@ CREATE TABLE contacts_contactgroup_contacts ( contact_id integer NOT NULL ); +DROP TABLE IF EXISTS flows_flow CASCADE; +CREATE TABLE flows_flow ( + id serial primary key, + uuid character varying(36) NOT NULL, + name character varying(128) NOT NULL +); + DROP TABLE IF EXISTS channels_channellog CASCADE; DROP TABLE IF EXISTS msgs_msg_labels CASCADE; DROP TABLE IF EXISTS msgs_msg CASCADE; @@ -88,6 +95,7 @@ CREATE TABLE msgs_msg ( contact_id integer NOT NULL references contacts_contact(id) on delete cascade, contact_urn_id integer NULL references contacts_contacturn(id) on delete cascade, org_id integer NOT NULL references orgs_org(id) on delete cascade, + flow_id integer NULL references flows_flow(id) on delete cascade, metadata text, topup_id integer, delete_reason char(1) NULL @@ -145,13 +153,6 @@ CREATE TABLE msgs_msg_labels ( label_id integer NOT NULL ); -DROP TABLE IF EXISTS flows_flow CASCADE; -CREATE TABLE flows_flow ( - id serial primary key, - uuid character varying(36) NOT NULL, - name character varying(128) NOT NULL -); - DROP TABLE IF EXISTS auth_user CASCADE; CREATE TABLE auth_user ( id serial primary key, @@ -254,21 +255,27 @@ INSERT INTO contacts_contactgroup_contacts(id, contact_id, contactgroup_id) VALU (3, 1, 4), (4, 3, 4); +INSERT INTO flows_flow(id, uuid, name) VALUES +(1, '6639286a-9120-45d4-aa39-03ae3942a4a6', 'Flow 1'), +(2, '629db399-a5fb-4fa0-88e6-f479957b63d2', 'Flow 2'), +(3, '3914b88e-625b-4603-bd9f-9319dc331c6b', 'Flow 3'), +(4, 'cfa2371d-2f06-481d-84b2-d974f3803bb0', 'Flow 4'); + INSERT INTO msgs_broadcast(id, text, created_on, purged, org_id, schedule_id) VALUES (1, 'eng=>"hello",fre=>"bonjour"'::hstore, '2017-08-12 22:11:59.890662+02:00', TRUE, 2, 1), (2, 'base=>"hola"'::hstore, '2017-08-12 22:11:59.890662+02:00', TRUE, 2, NULL), (3, 'base=>"not purged"'::hstore, '2017-08-12 19:11:59.890662+02:00', FALSE, 2, NULL), (4, 'base=>"new"'::hstore, '2019-08-12 19:11:59.890662+02:00', FALSE, 2, NULL); -INSERT INTO msgs_msg(id, broadcast_id, uuid, text, created_on, sent_on, modified_on, direction, status, visibility, msg_type, attachments, channel_id, contact_id, contact_urn_id, org_id, msg_count, error_count, next_attempt) VALUES -(1, NULL, '2f969340-704a-4aa2-a1bd-2f832a21d257', 'message 1', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', 'I', 'H', 'V', 'I', NULL, 2, 6, 7, 2, 1, 0, '2017-08-12 21:11:59.890662+00'), -(2, NULL, 'abe87ac1-015c-4803-be29-1e89509fe682', 'message 2', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', 'I', 'H', 'D', 'I', NULL, 2, 6, 7, 2, 1, 0, '2017-08-12 21:11:59.890662+00'), -(3, NULL, 'a7e83a22-a6ff-4e18-82d0-19545640ccba', 'message 3', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', 'O', 'H', 'V', 'I', '{"image/png:https://foo.bar/image1.png", "image/png:https://foo.bar/image2.png"}', NULL, 6, 7, 2, 1, 0, '2017-08-12 21:11:59.890662+00'), -(4, NULL, '1cad36af-5581-4c8a-81cd-83708398f61e', 'message 4', '2017-08-13 21:11:59.890662+00', '2017-08-13 21:11:59.890662+00', '2017-08-13 21:11:59.890662+00', 'I', 'H', 'V', 'I', NULL, 2, 6, 7, 2, 1, 0, '2017-08-13 21:11:59.890662+00'), -(5, NULL, 'f557972e-2eb5-42fa-9b87-902116d18787', 'message 5', '2017-08-11 21:11:59.890662+02:00', '2017-08-11 21:11:59.890662+02:00', '2017-08-11 21:11:59.890662+02:00', 'I', 'H', 'V', 'I', NULL, 3, 7, 8, 3, 1, 0, '2017-08-11 21:11:59.890662+02:00'), -(6, 2, '579d148c-0ab1-4afb-832f-afb1fe0e19b7', 'message 6', '2017-10-08 21:11:59.890662+00', '2017-10-08 21:11:59.890662+00', '2017-10-08 21:11:59.890662+00', 'I', 'H', 'V', 'I', NULL, 2, 6, 7, 2, 1, 0, '2017-10-08 21:11:59.890662+00'), -(7, NULL, '7aeca469-2593-444e-afe4-4702317534c9', 'message 7', '2018-01-02 21:11:59.890662+00', '2018-01-02 21:11:59.890662+00', '2018-01-02 21:11:59.890662+00', 'I', 'H', 'V', 'I', NULL, 2, 6, 7, 2, 1, 0, '2018-01-02 21:11:59.890662+00'), -(9, NULL, 'e14ab466-0d3b-436d-a0f7-5851fd7d9b7d', 'message 9', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', 'O', 'S', 'V', 'F', NULL, NULL, 6, NULL, 2, 1, 0, '2017-08-12 21:11:59.890662+00'); +INSERT INTO msgs_msg(id, broadcast_id, uuid, text, created_on, sent_on, modified_on, direction, status, visibility, msg_type, attachments, channel_id, contact_id, contact_urn_id, org_id, flow_id, msg_count, error_count, next_attempt) VALUES +(1, NULL, '2f969340-704a-4aa2-a1bd-2f832a21d257', 'message 1', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', 'I', 'H', 'V', 'I', NULL, 2, 6, 7, 2, NULL, 1, 0, '2017-08-12 21:11:59.890662+00'), +(2, NULL, 'abe87ac1-015c-4803-be29-1e89509fe682', 'message 2', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', 'I', 'H', 'D', 'I', NULL, 2, 6, 7, 2, NULL, 1, 0, '2017-08-12 21:11:59.890662+00'), +(3, NULL, 'a7e83a22-a6ff-4e18-82d0-19545640ccba', 'message 3', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', 'O', 'H', 'V', 'I', '{"image/png:https://foo.bar/image1.png", "image/png:https://foo.bar/image2.png"}', NULL, 6, 7, 2, NULL, 1, 0, '2017-08-12 21:11:59.890662+00'), +(4, NULL, '1cad36af-5581-4c8a-81cd-83708398f61e', 'message 4', '2017-08-13 21:11:59.890662+00', '2017-08-13 21:11:59.890662+00', '2017-08-13 21:11:59.890662+00', 'I', 'H', 'V', 'I', NULL, 2, 6, 7, 2, NULL, 1, 0, '2017-08-13 21:11:59.890662+00'), +(5, NULL, 'f557972e-2eb5-42fa-9b87-902116d18787', 'message 5', '2017-08-11 21:11:59.890662+02:00', '2017-08-11 21:11:59.890662+02:00', '2017-08-11 21:11:59.890662+02:00', 'I', 'H', 'V', 'I', NULL, 3, 7, 8, 3, NULL, 1, 0, '2017-08-11 21:11:59.890662+02:00'), +(6, 2, '579d148c-0ab1-4afb-832f-afb1fe0e19b7', 'message 6', '2017-10-08 21:11:59.890662+00', '2017-10-08 21:11:59.890662+00', '2017-10-08 21:11:59.890662+00', 'I', 'H', 'V', 'I', NULL, 2, 6, 7, 2, NULL, 1, 0, '2017-10-08 21:11:59.890662+00'), +(7, NULL, '7aeca469-2593-444e-afe4-4702317534c9', 'message 7', '2018-01-02 21:11:59.890662+00', '2018-01-02 21:11:59.890662+00', '2018-01-02 21:11:59.890662+00', 'I', 'H', 'V', 'F', NULL, 2, 6, 7, 2, 2, 1, 0, '2018-01-02 21:11:59.890662+00'), +(9, NULL, 'e14ab466-0d3b-436d-a0f7-5851fd7d9b7d', 'message 9', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', '2017-08-12 21:11:59.890662+00', 'O', 'S', 'V', 'F', NULL, NULL, 6, NULL, 2, 3, 1, 0, '2017-08-12 21:11:59.890662+00'); INSERT INTO msgs_label(id, uuid, name) VALUES (1, '1d9e3188-b74b-4ae0-a166-0de31aedb34a', 'Label 1'), @@ -289,12 +296,6 @@ INSERT INTO channels_channellog(id, msg_id) VALUES (5, 5), (6, 6); -INSERT INTO flows_flow(id, uuid, name) VALUES -(1, '6639286a-9120-45d4-aa39-03ae3942a4a6', 'Flow 1'), -(2, '629db399-a5fb-4fa0-88e6-f479957b63d2', 'Flow 2'), -(3, '3914b88e-625b-4603-bd9f-9319dc331c6b', 'Flow 3'), -(4, 'cfa2371d-2f06-481d-84b2-d974f3803bb0', 'Flow 4'); - INSERT INTO auth_user(id, username) VALUES (1, 'greg@gmail.com');