diff --git a/core/models/groups.go b/core/models/groups.go index 5a391c60d..ce8ab590b 100644 --- a/core/models/groups.go +++ b/core/models/groups.go @@ -376,7 +376,7 @@ func PopulateDynamicGroup(ctx context.Context, db *sqlx.DB, es *elastic.Client, return 0, errors.Wrapf(err, "error performing query: %s for group: %d", query, groupID) } - // find which need to be added or removed + // find which contacts need to be added or removed adds := make([]ContactID, 0, 100) for _, id := range new { if !present[id] { @@ -409,5 +409,15 @@ func PopulateDynamicGroup(ctx context.Context, db *sqlx.DB, es *elastic.Client, return 0, errors.Wrapf(err, "error marking dynamic group as ready") } + // finally update modified_on for all affected contacts to ensure these changes are seen by rp-indexer + changed := make([]ContactID, 0, len(adds)) + changed = append(changed, adds...) + changed = append(changed, removals...) + + err = UpdateContactModifiedOn(ctx, db, changed) + if err != nil { + return 0, errors.Wrapf(err, "error updating contact modified_on after group population") + } + return len(new), nil } diff --git a/core/tasks/contacts/populate_dynamic_group_test.go b/core/tasks/contacts/populate_dynamic_group_test.go index c4cc38484..686ede1ee 100644 --- a/core/tasks/contacts/populate_dynamic_group_test.go +++ b/core/tasks/contacts/populate_dynamic_group_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/nyaruka/gocommon/dates" "github.com/nyaruka/mailroom/core/tasks/contacts" "github.com/nyaruka/mailroom/testsuite" "github.com/nyaruka/mailroom/testsuite/testdata" @@ -52,6 +53,7 @@ func TestPopulateTask(t *testing.T) { }`, testdata.Cathy.ID) group := testdata.InsertContactGroup(db, testdata.Org1, "e52fee05-2f95-4445-aef6-2fe7dac2fd56", "Women", "gender = F") + start := dates.Now() task := &contacts.PopulateDynamicGroupTask{ GroupID: group.ID, @@ -61,4 +63,6 @@ func TestPopulateTask(t *testing.T) { require.NoError(t, err) testsuite.AssertQuery(t, db, `SELECT count(*) FROM contacts_contactgroup_contacts WHERE contactgroup_id = $1`, group.ID).Returns(1) + testsuite.AssertQuery(t, db, `SELECT contact_id FROM contacts_contactgroup_contacts WHERE contactgroup_id = $1`, group.ID).Returns(int64(testdata.Cathy.ID)) + testsuite.AssertQuery(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND modified_on > $2`, testdata.Cathy.ID, start).Returns(1) }