diff --git a/indexers/contacts.go b/indexers/contacts.go index c77230e..c6f2d08 100644 --- a/indexers/contacts.go +++ b/indexers/contacts.go @@ -147,7 +147,11 @@ SELECT org_id, id, modified_on, is_active, row_to_json(t) FROM ( ) AS groups, ( SELECT f.uuid FROM flows_flow f WHERE f.id = contacts_contact.current_flow_id - ) AS flow + ) AS flow, + current_flow_id AS flow_id, + ( + SELECT array_to_json(array_agg(DISTINCT fr.flow_id)) FROM flows_flowrun fr WHERE fr.contact_id = contacts_contact.id + ) AS flow_history_ids FROM contacts_contact WHERE modified_on >= $1 ORDER BY modified_on ASC diff --git a/indexers/contacts.settings.json b/indexers/contacts.settings.json index 75e8728..7a76a84 100644 --- a/indexers/contacts.settings.json +++ b/indexers/contacts.settings.json @@ -153,6 +153,12 @@ "flow": { "type": "keyword" }, + "flow_id": { + "type": "integer" + }, + "flow_history_ids": { + "type": "integer" + }, "tickets": { "type": "integer" }, diff --git a/indexers/contacts_test.go b/indexers/contacts_test.go index 8ef9916..071b9af 100644 --- a/indexers/contacts_test.go +++ b/indexers/contacts_test.go @@ -30,6 +30,10 @@ var contactQueryTests = []struct { {elastic.NewRangeQuery("tickets").Gt(0), []int64{1, 2, 3}}, {elastic.NewMatchQuery("flow", "6d3cf1eb-546e-4fb8-a5ca-69187648fbf6"), []int64{2, 3}}, {elastic.NewMatchQuery("flow", "4eea8ff1-4fe2-4ce5-92a4-0870a499973a"), []int64{4}}, + {elastic.NewMatchQuery("flow_id", 1), []int64{2, 3}}, + {elastic.NewMatchQuery("flow_id", 2), []int64{4}}, + {elastic.NewMatchQuery("flow_history_ids", 1), []int64{1, 2, 3}}, + {elastic.NewMatchQuery("flow_history_ids", 2), []int64{1, 2}}, {elastic.NewRangeQuery("created_on").Gt("2017-01-01"), []int64{1, 6, 8}}, // created_on range {elastic.NewRangeQuery("last_seen_on").Lt("2019-01-01"), []int64{3, 4}}, // last_seen_on range {elastic.NewExistsQuery("last_seen_on"), []int64{1, 2, 3, 4, 5, 6}}, // last_seen_on is set diff --git a/testdb.sql b/testdb.sql index 845d627..8e72d88 100644 --- a/testdb.sql +++ b/testdb.sql @@ -52,6 +52,15 @@ CREATE TABLE contacts_contactgroup_contacts ( contact_id integer NOT NULL REFERENCES contacts_contact(id) ); + +DROP TABLE IF EXISTS flows_flowrun CASCADE; +CREATE TABLE flows_flowrun ( + id SERIAL PRIMARY KEY, + uuid character varying(36) NOT NULL, + flow_id integer REFERENCES flows_flow(id), + contact_id integer REFERENCES contacts_contact(id) +); + INSERT INTO flows_flow(id, uuid, name) VALUES (1, '6d3cf1eb-546e-4fb8-a5ca-69187648fbf6', 'Favorites'), (2, '4eea8ff1-4fe2-4ce5-92a4-0870a499973a', 'Catch All'); @@ -152,3 +161,11 @@ INSERT INTO contacts_contactgroup_contacts(id, contact_id, contactgroup_id) VALU (1, 1, 1), (2, 1, 4), (3, 2, 4); + +INSERT INTO flows_flowrun(id, uuid, flow_id, contact_id) VALUES +(1, '8b30ee61-e19d-427e-bb9f-4b8cd2c31d0c', 1, 1), +(2, '94639979-155e-444d-95e9-a39dad64dbd5', 1, 1), +(3, '74d918df-0e31-4547-98a9-5d765450e2ac', 2, 1), +(4, '14fdf8fc-6e02-4759-b9be-cacc5991cd14', 1, 2), +(5, '5171b4d8-e9bb-46c1-901e-53e13f3afe5d', 2, 2), +(6, '4cc84e32-910f-41d6-865d-63fe25db4cde', 1, 3);