Skip to content

Commit

Permalink
Merge pull request rapidpro#464 from nyaruka/pop_dyn_group_fix
Browse files Browse the repository at this point in the history
🧯 Update contact `modified_on` after populate dynamic group task
  • Loading branch information
rowanseymour authored Jul 26, 2021
2 parents 7e0dded + 9d81dcb commit 59585f8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion core/models/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions core/tasks/contacts/populate_dynamic_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}

0 comments on commit 59585f8

Please sign in to comment.