From 6cb728796d82cf2bc5625486298d6b77b747758e Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Mon, 9 Oct 2023 14:44:48 +0200 Subject: [PATCH] Fix filtering by trigger optional channel --- core/models/triggers.go | 14 +- core/models/triggers_test.go | 139 ++++++++++++------ core/tasks/handler/contact_tasks.go | 6 +- .../handler/handle_contact_event_test.go | 12 +- testsuite/testdata/triggers.go | 16 +- web/simulation/simulation.go | 2 +- web/simulation/simulation_test.go | 6 +- 7 files changed, 125 insertions(+), 70 deletions(-) diff --git a/core/models/triggers.go b/core/models/triggers.go index 1d0cdcec3..3b4c12eb7 100644 --- a/core/models/triggers.go +++ b/core/models/triggers.go @@ -116,7 +116,7 @@ func loadTriggers(ctx context.Context, db *sql.DB, orgID OrgID) ([]*Trigger, err } // FindMatchingMsgTrigger finds the best match trigger for an incoming message from the given contact -func FindMatchingMsgTrigger(oa *OrgAssets, contact *flows.Contact, text string) *Trigger { +func FindMatchingMsgTrigger(oa *OrgAssets, channel *Channel, contact *flows.Contact, text string) *Trigger { // determine our message keyword words := utils.TokenizeString(text) keyword := "" @@ -132,28 +132,28 @@ func FindMatchingMsgTrigger(oa *OrgAssets, contact *flows.Contact, text string) }) // if we have a matching keyword trigger return that, otherwise we move on to catchall triggers.. - byKeyword := findBestTriggerMatch(candidates, nil, contact) + byKeyword := findBestTriggerMatch(candidates, channel, contact) if byKeyword != nil { return byKeyword } candidates = findTriggerCandidates(oa, CatchallTriggerType, nil) - return findBestTriggerMatch(candidates, nil, contact) + return findBestTriggerMatch(candidates, channel, contact) } // FindMatchingIncomingCallTrigger finds the best match trigger for incoming calls -func FindMatchingIncomingCallTrigger(oa *OrgAssets, contact *flows.Contact) *Trigger { +func FindMatchingIncomingCallTrigger(oa *OrgAssets, channel *Channel, contact *flows.Contact) *Trigger { candidates := findTriggerCandidates(oa, IncomingCallTriggerType, nil) - return findBestTriggerMatch(candidates, nil, contact) + return findBestTriggerMatch(candidates, channel, contact) } // FindMatchingMissedCallTrigger finds the best match trigger for missed incoming calls -func FindMatchingMissedCallTrigger(oa *OrgAssets) *Trigger { +func FindMatchingMissedCallTrigger(oa *OrgAssets, channel *Channel) *Trigger { candidates := findTriggerCandidates(oa, MissedCallTriggerType, nil) - return findBestTriggerMatch(candidates, nil, nil) + return findBestTriggerMatch(candidates, channel, nil) } // FindMatchingNewConversationTrigger finds the best match trigger for new conversation channel events diff --git a/core/models/triggers_test.go b/core/models/triggers_test.go index add89ce2d..168f97a0f 100644 --- a/core/models/triggers_test.go +++ b/core/models/triggers_test.go @@ -23,7 +23,7 @@ func TestLoadTriggers(t *testing.T) { farmersGroup := testdata.InsertContactGroup(rt, testdata.Org1, assets.GroupUUID(uuids.New()), "Farmers", "") // create trigger for other org to ensure it isn't loaded - testdata.InsertCatchallTrigger(rt, testdata.Org2, testdata.Org2Favorites, nil, nil) + testdata.InsertCatchallTrigger(rt, testdata.Org2, testdata.Org2Favorites, nil, nil, nil) tcs := []struct { id models.TriggerID @@ -38,14 +38,22 @@ func TestLoadTriggers(t *testing.T) { channelID models.ChannelID }{ { - id: testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.Favorites, "join", models.MatchFirst, nil, nil), + id: testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.Favorites, "join", models.MatchFirst, nil, nil, nil), type_: models.KeywordTriggerType, flowID: testdata.Favorites.ID, keyword: "join", keywordMatchType: models.MatchFirst, }, { - id: testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.PickANumber, "start", models.MatchOnly, []*testdata.Group{testdata.DoctorsGroup, testdata.TestersGroup}, []*testdata.Group{farmersGroup}), + id: testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.Favorites, "join", models.MatchFirst, nil, nil, testdata.TwilioChannel), + type_: models.KeywordTriggerType, + flowID: testdata.Favorites.ID, + keyword: "join", + keywordMatchType: models.MatchFirst, + channelID: testdata.TwilioChannel.ID, + }, + { + id: testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.PickANumber, "start", models.MatchOnly, []*testdata.Group{testdata.DoctorsGroup, testdata.TestersGroup}, []*testdata.Group{farmersGroup}, nil), type_: models.KeywordTriggerType, flowID: testdata.PickANumber.ID, keyword: "start", @@ -54,14 +62,22 @@ func TestLoadTriggers(t *testing.T) { excludeGroups: []models.GroupID{farmersGroup.ID}, }, { - id: testdata.InsertIncomingCallTrigger(rt, testdata.Org1, testdata.Favorites, []*testdata.Group{testdata.DoctorsGroup, testdata.TestersGroup}, []*testdata.Group{farmersGroup}), + id: testdata.InsertIncomingCallTrigger(rt, testdata.Org1, testdata.Favorites, []*testdata.Group{testdata.DoctorsGroup, testdata.TestersGroup}, []*testdata.Group{farmersGroup}, nil), type_: models.IncomingCallTriggerType, flowID: testdata.Favorites.ID, includeGroups: []models.GroupID{testdata.DoctorsGroup.ID, testdata.TestersGroup.ID}, excludeGroups: []models.GroupID{farmersGroup.ID}, }, { - id: testdata.InsertMissedCallTrigger(rt, testdata.Org1, testdata.Favorites), + id: testdata.InsertIncomingCallTrigger(rt, testdata.Org1, testdata.Favorites, []*testdata.Group{testdata.DoctorsGroup, testdata.TestersGroup}, []*testdata.Group{farmersGroup}, testdata.TwilioChannel), + type_: models.IncomingCallTriggerType, + flowID: testdata.Favorites.ID, + includeGroups: []models.GroupID{testdata.DoctorsGroup.ID, testdata.TestersGroup.ID}, + excludeGroups: []models.GroupID{farmersGroup.ID}, + channelID: testdata.TwilioChannel.ID, + }, + { + id: testdata.InsertMissedCallTrigger(rt, testdata.Org1, testdata.Favorites, nil), type_: models.MissedCallTriggerType, flowID: testdata.Favorites.ID, }, @@ -84,10 +100,16 @@ func TestLoadTriggers(t *testing.T) { channelID: testdata.TwilioChannel.ID, }, { - id: testdata.InsertCatchallTrigger(rt, testdata.Org1, testdata.Favorites, nil, nil), + id: testdata.InsertCatchallTrigger(rt, testdata.Org1, testdata.Favorites, nil, nil, nil), type_: models.CatchallTriggerType, flowID: testdata.Favorites.ID, }, + { + id: testdata.InsertCatchallTrigger(rt, testdata.Org1, testdata.Favorites, nil, nil, testdata.TwilioChannel), + type_: models.CatchallTriggerType, + flowID: testdata.Favorites.ID, + channelID: testdata.TwilioChannel.ID, + }, } oa, err := models.GetOrgAssetsWithRefresh(ctx, rt, testdata.Org1.ID, models.RefreshTriggers) @@ -118,16 +140,19 @@ func TestFindMatchingMsgTrigger(t *testing.T) { rt.DB.MustExec(`DELETE FROM triggers_trigger`) - joinID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.Favorites, "join", models.MatchFirst, nil, nil) - resistID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.SingleMessage, "resist", models.MatchOnly, nil, nil) - emojiID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.PickANumber, "👍", models.MatchFirst, nil, nil) - doctorsID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.SingleMessage, "resist", models.MatchOnly, []*testdata.Group{testdata.DoctorsGroup}, nil) - doctorsAndNotTestersID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.SingleMessage, "resist", models.MatchOnly, []*testdata.Group{testdata.DoctorsGroup}, []*testdata.Group{testdata.TestersGroup}) - doctorsCatchallID := testdata.InsertCatchallTrigger(rt, testdata.Org1, testdata.SingleMessage, []*testdata.Group{testdata.DoctorsGroup}, nil) - othersAllID := testdata.InsertCatchallTrigger(rt, testdata.Org1, testdata.SingleMessage, nil, nil) + joinID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.Favorites, "join", models.MatchFirst, nil, nil, nil) + joinTwilioOnlyID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.Favorites, "join", models.MatchFirst, nil, nil, testdata.TwilioChannel) + startTwilioOnlyID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.Favorites, "start", models.MatchFirst, nil, nil, testdata.TwilioChannel) + resistID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.SingleMessage, "resist", models.MatchOnly, nil, nil, nil) + resistTwilioOnlyID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.SingleMessage, "resist", models.MatchOnly, nil, nil, testdata.TwilioChannel) + emojiID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.PickANumber, "👍", models.MatchFirst, nil, nil, nil) + doctorsID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.SingleMessage, "resist", models.MatchOnly, []*testdata.Group{testdata.DoctorsGroup}, nil, nil) + doctorsAndNotTestersID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.SingleMessage, "resist", models.MatchOnly, []*testdata.Group{testdata.DoctorsGroup}, []*testdata.Group{testdata.TestersGroup}, nil) + doctorsCatchallID := testdata.InsertCatchallTrigger(rt, testdata.Org1, testdata.SingleMessage, []*testdata.Group{testdata.DoctorsGroup}, nil, nil) + othersAllID := testdata.InsertCatchallTrigger(rt, testdata.Org1, testdata.SingleMessage, nil, nil, nil) // trigger for other org - testdata.InsertCatchallTrigger(rt, testdata.Org2, testdata.Org2Favorites, nil, nil) + testdata.InsertCatchallTrigger(rt, testdata.Org2, testdata.Org2Favorites, nil, nil, nil) oa, err := models.GetOrgAssetsWithRefresh(ctx, rt, testdata.Org1.ID, models.RefreshTriggers) require.NoError(t, err) @@ -139,28 +164,40 @@ func TestFindMatchingMsgTrigger(t *testing.T) { _, george, _ := testdata.George.Load(rt, oa) _, bob, _ := testdata.Bob.Load(rt, oa) + twilioChannels, _ := models.GetChannelsByID(ctx, rt.DB.DB, []models.ChannelID{testdata.TwilioChannel.ID}) + facebookChannels, _ := models.GetChannelsByID(ctx, rt.DB.DB, []models.ChannelID{testdata.FacebookChannel.ID}) + tcs := []struct { text string + channel *models.Channel contact *flows.Contact expectedTriggerID models.TriggerID }{ - {" join ", cathy, joinID}, - {"JOIN", cathy, joinID}, - {"join this", cathy, joinID}, - {"resist", george, resistID}, - {"resist", bob, doctorsID}, - {"resist", cathy, doctorsAndNotTestersID}, - {"resist this", cathy, doctorsCatchallID}, - {" 👍 ", george, emojiID}, - {"👍🏾", george, emojiID}, // is 👍 + 🏾 - {"😀👍", george, othersAllID}, - {"other", cathy, doctorsCatchallID}, - {"other", george, othersAllID}, - {"", george, othersAllID}, + {" join ", nil, cathy, joinID}, + {"JOIN", nil, cathy, joinID}, + {"JOIN", twilioChannels[0], cathy, joinTwilioOnlyID}, + {"JOIN", facebookChannels[0], cathy, joinID}, + {"join this", nil, cathy, joinID}, + {"resist", nil, george, resistID}, + {"resist", twilioChannels[0], george, resistTwilioOnlyID}, + {"resist", nil, bob, doctorsID}, + {"resist", twilioChannels[0], cathy, resistTwilioOnlyID}, + {"resist", nil, cathy, doctorsAndNotTestersID}, + {"resist this", nil, cathy, doctorsCatchallID}, + {" 👍 ", nil, george, emojiID}, + {"👍🏾", nil, george, emojiID}, // is 👍 + 🏾 + {"😀👍", nil, george, othersAllID}, + {"other", nil, cathy, doctorsCatchallID}, + {"other", nil, george, othersAllID}, + {"", nil, george, othersAllID}, + {"start", twilioChannels[0], cathy, startTwilioOnlyID}, + {"start", facebookChannels[0], cathy, doctorsCatchallID}, + {"start", twilioChannels[0], george, startTwilioOnlyID}, + {"start", facebookChannels[0], george, othersAllID}, } for _, tc := range tcs { - trigger := models.FindMatchingMsgTrigger(oa, tc.contact, tc.text) + trigger := models.FindMatchingMsgTrigger(oa, tc.channel, tc.contact, tc.text) assertTrigger(t, tc.expectedTriggerID, trigger, "trigger mismatch for %s sending '%s'", tc.contact.Name(), tc.text) } @@ -171,10 +208,11 @@ func TestFindMatchingIncomingCallTrigger(t *testing.T) { defer testsuite.Reset(testsuite.ResetAll) - doctorsAndNotTestersTriggerID := testdata.InsertIncomingCallTrigger(rt, testdata.Org1, testdata.Favorites, []*testdata.Group{testdata.DoctorsGroup}, []*testdata.Group{testdata.TestersGroup}) - doctorsTriggerID := testdata.InsertIncomingCallTrigger(rt, testdata.Org1, testdata.Favorites, []*testdata.Group{testdata.DoctorsGroup}, nil) - notTestersTriggerID := testdata.InsertIncomingCallTrigger(rt, testdata.Org1, testdata.Favorites, nil, []*testdata.Group{testdata.TestersGroup}) - everyoneTriggerID := testdata.InsertIncomingCallTrigger(rt, testdata.Org1, testdata.Favorites, nil, nil) + doctorsAndNotTestersTriggerID := testdata.InsertIncomingCallTrigger(rt, testdata.Org1, testdata.Favorites, []*testdata.Group{testdata.DoctorsGroup}, []*testdata.Group{testdata.TestersGroup}, nil) + doctorsTriggerID := testdata.InsertIncomingCallTrigger(rt, testdata.Org1, testdata.Favorites, []*testdata.Group{testdata.DoctorsGroup}, nil, nil) + notTestersTriggerID := testdata.InsertIncomingCallTrigger(rt, testdata.Org1, testdata.Favorites, nil, []*testdata.Group{testdata.TestersGroup}, nil) + everyoneTriggerID := testdata.InsertIncomingCallTrigger(rt, testdata.Org1, testdata.Favorites, nil, nil, nil) + specificChannelTriggerID := testdata.InsertIncomingCallTrigger(rt, testdata.Org1, testdata.Favorites, nil, nil, testdata.TwilioChannel) oa, err := models.GetOrgAssetsWithRefresh(ctx, rt, testdata.Org1.ID, models.RefreshTriggers) require.NoError(t, err) @@ -187,18 +225,24 @@ func TestFindMatchingIncomingCallTrigger(t *testing.T) { _, george, _ := testdata.George.Load(rt, oa) _, alexa, _ := testdata.Alexandria.Load(rt, oa) + twilioChannels, _ := models.GetChannelsByID(ctx, rt.DB.DB, []models.ChannelID{testdata.TwilioChannel.ID}) + facebookChannels, _ := models.GetChannelsByID(ctx, rt.DB.DB, []models.ChannelID{testdata.FacebookChannel.ID}) + tcs := []struct { contact *flows.Contact + channel *models.Channel expectedTriggerID models.TriggerID }{ - {cathy, doctorsAndNotTestersTriggerID}, // they're in doctors and not in testers - {bob, doctorsTriggerID}, // they're in doctors and testers - {george, notTestersTriggerID}, // they're not in doctors and not in testers - {alexa, everyoneTriggerID}, // they're not in doctors but are in testers + {cathy, twilioChannels[0], specificChannelTriggerID}, // specific channel + {cathy, facebookChannels[0], doctorsAndNotTestersTriggerID}, // not matching channel, get the next best scored channel + {cathy, nil, doctorsAndNotTestersTriggerID}, // they're in doctors and not in testers + {bob, nil, doctorsTriggerID}, // they're in doctors and testers + {george, nil, notTestersTriggerID}, // they're not in doctors and not in testers + {alexa, nil, everyoneTriggerID}, // they're not in doctors but are in testers } for _, tc := range tcs { - trigger := models.FindMatchingIncomingCallTrigger(oa, tc.contact) + trigger := models.FindMatchingIncomingCallTrigger(oa, tc.channel, tc.contact) assertTrigger(t, tc.expectedTriggerID, trigger, "trigger mismatch for %s", tc.contact.Name()) } @@ -209,22 +253,33 @@ func TestFindMatchingMissedCallTrigger(t *testing.T) { defer testsuite.Reset(testsuite.ResetData) - testdata.InsertCatchallTrigger(rt, testdata.Org1, testdata.SingleMessage, nil, nil) + testdata.InsertCatchallTrigger(rt, testdata.Org1, testdata.SingleMessage, nil, nil, nil) oa, err := models.GetOrgAssetsWithRefresh(ctx, rt, testdata.Org1.ID, models.RefreshTriggers) require.NoError(t, err) // no missed call trigger yet - trigger := models.FindMatchingMissedCallTrigger(oa) + trigger := models.FindMatchingMissedCallTrigger(oa, nil) assert.Nil(t, trigger) - triggerID := testdata.InsertMissedCallTrigger(rt, testdata.Org1, testdata.Favorites) + triggerID := testdata.InsertMissedCallTrigger(rt, testdata.Org1, testdata.Favorites, nil) oa, err = models.GetOrgAssetsWithRefresh(ctx, rt, testdata.Org1.ID, models.RefreshTriggers) require.NoError(t, err) - trigger = models.FindMatchingMissedCallTrigger(oa) + trigger = models.FindMatchingMissedCallTrigger(oa, nil) assertTrigger(t, triggerID, trigger) + + triggerIDwithChannel := testdata.InsertMissedCallTrigger(rt, testdata.Org1, testdata.Favorites, testdata.TwilioChannel) + + oa, err = models.GetOrgAssetsWithRefresh(ctx, rt, testdata.Org1.ID, models.RefreshTriggers) + require.NoError(t, err) + + channels, _ := models.GetChannelsByID(ctx, rt.DB.DB, []models.ChannelID{testdata.TwilioChannel.ID}) + + trigger = models.FindMatchingMissedCallTrigger(oa, channels[0]) + assertTrigger(t, triggerIDwithChannel, trigger) + } func TestFindMatchingNewConversationTrigger(t *testing.T) { @@ -348,7 +403,7 @@ func TestArchiveContactTriggers(t *testing.T) { defer testsuite.Reset(testsuite.ResetAll) - everybodyID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.Favorites, "join", models.MatchFirst, nil, nil) + everybodyID := testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.Favorites, "join", models.MatchFirst, nil, nil, nil) cathyOnly1ID := testdata.InsertScheduledTrigger(rt, testdata.Org1, testdata.Favorites, models.NilScheduleID, nil, nil, []*testdata.Contact{testdata.Cathy}) cathyOnly2ID := testdata.InsertScheduledTrigger(rt, testdata.Org1, testdata.Favorites, models.NilScheduleID, nil, nil, []*testdata.Contact{testdata.Cathy}) cathyAndGeorgeID := testdata.InsertScheduledTrigger(rt, testdata.Org1, testdata.Favorites, models.NilScheduleID, nil, nil, []*testdata.Contact{testdata.Cathy, testdata.George}) diff --git a/core/tasks/handler/contact_tasks.go b/core/tasks/handler/contact_tasks.go index 2f049b8a0..c9b88516a 100644 --- a/core/tasks/handler/contact_tasks.go +++ b/core/tasks/handler/contact_tasks.go @@ -187,9 +187,9 @@ func HandleChannelEvent(ctx context.Context, rt *runtime.Runtime, eventType mode case models.EventTypeReferral: trigger = models.FindMatchingReferralTrigger(oa, channel, event.ExtraString("referrer_id")) case models.EventTypeMissedCall: - trigger = models.FindMatchingMissedCallTrigger(oa) + trigger = models.FindMatchingMissedCallTrigger(oa, channel) case models.EventTypeIncomingCall: - trigger = models.FindMatchingIncomingCallTrigger(oa, contact) + trigger = models.FindMatchingIncomingCallTrigger(oa, channel, contact) case models.EventTypeOptIn: trigger = models.FindMatchingOptInTrigger(oa, channel) case models.EventTypeOptOut: @@ -410,7 +410,7 @@ func handleMsgEvent(ctx context.Context, rt *runtime.Runtime, event *MsgEvent) e } // find any matching triggers - trigger := models.FindMatchingMsgTrigger(oa, contact, event.Text) + trigger := models.FindMatchingMsgTrigger(oa, channel, contact, event.Text) // look for a waiting session for this contact session, err := models.FindWaitingSessionForContact(ctx, rt.DB, rt.SessionStorage, oa, models.FlowTypeMessaging, contact) diff --git a/core/tasks/handler/handle_contact_event_test.go b/core/tasks/handler/handle_contact_event_test.go index 6958e3868..2c26b5b0b 100644 --- a/core/tasks/handler/handle_contact_event_test.go +++ b/core/tasks/handler/handle_contact_event_test.go @@ -30,11 +30,11 @@ func TestMsgEvents(t *testing.T) { defer testsuite.Reset(testsuite.ResetAll) - testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.Favorites, "start", models.MatchOnly, nil, nil) - testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.IVRFlow, "ivr", models.MatchOnly, nil, nil) + testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.Favorites, "start", models.MatchOnly, nil, nil, nil) + testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.IVRFlow, "ivr", models.MatchOnly, nil, nil, nil) - testdata.InsertKeywordTrigger(rt, testdata.Org2, testdata.Org2Favorites, "start", models.MatchOnly, nil, nil) - testdata.InsertCatchallTrigger(rt, testdata.Org2, testdata.Org2SingleMessage, nil, nil) + testdata.InsertKeywordTrigger(rt, testdata.Org2, testdata.Org2Favorites, "start", models.MatchOnly, nil, nil, nil) + testdata.InsertCatchallTrigger(rt, testdata.Org2, testdata.Org2SingleMessage, nil, nil, nil) // give Cathy and Bob some tickets... openTickets := map[*testdata.Contact][]*testdata.Ticket{ @@ -642,8 +642,8 @@ func TestTimedEvents(t *testing.T) { defer testsuite.Reset(testsuite.ResetAll) // create some keyword triggers - testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.Favorites, "start", models.MatchOnly, nil, nil) - testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.PickANumber, "pick", models.MatchOnly, nil, nil) + testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.Favorites, "start", models.MatchOnly, nil, nil, nil) + testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.PickANumber, "pick", models.MatchOnly, nil, nil, nil) tcs := []struct { EventType string diff --git a/testsuite/testdata/triggers.go b/testsuite/testdata/triggers.go index 004aa1524..babf66c09 100644 --- a/testsuite/testdata/triggers.go +++ b/testsuite/testdata/triggers.go @@ -5,16 +5,16 @@ import ( "github.com/nyaruka/mailroom/runtime" ) -func InsertKeywordTrigger(rt *runtime.Runtime, org *Org, flow *Flow, keyword string, matchType models.MatchType, includeGroups []*Group, excludeGroups []*Group) models.TriggerID { - return insertTrigger(rt, org, models.KeywordTriggerType, flow, keyword, matchType, models.NilScheduleID, includeGroups, excludeGroups, nil, "", nil) +func InsertKeywordTrigger(rt *runtime.Runtime, org *Org, flow *Flow, keyword string, matchType models.MatchType, includeGroups []*Group, excludeGroups []*Group, channel *Channel) models.TriggerID { + return insertTrigger(rt, org, models.KeywordTriggerType, flow, keyword, matchType, models.NilScheduleID, includeGroups, excludeGroups, nil, "", channel) } -func InsertIncomingCallTrigger(rt *runtime.Runtime, org *Org, flow *Flow, includeGroups, excludeGroups []*Group) models.TriggerID { - return insertTrigger(rt, org, models.IncomingCallTriggerType, flow, "", "", models.NilScheduleID, includeGroups, excludeGroups, nil, "", nil) +func InsertIncomingCallTrigger(rt *runtime.Runtime, org *Org, flow *Flow, includeGroups, excludeGroups []*Group, channel *Channel) models.TriggerID { + return insertTrigger(rt, org, models.IncomingCallTriggerType, flow, "", "", models.NilScheduleID, includeGroups, excludeGroups, nil, "", channel) } -func InsertMissedCallTrigger(rt *runtime.Runtime, org *Org, flow *Flow) models.TriggerID { - return insertTrigger(rt, org, models.MissedCallTriggerType, flow, "", "", models.NilScheduleID, nil, nil, nil, "", nil) +func InsertMissedCallTrigger(rt *runtime.Runtime, org *Org, flow *Flow, channel *Channel) models.TriggerID { + return insertTrigger(rt, org, models.MissedCallTriggerType, flow, "", "", models.NilScheduleID, nil, nil, nil, "", channel) } func InsertNewConversationTrigger(rt *runtime.Runtime, org *Org, flow *Flow, channel *Channel) models.TriggerID { @@ -33,8 +33,8 @@ func InsertReferralTrigger(rt *runtime.Runtime, org *Org, flow *Flow, referrerID return insertTrigger(rt, org, models.ReferralTriggerType, flow, "", "", models.NilScheduleID, nil, nil, nil, referrerID, channel) } -func InsertCatchallTrigger(rt *runtime.Runtime, org *Org, flow *Flow, includeGroups, excludeGroups []*Group) models.TriggerID { - return insertTrigger(rt, org, models.CatchallTriggerType, flow, "", "", models.NilScheduleID, includeGroups, excludeGroups, nil, "", nil) +func InsertCatchallTrigger(rt *runtime.Runtime, org *Org, flow *Flow, includeGroups, excludeGroups []*Group, channel *Channel) models.TriggerID { + return insertTrigger(rt, org, models.CatchallTriggerType, flow, "", "", models.NilScheduleID, includeGroups, excludeGroups, nil, "", channel) } func InsertScheduledTrigger(rt *runtime.Runtime, org *Org, flow *Flow, schedID models.ScheduleID, includeGroups, excludeGroups []*Group, includeContacts []*Contact) models.TriggerID { diff --git a/web/simulation/simulation.go b/web/simulation/simulation.go index e7e7ade13..e328f75ae 100644 --- a/web/simulation/simulation.go +++ b/web/simulation/simulation.go @@ -199,7 +199,7 @@ func handleResume(ctx context.Context, rt *runtime.Runtime, r *resumeRequest) (a // if this is a msg resume we want to check whether it might be caught by a trigger if resume.Type() == resumes.TypeMsg { msgResume := resume.(*resumes.MsgResume) - trigger := models.FindMatchingMsgTrigger(oa, msgResume.Contact(), msgResume.Msg().Text()) + trigger := models.FindMatchingMsgTrigger(oa, nil, msgResume.Contact(), msgResume.Msg().Text()) if trigger != nil { var flow *models.Flow for _, r := range session.Runs() { diff --git a/web/simulation/simulation_test.go b/web/simulation/simulation_test.go index a35043552..a9ebf83b6 100644 --- a/web/simulation/simulation_test.go +++ b/web/simulation/simulation_test.go @@ -214,13 +214,13 @@ func TestServer(t *testing.T) { var session json.RawMessage // add a trigger for our campaign flow with 'trigger' - testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.CampaignFlow, "trigger", models.MatchOnly, nil, nil) + testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.CampaignFlow, "trigger", models.MatchOnly, nil, nil, nil) // and a trigger which will trigger an IVR flow - testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.IVRFlow, "ivr", models.MatchOnly, nil, nil) + testdata.InsertKeywordTrigger(rt, testdata.Org1, testdata.IVRFlow, "ivr", models.MatchOnly, nil, nil, nil) // also add a catch all - testdata.InsertCatchallTrigger(rt, testdata.Org1, testdata.CampaignFlow, nil, nil) + testdata.InsertCatchallTrigger(rt, testdata.Org1, testdata.CampaignFlow, nil, nil, nil) tcs := []struct { URL string