Skip to content

Commit

Permalink
Merge pull request #39 from nyaruka/flow_history
Browse files Browse the repository at this point in the history
Track history of flow ids on contacts
  • Loading branch information
rowanseymour authored Mar 31, 2022
2 parents c99c739 + bace1d5 commit 1ad3e88
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion indexers/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions indexers/contacts.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@
"flow": {
"type": "keyword"
},
"flow_id": {
"type": "integer"
},
"flow_history_ids": {
"type": "integer"
},
"tickets": {
"type": "integer"
},
Expand Down
4 changes: 4 additions & 0 deletions indexers/contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions testdb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);

0 comments on commit 1ad3e88

Please sign in to comment.