diff --git a/indexers/contacts.go b/indexers/contacts.go index e15520d..64e059e 100644 --- a/indexers/contacts.go +++ b/indexers/contacts.go @@ -136,7 +136,10 @@ SELECT org_id, id, modified_on, is_active, row_to_json(t) FROM ( ) AS f ) AS fields, ( - SELECT array_to_json(array_agg(gc.contactgroup_id)) FROM contacts_contactgroup_contacts gc WHERE gc.contact_id = contacts_contact.id + SELECT array_to_json(array_agg(gc.contactgroup_id)) + FROM contacts_contactgroup_contacts gc + INNER JOIN contacts_contactgroup g ON g.id = gc.contactgroup_id + WHERE gc.contact_id = contacts_contact.id AND g.group_type IN ('M', 'Q') ) AS group_ids, current_flow_id AS flow_id, ( diff --git a/indexers/contacts_test.go b/indexers/contacts_test.go index bcc9943..814ec80 100644 --- a/indexers/contacts_test.go +++ b/indexers/contacts_test.go @@ -161,6 +161,7 @@ var contactQueryTests = []struct { {elastic.Match("group_ids", 1), []int64{1}}, {elastic.Match("group_ids", 4), []int64{1, 2}}, {elastic.Match("group_ids", 2), []int64{}}, + {elastic.Match("group_ids", 5), []int64{}}, // wrong group type } func TestContacts(t *testing.T) { @@ -202,7 +203,7 @@ func TestContacts(t *testing.T) { // now make some contact changes, removing one contact, updating another _, err = db.Exec(` - DELETE FROM contacts_contactgroup_contacts WHERE id = 3; + DELETE FROM contacts_contactgroup_contacts WHERE contact_id = 2 AND contactgroup_id = 4; UPDATE contacts_contact SET name = 'John Deer', modified_on = '2020-08-20 14:00:00+00' where id = 2; UPDATE contacts_contact SET is_active = FALSE, modified_on = '2020-08-22 15:00:00+00' where id = 4;`) require.NoError(t, err) diff --git a/testdb.sql b/testdb.sql index 0996fdf..9d7921f 100644 --- a/testdb.sql +++ b/testdb.sql @@ -42,7 +42,8 @@ DROP TABLE IF EXISTS contacts_contactgroup CASCADE; CREATE TABLE contacts_contactgroup ( id SERIAL PRIMARY KEY, uuid character varying(36) NOT NULL, - name character varying(128) NOT NULL + name character varying(128) NOT NULL, + group_type character varying(1) NOT NULL ); DROP TABLE IF EXISTS contacts_contactgroup_contacts CASCADE; @@ -151,16 +152,19 @@ INSERT INTO contacts_contacturn(id, contact_id, scheme, org_id, priority, path, (10, 9, 'twitterid', 2, 90, 1000001, 'fungal', 'twitterid:1000001'), (11, 10, 'whatsapp', 2, 90, 1000003, NULL, 'whatsapp:1000003'); -INSERT INTO contacts_contactgroup(id, uuid, name) VALUES -(1, '4ea0f313-2f62-4e57-bdf0-232b5191dd57', 'Group 1'), -(2, '4c016340-468d-4675-a974-15cb7a45a5ab', 'Group 2'), -(3, 'e61b5bf7-8ddf-4e05-b0a8-4c46a6b68cff', 'Group 3'), -(4, '529bac39-550a-4d6f-817c-1833f3449007', 'Group 4'); +INSERT INTO contacts_contactgroup(id, uuid, name, group_type) VALUES +(1, '4ea0f313-2f62-4e57-bdf0-232b5191dd57', 'Group 1', 'Q'), +(2, '4c016340-468d-4675-a974-15cb7a45a5ab', 'Group 2', 'M'), +(3, 'e61b5bf7-8ddf-4e05-b0a8-4c46a6b68cff', 'Group 3', 'Q'), +(4, '529bac39-550a-4d6f-817c-1833f3449007', 'Group 4', 'M'), +(5, '004c982b-859e-4e7b-9c30-e38c0e6b6537', 'Active', 'A'); INSERT INTO contacts_contactgroup_contacts(id, contact_id, contactgroup_id) VALUES (1, 1, 1), (2, 1, 4), -(3, 2, 4); +(3, 1, 5), +(4, 2, 4), +(5, 2, 5); INSERT INTO flows_flowrun(id, uuid, flow_id, contact_id) VALUES (1, '8b30ee61-e19d-427e-bb9f-4b8cd2c31d0c', 1, 1),