Skip to content

Commit 1ad3e88

Browse files
authored
Merge pull request #39 from nyaruka/flow_history
Track history of flow ids on contacts
2 parents c99c739 + bace1d5 commit 1ad3e88

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

Diff for: indexers/contacts.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ SELECT org_id, id, modified_on, is_active, row_to_json(t) FROM (
147147
) AS groups,
148148
(
149149
SELECT f.uuid FROM flows_flow f WHERE f.id = contacts_contact.current_flow_id
150-
) AS flow
150+
) AS flow,
151+
current_flow_id AS flow_id,
152+
(
153+
SELECT array_to_json(array_agg(DISTINCT fr.flow_id)) FROM flows_flowrun fr WHERE fr.contact_id = contacts_contact.id
154+
) AS flow_history_ids
151155
FROM contacts_contact
152156
WHERE modified_on >= $1
153157
ORDER BY modified_on ASC

Diff for: indexers/contacts.settings.json

+6
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@
153153
"flow": {
154154
"type": "keyword"
155155
},
156+
"flow_id": {
157+
"type": "integer"
158+
},
159+
"flow_history_ids": {
160+
"type": "integer"
161+
},
156162
"tickets": {
157163
"type": "integer"
158164
},

Diff for: indexers/contacts_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ var contactQueryTests = []struct {
3030
{elastic.NewRangeQuery("tickets").Gt(0), []int64{1, 2, 3}},
3131
{elastic.NewMatchQuery("flow", "6d3cf1eb-546e-4fb8-a5ca-69187648fbf6"), []int64{2, 3}},
3232
{elastic.NewMatchQuery("flow", "4eea8ff1-4fe2-4ce5-92a4-0870a499973a"), []int64{4}},
33+
{elastic.NewMatchQuery("flow_id", 1), []int64{2, 3}},
34+
{elastic.NewMatchQuery("flow_id", 2), []int64{4}},
35+
{elastic.NewMatchQuery("flow_history_ids", 1), []int64{1, 2, 3}},
36+
{elastic.NewMatchQuery("flow_history_ids", 2), []int64{1, 2}},
3337
{elastic.NewRangeQuery("created_on").Gt("2017-01-01"), []int64{1, 6, 8}}, // created_on range
3438
{elastic.NewRangeQuery("last_seen_on").Lt("2019-01-01"), []int64{3, 4}}, // last_seen_on range
3539
{elastic.NewExistsQuery("last_seen_on"), []int64{1, 2, 3, 4, 5, 6}}, // last_seen_on is set

Diff for: testdb.sql

+17
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ CREATE TABLE contacts_contactgroup_contacts (
5252
contact_id integer NOT NULL REFERENCES contacts_contact(id)
5353
);
5454

55+
56+
DROP TABLE IF EXISTS flows_flowrun CASCADE;
57+
CREATE TABLE flows_flowrun (
58+
id SERIAL PRIMARY KEY,
59+
uuid character varying(36) NOT NULL,
60+
flow_id integer REFERENCES flows_flow(id),
61+
contact_id integer REFERENCES contacts_contact(id)
62+
);
63+
5564
INSERT INTO flows_flow(id, uuid, name) VALUES
5665
(1, '6d3cf1eb-546e-4fb8-a5ca-69187648fbf6', 'Favorites'),
5766
(2, '4eea8ff1-4fe2-4ce5-92a4-0870a499973a', 'Catch All');
@@ -152,3 +161,11 @@ INSERT INTO contacts_contactgroup_contacts(id, contact_id, contactgroup_id) VALU
152161
(1, 1, 1),
153162
(2, 1, 4),
154163
(3, 2, 4);
164+
165+
INSERT INTO flows_flowrun(id, uuid, flow_id, contact_id) VALUES
166+
(1, '8b30ee61-e19d-427e-bb9f-4b8cd2c31d0c', 1, 1),
167+
(2, '94639979-155e-444d-95e9-a39dad64dbd5', 1, 1),
168+
(3, '74d918df-0e31-4547-98a9-5d765450e2ac', 2, 1),
169+
(4, '14fdf8fc-6e02-4759-b9be-cacc5991cd14', 1, 2),
170+
(5, '5171b4d8-e9bb-46c1-901e-53e13f3afe5d', 2, 2),
171+
(6, '4cc84e32-910f-41d6-865d-63fe25db4cde', 1, 3);

0 commit comments

Comments
 (0)