Skip to content

Commit

Permalink
Add support for excluding contacts already in a flow in start_session…
Browse files Browse the repository at this point in the history
… actions
  • Loading branch information
rowanseymour committed Jun 9, 2022
1 parent 97df539 commit 7aff134
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 41 deletions.
3 changes: 2 additions & 1 deletion core/hooks/insert_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ func (h *insertStartHook) Apply(ctx context.Context, rt *runtime.Runtime, tx *sq
}

// create our start
start := models.NewFlowStart(oa.OrgID(), models.StartTypeFlowAction, flow.FlowType(), flow.ID(), true, true).
start := models.NewFlowStart(oa.OrgID(), models.StartTypeFlowAction, flow.FlowType(), flow.ID(), true).
WithGroupIDs(groupIDs).
WithContactIDs(contactIDs).
WithURNs(event.URNs).
WithQuery(event.ContactQuery).
WithExcludeInAFlow(event.Exclusions.InAFlow).
WithCreateContact(event.CreateContact).
WithParentSummary(event.RunSummary).
WithSessionHistory(historyJSON)
Expand Down
9 changes: 6 additions & 3 deletions core/models/starts.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ func (s *FlowStart) WithQuery(query string) *FlowStart {

func (s *FlowStart) RestartParticipants() bool { return s.s.RestartParticipants }
func (s *FlowStart) IncludeActive() bool { return s.s.IncludeActive }
func (s *FlowStart) WithExcludeInAFlow(exclude bool) *FlowStart {
s.s.IncludeActive = !exclude
return s
}

func (s *FlowStart) CreateContact() bool { return s.s.CreateContact }
func (s *FlowStart) WithCreateContact(create bool) *FlowStart {
Expand Down Expand Up @@ -234,16 +238,15 @@ func GetFlowStartAttributes(ctx context.Context, db Queryer, startID StartID) (*
}

// NewFlowStart creates a new flow start objects for the passed in parameters
func NewFlowStart(orgID OrgID, startType StartType, flowType FlowType, flowID FlowID, restartParticipants, includeActive bool) *FlowStart {
func NewFlowStart(orgID OrgID, startType StartType, flowType FlowType, flowID FlowID, restartParticipants bool) *FlowStart {
s := &FlowStart{}
s.s.UUID = uuids.New()
s.s.OrgID = orgID
s.s.StartType = startType
s.s.FlowType = flowType
s.s.FlowID = flowID
s.s.RestartParticipants = restartParticipants
s.s.IncludeActive = includeActive

s.s.IncludeActive = true
return s
}

Expand Down
2 changes: 1 addition & 1 deletion core/models/starts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestStartsBuilding(t *testing.T) {
uuids.SetGenerator(uuids.NewSeededGenerator(12345))
defer uuids.SetGenerator(uuids.DefaultGenerator)

start := models.NewFlowStart(testdata.Org1.ID, models.StartTypeManual, models.FlowTypeMessaging, testdata.Favorites.ID, true, true).
start := models.NewFlowStart(testdata.Org1.ID, models.StartTypeManual, models.FlowTypeMessaging, testdata.Favorites.ID, true).
WithGroupIDs([]models.GroupID{testdata.DoctorsGroup.ID}).
WithExcludeGroupIDs([]models.GroupID{testdata.TestersGroup.ID}).
WithContactIDs([]models.ContactID{testdata.Cathy.ID, testdata.Bob.ID}).
Expand Down
2 changes: 1 addition & 1 deletion core/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ func TriggerIVRFlow(ctx context.Context, rt *runtime.Runtime, orgID models.OrgID
tx, _ := rt.DB.BeginTxx(ctx, nil)

// create our start
start := models.NewFlowStart(orgID, models.StartTypeTrigger, models.FlowTypeVoice, flowID, true, true).
start := models.NewFlowStart(orgID, models.StartTypeTrigger, models.FlowTypeVoice, flowID, true).
WithContactIDs(contactIDs)

// insert it
Expand Down
37 changes: 19 additions & 18 deletions core/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,34 +192,35 @@ func TestBatchStart(t *testing.T) {
contactIDs := []models.ContactID{testdata.Cathy.ID, testdata.Bob.ID}

tcs := []struct {
Flow models.FlowID
Restart bool
IncludeActive bool
Extra json.RawMessage
Msg string
Count int
TotalCount int
Flow models.FlowID
Restart bool
ExcludeInAFlow bool
Extra json.RawMessage
Msg string
Count int
TotalCount int
}{
{testdata.SingleMessage.ID, true, true, nil, "Hey, how are you?", 2, 2},
{testdata.SingleMessage.ID, false, true, nil, "Hey, how are you?", 0, 2},
{testdata.SingleMessage.ID, true, false, nil, "Hey, how are you?", 2, 2},
{testdata.SingleMessage.ID, false, false, nil, "Hey, how are you?", 0, 2},
{testdata.SingleMessage.ID, true, false, nil, "Hey, how are you?", 2, 4},
{testdata.SingleMessage.ID, false, true, nil, "Hey, how are you?", 0, 2},
{testdata.SingleMessage.ID, true, true, nil, "Hey, how are you?", 2, 4},
{
Flow: testdata.IncomingExtraFlow.ID,
Restart: true,
IncludeActive: false,
Extra: json.RawMessage([]byte(`{"name":"Fred", "age":33}`)),
Msg: "Great to meet you Fred. Your age is 33.",
Count: 2,
TotalCount: 2,
Flow: testdata.IncomingExtraFlow.ID,
Restart: true,
ExcludeInAFlow: true,
Extra: json.RawMessage([]byte(`{"name":"Fred", "age":33}`)),
Msg: "Great to meet you Fred. Your age is 33.",
Count: 2,
TotalCount: 2,
},
}

last := time.Now()

for i, tc := range tcs {
start := models.NewFlowStart(models.OrgID(1), models.StartTypeManual, models.FlowTypeMessaging, tc.Flow, tc.Restart, tc.IncludeActive).
start := models.NewFlowStart(models.OrgID(1), models.StartTypeManual, models.FlowTypeMessaging, tc.Flow, tc.Restart).
WithContactIDs(contactIDs).
WithExcludeInAFlow(tc.ExcludeInAFlow).
WithExtra(tc.Extra)
batch := start.CreateBatch(contactIDs, true, len(contactIDs))

Expand Down
2 changes: 1 addition & 1 deletion core/tasks/ivr/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestRetries(t *testing.T) {
db.MustExec(`UPDATE channels_channel SET channel_type = 'ZZ', config = '{"max_concurrent_events": 1}' WHERE id = $1`, testdata.TwilioChannel.ID)

// create a flow start for cathy
start := models.NewFlowStart(testdata.Org1.ID, models.StartTypeTrigger, models.FlowTypeVoice, testdata.IVRFlow.ID, true, true).
start := models.NewFlowStart(testdata.Org1.ID, models.StartTypeTrigger, models.FlowTypeVoice, testdata.IVRFlow.ID, true).
WithContactIDs([]models.ContactID{testdata.Cathy.ID})

// call our master starter
Expand Down
2 changes: 1 addition & 1 deletion core/tasks/ivr/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestIVR(t *testing.T) {
db.MustExec(`UPDATE channels_channel SET channel_type = 'ZZ', config = '{"max_concurrent_events": 1}' WHERE id = $1`, testdata.TwilioChannel.ID)

// create a flow start for cathy
start := models.NewFlowStart(testdata.Org1.ID, models.StartTypeTrigger, models.FlowTypeVoice, testdata.IVRFlow.ID, true, true).
start := models.NewFlowStart(testdata.Org1.ID, models.StartTypeTrigger, models.FlowTypeVoice, testdata.IVRFlow.ID, true).
WithContactIDs([]models.ContactID{testdata.Cathy.ID})

// call our master starter
Expand Down
24 changes: 14 additions & 10 deletions core/tasks/starts/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestStarts(t *testing.T) {
query string
queryResult []models.ContactID
restartParticipants bool
includeActive bool
excludeInAFlow bool
queue string
expectedContactCount int
expectedBatchCount int
Expand All @@ -59,6 +59,7 @@ func TestStarts(t *testing.T) {
{
label: "Empty flow start",
flowID: testdata.Favorites.ID,
excludeInAFlow: true,
queue: queue.BatchQueue,
expectedContactCount: 0,
expectedBatchCount: 0,
Expand All @@ -70,6 +71,7 @@ func TestStarts(t *testing.T) {
label: "Single group",
flowID: testdata.Favorites.ID,
groupIDs: []models.GroupID{testdata.DoctorsGroup.ID},
excludeInAFlow: true,
queue: queue.BatchQueue,
expectedContactCount: 121,
expectedBatchCount: 2,
Expand All @@ -82,6 +84,7 @@ func TestStarts(t *testing.T) {
flowID: testdata.Favorites.ID,
groupIDs: []models.GroupID{testdata.DoctorsGroup.ID},
contactIDs: []models.ContactID{testdata.Cathy.ID},
excludeInAFlow: true,
queue: queue.BatchQueue,
expectedContactCount: 121,
expectedBatchCount: 2,
Expand All @@ -94,7 +97,7 @@ func TestStarts(t *testing.T) {
flowID: testdata.Favorites.ID,
contactIDs: []models.ContactID{testdata.Cathy.ID},
restartParticipants: true,
includeActive: true,
excludeInAFlow: false,
queue: queue.HandlerQueue,
expectedContactCount: 1,
expectedBatchCount: 1,
Expand Down Expand Up @@ -129,7 +132,7 @@ func TestStarts(t *testing.T) {
label: "Single contact, include active, but no restart",
flowID: testdata.Favorites.ID,
contactIDs: []models.ContactID{testdata.Bob.ID},
includeActive: true,
excludeInAFlow: false,
queue: queue.HandlerQueue,
expectedContactCount: 1,
expectedBatchCount: 1,
Expand All @@ -142,7 +145,7 @@ func TestStarts(t *testing.T) {
flowID: testdata.Favorites.ID,
contactIDs: []models.ContactID{testdata.Bob.ID},
restartParticipants: true,
includeActive: true,
excludeInAFlow: false,
queue: queue.HandlerQueue,
expectedContactCount: 1,
expectedBatchCount: 1,
Expand All @@ -156,7 +159,7 @@ func TestStarts(t *testing.T) {
query: "bob",
queryResult: []models.ContactID{testdata.Bob.ID},
restartParticipants: true,
includeActive: true,
excludeInAFlow: false,
queue: queue.HandlerQueue,
expectedContactCount: 1,
expectedBatchCount: 1,
Expand All @@ -169,7 +172,7 @@ func TestStarts(t *testing.T) {
flowID: testdata.Favorites.ID,
query: "xyz = 45",
restartParticipants: true,
includeActive: true,
excludeInAFlow: false,
queue: queue.HandlerQueue,
expectedContactCount: 0,
expectedBatchCount: 0,
Expand All @@ -192,7 +195,7 @@ func TestStarts(t *testing.T) {
label: "Other messaging flow",
flowID: testdata.PickANumber.ID,
contactIDs: []models.ContactID{testdata.Bob.ID},
includeActive: true,
excludeInAFlow: false,
queue: queue.HandlerQueue,
expectedContactCount: 1,
expectedBatchCount: 1,
Expand All @@ -204,7 +207,7 @@ func TestStarts(t *testing.T) {
label: "Background flow",
flowID: testdata.SingleMessage.ID,
contactIDs: []models.ContactID{testdata.Bob.ID},
includeActive: true,
excludeInAFlow: false,
queue: queue.HandlerQueue,
expectedContactCount: 1,
expectedBatchCount: 1,
Expand All @@ -218,7 +221,7 @@ func TestStarts(t *testing.T) {
contactIDs: []models.ContactID{testdata.Cathy.ID, testdata.Bob.ID},
excludeGroupIDs: []models.GroupID{testdata.DoctorsGroup.ID}, // should exclude Cathy
restartParticipants: true,
includeActive: true,
excludeInAFlow: false,
queue: queue.HandlerQueue,
expectedContactCount: 1,
expectedBatchCount: 1,
Expand All @@ -234,11 +237,12 @@ func TestStarts(t *testing.T) {
}

// handle our start task
start := models.NewFlowStart(testdata.Org1.ID, models.StartTypeManual, models.FlowTypeMessaging, tc.flowID, tc.restartParticipants, tc.includeActive).
start := models.NewFlowStart(testdata.Org1.ID, models.StartTypeManual, models.FlowTypeMessaging, tc.flowID, tc.restartParticipants).
WithGroupIDs(tc.groupIDs).
WithExcludeGroupIDs(tc.excludeGroupIDs).
WithContactIDs(tc.contactIDs).
WithQuery(tc.query).
WithExcludeInAFlow(tc.excludeInAFlow).
WithCreateContact(tc.createContact)

err := models.InsertFlowStarts(ctx, db, []*models.FlowStart{start})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/lib/pq v1.10.4
github.com/nyaruka/ezconf v0.2.1
github.com/nyaruka/gocommon v1.21.0
github.com/nyaruka/goflow v0.159.2
github.com/nyaruka/goflow v0.160.0
github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d
github.com/nyaruka/null v1.2.0
github.com/nyaruka/redisx v0.2.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ github.com/nyaruka/ezconf v0.2.1 h1:TDXWoqjqYya1uhou1mAJZg7rgFYL98EB0Tb3+BWtUh0=
github.com/nyaruka/ezconf v0.2.1/go.mod h1:ey182kYkw2MIi4XiWe1FR/mzI33WCmTWuceDYYxgnQw=
github.com/nyaruka/gocommon v1.21.0 h1:nu7M2cdSPrkqUPdGsEeWX047+neo69H4x+4g/OKpoLM=
github.com/nyaruka/gocommon v1.21.0/go.mod h1:cv9r6amof1gSktfPZROClZhLFzdSIH/N9KbW6Nny4g8=
github.com/nyaruka/goflow v0.159.2 h1:tyAILWt7DGQ5nfd2/ZN4BNlVxPbm/Omoz/CHS6hs+UM=
github.com/nyaruka/goflow v0.159.2/go.mod h1:iLYZOLLggFRR8e5dVgFEmB95W4FPrIKbz3w3C6Ia/0g=
github.com/nyaruka/goflow v0.160.0 h1:eBaokSbxnwr39SLd1Yqd/zALusdyviVxCoceyRZQGnY=
github.com/nyaruka/goflow v0.160.0/go.mod h1:iLYZOLLggFRR8e5dVgFEmB95W4FPrIKbz3w3C6Ia/0g=
github.com/nyaruka/librato v1.0.0 h1:Vznj9WCeC1yZXbBYyYp40KnbmXLbEkjKmHesV/v2SR0=
github.com/nyaruka/librato v1.0.0/go.mod h1:pkRNLFhFurOz0QqBz6/DuTFhHHxAubWxs4Jx+J7yUgg=
github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d h1:hyp9u36KIwbTCo2JAJ+TuJcJBc+UZzEig7RI/S5Dvkc=
Expand Down
4 changes: 2 additions & 2 deletions web/ivr/ivr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestTwilioIVR(t *testing.T) {
},
"results": {}
}`)
start := models.NewFlowStart(testdata.Org1.ID, models.StartTypeTrigger, models.FlowTypeVoice, testdata.IVRFlow.ID, true, true).
start := models.NewFlowStart(testdata.Org1.ID, models.StartTypeTrigger, models.FlowTypeVoice, testdata.IVRFlow.ID, true).
WithContactIDs([]models.ContactID{testdata.Cathy.ID, testdata.Bob.ID, testdata.George.ID}).
WithParentSummary(parentSummary)

Expand Down Expand Up @@ -402,7 +402,7 @@ func TestVonageIVR(t *testing.T) {

// create a flow start for cathy and george
extra := json.RawMessage(`{"ref_id":"123"}`)
start := models.NewFlowStart(testdata.Org1.ID, models.StartTypeTrigger, models.FlowTypeVoice, testdata.IVRFlow.ID, true, true).
start := models.NewFlowStart(testdata.Org1.ID, models.StartTypeTrigger, models.FlowTypeVoice, testdata.IVRFlow.ID, true).
WithContactIDs([]models.ContactID{testdata.Cathy.ID, testdata.George.ID}).
WithExtra(extra)
models.InsertFlowStarts(ctx, db, []*models.FlowStart{start})
Expand Down

0 comments on commit 7aff134

Please sign in to comment.