Skip to content

Commit

Permalink
Merge pull request rapidpro#167 from nyaruka/fix-create-contact
Browse files Browse the repository at this point in the history
🚨 Fix create contact failing to start
  • Loading branch information
nicpottier authored Aug 19, 2019
2 parents 35ef56d + cec1d1a commit 4679f02
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 36 deletions.
28 changes: 14 additions & 14 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ func TestMsgEvents(t *testing.T) {

db.MustExec(
`INSERT INTO triggers_trigger(is_active, created_on, modified_on, keyword, is_archived,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id, trigger_count)
VALUES(TRUE, now(), now(), 'start', false, $1, 'K', 'O', 1, 1, 1, 0) RETURNING id`, models.FavoritesFlowID)
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id)
VALUES(TRUE, now(), now(), 'start', false, $1, 'K', 'O', 1, 1, 1) RETURNING id`, models.FavoritesFlowID)

db.MustExec(
`INSERT INTO triggers_trigger(is_active, created_on, modified_on, keyword, is_archived,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id, trigger_count)
VALUES(TRUE, now(), now(), 'start', false, $1, 'K', 'O', 1, 1, 2, 0) RETURNING id`, models.Org2FavoritesFlowID)
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id)
VALUES(TRUE, now(), now(), 'start', false, $1, 'K', 'O', 1, 1, 2) RETURNING id`, models.Org2FavoritesFlowID)

db.MustExec(
`INSERT INTO triggers_trigger(is_active, created_on, modified_on, keyword, is_archived,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id, trigger_count)
VALUES(TRUE, now(), now(), '', false, $1, 'C', 'O', 1, 1, 2, 0) RETURNING id`, models.Org2SingleMessageFlowID)
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id)
VALUES(TRUE, now(), now(), '', false, $1, 'C', 'O', 1, 1, 2) RETURNING id`, models.Org2SingleMessageFlowID)

db.MustExec(
`INSERT INTO triggers_trigger(is_active, created_on, modified_on, keyword, is_archived,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id, trigger_count)
VALUES(TRUE, now(), now(), 'ivr', false, $1, 'K', 'O', 1, 1, 1, 0) RETURNING id`, models.IVRFlowID)
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id)
VALUES(TRUE, now(), now(), 'ivr', false, $1, 'K', 'O', 1, 1, 1) RETURNING id`, models.IVRFlowID)

// clear all of Alexandria's URNs
db.MustExec(`UPDATE contacts_contacturn SET contact_id = NULL WHERE contact_id = $1`, models.AlexandriaID)
Expand Down Expand Up @@ -233,15 +233,15 @@ func TestChannelEvents(t *testing.T) {
// trigger on our twitter channel for new conversations and favorites flow
db.MustExec(
`INSERT INTO triggers_trigger(is_active, created_on, modified_on, keyword, is_archived,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id, trigger_count, channel_id)
VALUES(TRUE, now(), now(), NULL, false, $1, 'N', NULL, 1, 1, 1, 0, $2) RETURNING id`,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id, channel_id)
VALUES(TRUE, now(), now(), NULL, false, $1, 'N', NULL, 1, 1, 1, $2) RETURNING id`,
models.FavoritesFlowID, models.TwitterChannelID)

// trigger on our nexmo channel for referral and number flow
db.MustExec(
`INSERT INTO triggers_trigger(is_active, created_on, modified_on, keyword, is_archived,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id, trigger_count, channel_id)
VALUES(TRUE, now(), now(), NULL, false, $1, 'R', NULL, 1, 1, 1, 0, $2) RETURNING id`,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id, channel_id)
VALUES(TRUE, now(), now(), NULL, false, $1, 'R', NULL, 1, 1, 1, $2) RETURNING id`,
models.PickNumberFlowID, models.NexmoChannelID)

// add a URN for cathy so we can test twitter URNs
Expand Down Expand Up @@ -357,8 +357,8 @@ func TestTimedEvents(t *testing.T) {
// start to start our favorites flow
db.MustExec(
`INSERT INTO triggers_trigger(is_active, created_on, modified_on, keyword, is_archived,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id, trigger_count)
VALUES(TRUE, now(), now(), 'start', false, $1, 'K', 'O', 1, 1, 1, 0) RETURNING id`,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id)
VALUES(TRUE, now(), now(), 'start', false, $1, 'K', 'O', 1, 1, 1) RETURNING id`,
models.FavoritesFlowID,
)

Expand Down
Binary file modified mailroom_test.dump
Binary file not shown.
4 changes: 2 additions & 2 deletions models/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func insertTrigger(t *testing.T, db *sqlx.DB, active bool, flowID FlowID, trigge
var triggerID TriggerID
err := db.Get(&triggerID,
`INSERT INTO triggers_trigger(is_active, created_on, modified_on, keyword, referrer_id, is_archived,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id, trigger_count, channel_id)
VALUES($1, now(), now(), $2, $6, false, $3, $4, $5, 1, 1, 1, 0, $7) RETURNING id`, active, keyword, flowID, triggerType, matchType, referrerID, channelID)
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id, channel_id)
VALUES($1, now(), now(), $2, $6, false, $3, $4, $5, 1, 1, 1, $7) RETURNING id`, active, keyword, flowID, triggerType, matchType, referrerID, channelID)

assert.NoError(t, err)

Expand Down
7 changes: 2 additions & 5 deletions starts/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,14 @@ func CreateFlowBatches(ctx context.Context, db *sqlx.DB, rp *redis.Pool, ec *ela

// if we are meant to create a new contact, do so
if start.CreateContact() {
if org == nil {
org, err = models.GetOrgAssets(ctx, db, start.OrgID())
if err != nil {
return errors.Wrapf(err, "error loading org assets")
}
if assets == nil {
assets, err = models.GetSessionAssets(org)
if err != nil {
return errors.Wrapf(err, "error loading session assets")
}
}

logrus.WithField("assets", assets).WithField("fields", assets.Fields()).Error("creating contact")
newID, err := models.CreateContact(ctx, db, org, assets, urns.NilURN)
if err != nil {
return errors.Wrapf(err, "error creating new contact")
Expand Down
115 changes: 104 additions & 11 deletions starts/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ func TestStarts(t *testing.T) {
VALUES($1, TRUE, now(), now(), FALSE, $2, $3, 1);`, uuids.New(), models.GeorgeID, models.SingleMessageFlowID)

tcs := []struct {
Label string
FlowID models.FlowID
GroupIDs []models.GroupID
ContactIDs []models.ContactID
CreateContact bool
Query string
QueryResponse string
RestartParticipants models.RestartParticipants
Expand All @@ -53,15 +55,89 @@ func TestStarts(t *testing.T) {
BatchCount int
TotalCount int
}{
{models.SingleMessageFlowID, nil, nil, "", "", false, false, queue.BatchQueue, 0, 0, 0},
{models.SingleMessageFlowID, []models.GroupID{models.DoctorsGroupID}, nil, "", "", false, false, queue.BatchQueue, 121, 2, 121},
{models.SingleMessageFlowID, []models.GroupID{models.DoctorsGroupID}, []models.ContactID{models.CathyID}, "", "", false, false, queue.BatchQueue, 121, 2, 0},
{models.SingleMessageFlowID, nil, []models.ContactID{models.CathyID}, "", "", true, true, queue.HandlerQueue, 1, 1, 1},
{models.SingleMessageFlowID, []models.GroupID{models.DoctorsGroupID}, []models.ContactID{models.BobID}, "", "", false, false, queue.BatchQueue, 122, 2, 1},
{models.SingleMessageFlowID, nil, []models.ContactID{models.BobID}, "", "", false, false, queue.HandlerQueue, 1, 1, 0},
{models.SingleMessageFlowID, nil, []models.ContactID{models.BobID}, "", "", false, true, queue.HandlerQueue, 1, 1, 0},
{models.SingleMessageFlowID, nil, []models.ContactID{models.BobID}, "", "", true, true, queue.HandlerQueue, 1, 1, 1},
{models.SingleMessageFlowID, nil, nil, "bob", fmt.Sprintf(`{
{
Label: "empty flow start",
FlowID: models.SingleMessageFlowID,
Queue: queue.BatchQueue,
ContactCount: 0,
BatchCount: 0,
TotalCount: 0,
},
{
Label: "Single group",
FlowID: models.SingleMessageFlowID,
GroupIDs: []models.GroupID{models.DoctorsGroupID},
Queue: queue.BatchQueue,
ContactCount: 121,
BatchCount: 2,
TotalCount: 121,
},
{
Label: "Group and Contact (but all already active)",
FlowID: models.SingleMessageFlowID,
GroupIDs: []models.GroupID{models.DoctorsGroupID},
ContactIDs: []models.ContactID{models.CathyID},
Queue: queue.BatchQueue,
ContactCount: 121,
BatchCount: 2,
TotalCount: 0,
},
{
Label: "Contact restart",
FlowID: models.SingleMessageFlowID,
ContactIDs: []models.ContactID{models.CathyID},
RestartParticipants: true,
IncludeActive: true,
Queue: queue.HandlerQueue,
ContactCount: 1,
BatchCount: 1,
TotalCount: 1,
},
{
Label: "Previous group and one new contact",
FlowID: models.SingleMessageFlowID,
GroupIDs: []models.GroupID{models.DoctorsGroupID},
ContactIDs: []models.ContactID{models.BobID},
Queue: queue.BatchQueue,
ContactCount: 122,
BatchCount: 2,
TotalCount: 1,
},
{
Label: "Single contact, no restart",
FlowID: models.SingleMessageFlowID,
ContactIDs: []models.ContactID{models.BobID},
Queue: queue.HandlerQueue,
ContactCount: 1,
BatchCount: 1,
TotalCount: 0,
},
{
Label: "Single contact, include active, but no restart",
FlowID: models.SingleMessageFlowID,
ContactIDs: []models.ContactID{models.BobID},
IncludeActive: true,
Queue: queue.HandlerQueue,
ContactCount: 1,
BatchCount: 1,
TotalCount: 0,
},
{
Label: "Single contact, include active and restart",
FlowID: models.SingleMessageFlowID,
ContactIDs: []models.ContactID{models.BobID},
RestartParticipants: true,
IncludeActive: true,
Queue: queue.HandlerQueue,
ContactCount: 1,
BatchCount: 1,
TotalCount: 1,
},
{
Label: "Query start",
FlowID: models.SingleMessageFlowID,
Query: "bob",
QueryResponse: fmt.Sprintf(`{
"_scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAbgc0WS1hqbHlfb01SM2lLTWJRMnVOSVZDdw==",
"took": 2,
"timed_out": false,
Expand All @@ -87,7 +163,23 @@ func TestStarts(t *testing.T) {
}
]
}
}`, models.BobID), true, true, queue.HandlerQueue, 1, 1, 1},
}`, models.BobID),
RestartParticipants: true,
IncludeActive: true,
Queue: queue.HandlerQueue,
ContactCount: 1,
BatchCount: 1,
TotalCount: 1,
},
{
Label: "New Contact",
FlowID: models.SingleMessageFlowID,
CreateContact: true,
Queue: queue.HandlerQueue,
ContactCount: 1,
BatchCount: 1,
TotalCount: 1,
},
}

for i, tc := range tcs {
Expand All @@ -97,7 +189,8 @@ func TestStarts(t *testing.T) {
start := models.NewFlowStart(models.Org1, models.MessagingFlow, tc.FlowID, tc.RestartParticipants, tc.IncludeActive).
WithGroupIDs(tc.GroupIDs).
WithContactIDs(tc.ContactIDs).
WithQuery(tc.Query)
WithQuery(tc.Query).
WithCreateContact(tc.CreateContact)

err := models.InsertFlowStarts(ctx, db, []*models.FlowStart{start})
assert.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions web/simulation/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,16 @@ func TestServer(t *testing.T) {
// add a trigger for our campaign flow with 'trigger'
db.MustExec(
`INSERT INTO triggers_trigger(is_active, created_on, modified_on, keyword, is_archived,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id, trigger_count)
VALUES(TRUE, now(), now(), 'trigger', false, $1, 'K', 'O', 1, 1, 1, 0) RETURNING id`,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id)
VALUES(TRUE, now(), now(), 'trigger', false, $1, 'K', 'O', 1, 1, 1) RETURNING id`,
models.CampaignFlowID,
)

// also add a catch all
db.MustExec(
`INSERT INTO triggers_trigger(is_active, created_on, modified_on, keyword, is_archived,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id, trigger_count)
VALUES(TRUE, now(), now(), NULL, false, $1, 'C', NULL, 1, 1, 1, 0) RETURNING id`,
flow_id, trigger_type, match_type, created_by_id, modified_by_id, org_id)
VALUES(TRUE, now(), now(), NULL, false, $1, 'C', NULL, 1, 1, 1) RETURNING id`,
models.CampaignFlowID,
)

Expand Down

0 comments on commit 4679f02

Please sign in to comment.