Skip to content

Commit

Permalink
Merge pull request #612 from nyaruka/stopped_fix
Browse files Browse the repository at this point in the history
Fix unstopping of contacts who message in
  • Loading branch information
rowanseymour authored Apr 4, 2022
2 parents ecf0553 + 7841cd1 commit 36dd54a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
55 changes: 49 additions & 6 deletions core/tasks/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestMsgEvents(t *testing.T) {
expectedType models.MsgType
expectedFlow *testdata.Flow
}{
// 0:
{
org: testdata.Org1,
channel: testdata.TwitterChannel,
Expand All @@ -79,6 +80,8 @@ func TestMsgEvents(t *testing.T) {
expectedReply: "",
expectedType: models.MsgTypeInbox,
},

// 1:
{
org: testdata.Org1,
channel: testdata.TwitterChannel,
Expand All @@ -87,6 +90,8 @@ func TestMsgEvents(t *testing.T) {
expectedReply: "",
expectedType: models.MsgTypeInbox,
},

// 2:
{
org: testdata.Org1,
channel: testdata.TwitterChannel,
Expand All @@ -96,6 +101,8 @@ func TestMsgEvents(t *testing.T) {
expectedType: models.MsgTypeFlow,
expectedFlow: testdata.Favorites,
},

// 3:
{
org: testdata.Org1,
channel: testdata.TwitterChannel,
Expand All @@ -105,6 +112,8 @@ func TestMsgEvents(t *testing.T) {
expectedType: models.MsgTypeFlow,
expectedFlow: testdata.Favorites,
},

// 4:
{
org: testdata.Org1,
channel: testdata.TwitterChannel,
Expand All @@ -114,6 +123,8 @@ func TestMsgEvents(t *testing.T) {
expectedType: models.MsgTypeFlow,
expectedFlow: testdata.Favorites,
},

// 5:
{
org: testdata.Org1,
channel: testdata.TwitterChannel,
Expand All @@ -123,6 +134,8 @@ func TestMsgEvents(t *testing.T) {
expectedType: models.MsgTypeFlow,
expectedFlow: testdata.Favorites,
},

// 6:
{
org: testdata.Org1,
channel: testdata.TwitterChannel,
Expand All @@ -132,6 +145,8 @@ func TestMsgEvents(t *testing.T) {
expectedType: models.MsgTypeFlow,
expectedFlow: testdata.Favorites,
},

// 7:
{
org: testdata.Org1,
channel: testdata.TwitterChannel,
Expand All @@ -141,6 +156,7 @@ func TestMsgEvents(t *testing.T) {
expectedType: models.MsgTypeInbox,
},

// 8:
{
org: testdata.Org2,
channel: testdata.Org2Channel,
Expand All @@ -150,6 +166,8 @@ func TestMsgEvents(t *testing.T) {
expectedType: models.MsgTypeFlow,
expectedFlow: testdata.Org2SingleMessage,
},

// 9:
{
org: testdata.Org2,
channel: testdata.Org2Channel,
Expand All @@ -159,6 +177,8 @@ func TestMsgEvents(t *testing.T) {
expectedType: models.MsgTypeFlow,
expectedFlow: testdata.Org2Favorites,
},

// 10:
{
org: testdata.Org2,
channel: testdata.Org2Channel,
Expand All @@ -168,6 +188,8 @@ func TestMsgEvents(t *testing.T) {
expectedType: models.MsgTypeFlow,
expectedFlow: testdata.Org2Favorites,
},

// 11:
{
org: testdata.Org2,
channel: testdata.Org2Channel,
Expand All @@ -177,6 +199,8 @@ func TestMsgEvents(t *testing.T) {
expectedType: models.MsgTypeFlow,
expectedFlow: testdata.Org2Favorites,
},

// 12:
{
org: testdata.Org2,
channel: testdata.Org2Channel,
Expand All @@ -186,6 +210,8 @@ func TestMsgEvents(t *testing.T) {
expectedType: models.MsgTypeFlow,
expectedFlow: testdata.Org2Favorites,
},

// 13:
{
org: testdata.Org2,
channel: testdata.Org2Channel,
Expand All @@ -196,6 +222,7 @@ func TestMsgEvents(t *testing.T) {
expectedFlow: testdata.Org2SingleMessage,
},

// 14:
{
org: testdata.Org1,
channel: testdata.TwitterChannel,
Expand All @@ -206,7 +233,21 @@ func TestMsgEvents(t *testing.T) {
expectedFlow: testdata.IVRFlow,
},

// no URN on contact but handle event, session gets started but no message created
// 15: stopped contact should be unstopped
{
preHook: func() {
db.MustExec(`UPDATE contacts_contact SET status = 'S' WHERE id = $1`, testdata.George.ID)
},
org: testdata.Org1,
channel: testdata.TwitterChannel,
contact: testdata.George,
text: "start",
expectedReply: "What is your favorite color?",
expectedType: models.MsgTypeFlow,
expectedFlow: testdata.Favorites,
},

// 16: no URN on contact but handle event, session gets started but no message created
{
org: testdata.Org1,
channel: testdata.TwilioChannel,
Expand All @@ -217,7 +258,7 @@ func TestMsgEvents(t *testing.T) {
expectedFlow: testdata.Favorites,
},

// start Fred back in our favorite flow, then make it inactive, will be handled by catch-all
// 17: start Fred back in our favorite flow, then make it inactive, will be handled by catch-all
{
org: testdata.Org2,
channel: testdata.Org2Channel,
Expand All @@ -227,6 +268,8 @@ func TestMsgEvents(t *testing.T) {
expectedType: models.MsgTypeFlow,
expectedFlow: testdata.Org2Favorites,
},

// 18:
{
preHook: func() {
db.MustExec(`UPDATE flows_flow SET is_active = FALSE WHERE id = $1`, testdata.Org2Favorites.ID)
Expand All @@ -240,7 +283,7 @@ func TestMsgEvents(t *testing.T) {
expectedFlow: testdata.Org2SingleMessage,
},

// start Fred back in our favorites flow to test retries
// 19: start Fred back in our favorites flow to test retries
{
preHook: func() {
db.MustExec(`UPDATE flows_flow SET is_active = TRUE WHERE id = $1`, testdata.Org2Favorites.ID)
Expand Down Expand Up @@ -303,8 +346,8 @@ func TestMsgEvents(t *testing.T) {

// if we are meant to have a reply, check it
if tc.expectedReply != "" {
assertdb.Query(t, db, `SELECT text FROM msgs_msg WHERE contact_id = $1 AND created_on > $2 ORDER BY id DESC LIMIT 1`, tc.contact.ID, last).
Returns(tc.expectedReply, "%d: response mismatch", i)
assertdb.Query(t, db, `SELECT text, status FROM msgs_msg WHERE contact_id = $1 AND created_on > $2 ORDER BY id DESC LIMIT 1`, tc.contact.ID, last).
Columns(map[string]interface{}{"text": tc.expectedReply, "status": "Q"}, "%d: response mismatch", i)
}

// check any open tickets for this contact where updated
Expand All @@ -331,7 +374,7 @@ func TestMsgEvents(t *testing.T) {

// check messages queued to courier
testsuite.AssertCourierQueues(t, map[string][]int{
fmt.Sprintf("msgs:%s|10/1", testdata.TwitterChannel.UUID): {1, 1, 1, 1, 1},
fmt.Sprintf("msgs:%s|10/1", testdata.TwitterChannel.UUID): {1, 1, 1, 1, 1, 1},
fmt.Sprintf("msgs:%s|10/1", testdata.Org2Channel.UUID): {1, 1, 1, 1, 1, 1, 1, 1, 1},
})

Expand Down
12 changes: 6 additions & 6 deletions core/tasks/handler/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,6 @@ func handleMsgEvent(ctx context.Context, rt *runtime.Runtime, event *MsgEvent) e
}
}

// build our flow contact
contact, err := modelContact.FlowContact(oa)
if err != nil {
return errors.Wrapf(err, "error creating flow contact")
}

// if this channel is no longer active or this contact is blocked, ignore this message (mark it as handled)
if channel == nil || modelContact.Status() == models.ContactStatusBlocked {
err := models.UpdateMessage(ctx, rt.DB, event.MsgID, models.MsgStatusHandled, models.VisibilityArchived, models.MsgTypeInbox, models.NilFlowID, topupID)
Expand All @@ -540,6 +534,12 @@ func handleMsgEvent(ctx context.Context, rt *runtime.Runtime, event *MsgEvent) e
newContact = true
}

// build our flow contact
contact, err := modelContact.FlowContact(oa)
if err != nil {
return errors.Wrapf(err, "error creating flow contact")
}

// if this is a new contact, we need to calculate dynamic groups and campaigns
if newContact {
err = models.CalculateDynamicGroups(ctx, rt.DB, oa, []*flows.Contact{contact})
Expand Down

0 comments on commit 36dd54a

Please sign in to comment.