Skip to content

Commit

Permalink
Stop writing is_blocked and is_stopped
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Aug 25, 2020
1 parent 286acd1 commit cbdd5f7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 26 deletions.
10 changes: 5 additions & 5 deletions hooks/contact_status_changed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestContactStatusChanged(t *testing.T) {
db := testsuite.DB()

// make sure cathyID contact is active
db.Exec(`UPDATE contacts_contact SET status = 'A', is_blocked = FALSE, is_stopped = FALSE WHERE id = $1`, models.CathyID)
db.Exec(`UPDATE contacts_contact SET status = 'A' WHERE id = $1`, models.CathyID)

tcs := []HookTestCase{
{
Expand All @@ -23,7 +23,7 @@ func TestContactStatusChanged(t *testing.T) {
},
SQLAssertions: []SQLAssertion{
{
SQL: `select count(*) from contacts_contact where id = $1 AND status = 'B' AND is_blocked = TRUE`,
SQL: `select count(*) from contacts_contact where id = $1 AND status = 'B'`,
Args: []interface{}{models.CathyID},
Count: 1,
},
Expand All @@ -35,7 +35,7 @@ func TestContactStatusChanged(t *testing.T) {
},
SQLAssertions: []SQLAssertion{
{
SQL: `select count(*) from contacts_contact where id = $1 AND status = 'S' AND is_stopped = TRUE`,
SQL: `select count(*) from contacts_contact where id = $1 AND status = 'S'`,
Args: []interface{}{models.CathyID},
Count: 1,
},
Expand All @@ -47,12 +47,12 @@ func TestContactStatusChanged(t *testing.T) {
},
SQLAssertions: []SQLAssertion{
{
SQL: `select count(*) from contacts_contact where id = $1 AND status = 'A' AND is_stopped = FALSE`,
SQL: `select count(*) from contacts_contact where id = $1 AND status = 'A'`,
Args: []interface{}{models.CathyID},
Count: 1,
},
{
SQL: `select count(*) from contacts_contact where id = $1 AND status = 'A' AND is_blocked = FALSE`,
SQL: `select count(*) from contacts_contact where id = $1 AND status = 'A'`,
Args: []interface{}{models.CathyID},
Count: 1,
},
Expand Down
Binary file modified mailroom_test.dump
Binary file not shown.
19 changes: 6 additions & 13 deletions models/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,9 @@ func (c *Contact) URNForID(urnID URNID) urns.URN {
return urns.NilURN
}

// Unstop sets the is_stopped attribute to false for this contact
// Unstop sets the status to stopped for this contact
func (c *Contact) Unstop(ctx context.Context, db *sqlx.DB) error {
_, err := db.ExecContext(ctx, `UPDATE contacts_contact SET status = 'A', is_stopped = FALSE, modified_on = NOW() WHERE id = $1`, c.id)
_, err := db.ExecContext(ctx, `UPDATE contacts_contact SET status = 'A', modified_on = NOW() WHERE id = $1`, c.id)
if err != nil {
return errors.Wrapf(err, "error unstopping contact")
}
Expand Down Expand Up @@ -630,9 +630,9 @@ func CreateContact(ctx context.Context, db *sqlx.DB, org *OrgAssets, urn urns.UR
err = tx.GetContext(ctx, &contactID,
`INSERT INTO
contacts_contact
(org_id, is_active, status, is_blocked, is_stopped, uuid, created_on, modified_on, created_by_id, modified_by_id, name)
(org_id, is_active, status, uuid, created_on, modified_on, created_by_id, modified_by_id, name)
VALUES
($1, TRUE, 'A', FALSE, FALSE, $2, NOW(), NOW(), 1, 1, '')
($1, TRUE, 'A', $2, NOW(), NOW(), 1, 1, '')
RETURNING id`,
org.OrgID(), uuids.New(),
)
Expand Down Expand Up @@ -932,7 +932,6 @@ const markContactStoppedSQL = `
UPDATE
contacts_contact
SET
is_stopped = TRUE,
status = 'S',
modified_on = NOW()
WHERE
Expand Down Expand Up @@ -1330,8 +1329,6 @@ type ContactStatusChange struct {
type contactStatusUpdate struct {
ContactID ContactID `db:"id"`
Status ContactStatus `db:"status"`
Blocked bool `db:"is_blocked"`
Stopped bool `db:"is_stopped"`
}

// UpdateContactStatus updates the contacts status as the passed changes
Expand All @@ -1354,8 +1351,6 @@ func UpdateContactStatus(ctx context.Context, tx Queryer, changes []*ContactStat
&contactStatusUpdate{
ContactID: ch.ContactID,
Status: status,
Blocked: blocked,
Stopped: stopped,
},
)

Expand All @@ -1380,13 +1375,11 @@ const updateContactStatusSQL = `
contacts_contact c
SET
status = r.status,
is_blocked = r.is_blocked::boolean,
is_stopped = r.is_stopped::boolean,
modified_on = NOW()
FROM (
VALUES(:id, :status, :is_blocked, :is_stopped)
VALUES(:id, :status)
) AS
r(id, status, is_blocked, is_stopped)
r(id, status)
WHERE
c.id = r.id::int
`
14 changes: 7 additions & 7 deletions models/contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func TestStopContact(t *testing.T) {
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contactgroup_contacts WHERE contact_id = $1`, []interface{}{CathyID}, 1)

// verify she's stopped
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'S' AND is_stopped = TRUE AND is_active = TRUE and is_blocked = FALSE`, []interface{}{CathyID}, 1)
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'S' AND is_active = TRUE`, []interface{}{CathyID}, 1)
}

func TestUpdateContactLastSeenAndModifiedOn(t *testing.T) {
Expand Down Expand Up @@ -405,24 +405,24 @@ func TestUpdateContactStatus(t *testing.T) {
err := UpdateContactStatus(ctx, db, []*ContactStatusChange{})
assert.NoError(t, err)

testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'B' AND is_blocked = TRUE`, []interface{}{CathyID}, 0)
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'S' AND is_stopped = TRUE`, []interface{}{CathyID}, 0)
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'B'`, []interface{}{CathyID}, 0)
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'S'`, []interface{}{CathyID}, 0)

changes := make([]*ContactStatusChange, 0, 1)
changes = append(changes, &ContactStatusChange{CathyID, flows.ContactStatusBlocked})

err = UpdateContactStatus(ctx, db, changes)

testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'B' AND is_blocked = TRUE`, []interface{}{CathyID}, 1)
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'S' AND is_stopped = TRUE`, []interface{}{CathyID}, 0)
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'B'`, []interface{}{CathyID}, 1)
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'S'`, []interface{}{CathyID}, 0)

changes = make([]*ContactStatusChange, 0, 1)
changes = append(changes, &ContactStatusChange{CathyID, flows.ContactStatusStopped})

err = UpdateContactStatus(ctx, db, changes)

testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'B' AND is_blocked = TRUE`, []interface{}{CathyID}, 0)
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'S' AND is_stopped = TRUE`, []interface{}{CathyID}, 1)
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'B'`, []interface{}{CathyID}, 0)
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'S'`, []interface{}{CathyID}, 1)

}

Expand Down
2 changes: 1 addition & 1 deletion tasks/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func TestStopEvent(t *testing.T) {
testsuite.AssertQueryCount(t, db, `SELECT count(*) from contacts_contactgroup_contacts WHERE contactgroup_id = $1 AND contact_id = $2`, []interface{}{models.DoctorsGroupID, models.GeorgeID}, 1)

// that cathy is stopped
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'S' AND is_stopped = TRUE`, []interface{}{models.CathyID}, 1)
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM contacts_contact WHERE id = $1 AND status = 'S'`, []interface{}{models.CathyID}, 1)

// and has no upcoming events
testsuite.AssertQueryCount(t, db, `SELECT count(*) FROM campaigns_eventfire WHERE contact_id = $1`, []interface{}{models.CathyID}, 0)
Expand Down

0 comments on commit cbdd5f7

Please sign in to comment.