Skip to content

Commit

Permalink
Merge pull request #69 from nyaruka/start_exclusions_4
Browse files Browse the repository at this point in the history
Read from new flow start fields
  • Loading branch information
rowanseymour authored May 10, 2023
2 parents 4c828c1 + a071d4a commit 19e4317
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
6 changes: 3 additions & 3 deletions core/ivr/ivr.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ func StartIVRFlow(
}

var params *types.XObject
if !start.Extra.IsNull() {
params, err = types.ReadXObject(start.Extra)
if !start.Params.IsNull() {
params, err = types.ReadXObject(start.Params)
if err != nil {
return errors.Wrap(err, "unable to read JSON from flow start extra")
return errors.Wrap(err, "unable to read JSON from flow start params")
}
}

Expand Down
15 changes: 4 additions & 11 deletions core/models/starts.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,12 @@ func (s *FlowStart) WithQuery(query string) *FlowStart {
return s
}

func (s *FlowStart) ExcludeStartedPreviously() bool { return !s.RestartParticipants }
func (s *FlowStart) WithExcludeStartedPreviously(exclude bool) *FlowStart {
s.Exclusions.StartedPreviously = exclude
s.RestartParticipants = !exclude
return s
}

func (s *FlowStart) ExcludeInAFlow() bool { return !s.IncludeActive }
func (s *FlowStart) WithExcludeInAFlow(exclude bool) *FlowStart {
s.Exclusions.InAFlow = exclude
s.IncludeActive = !exclude
Expand Down Expand Up @@ -190,10 +188,10 @@ func MarkStartFailed(ctx context.Context, db Queryer, startID StartID) error {
return errors.Wrapf(err, "error setting flow start as failed")
}

// GetFlowStartAttributes gets the basic attributes for the passed in start id, this includes ONLY its id, uuid, flow_id and extra
// GetFlowStartAttributes gets the basic attributes for the passed in start id, this includes ONLY its id, uuid, flow_id and params
func GetFlowStartAttributes(ctx context.Context, db Queryer, startID StartID) (*FlowStart, error) {
start := &FlowStart{}
err := db.GetContext(ctx, start, `SELECT id, uuid, flow_id, extra, parent_summary, session_history FROM flows_flowstart WHERE id = $1`, startID)
err := db.GetContext(ctx, start, `SELECT id, uuid, flow_id, params, parent_summary, session_history FROM flows_flowstart WHERE id = $1`, startID)
if err != nil {
return nil, errors.Wrapf(err, "unable to load start attributes for id: %d", startID)
}
Expand Down Expand Up @@ -272,15 +270,13 @@ func (s *FlowStart) CreateBatch(contactIDs []ContactID, last bool, totalContacts
FlowID: s.FlowID,
FlowType: s.FlowType,
ContactIDs: contactIDs,
Exclusions: Exclusions{StartedPreviously: !s.RestartParticipants, InAFlow: !s.IncludeActive},
Exclusions: s.Exclusions,
ParentSummary: s.ParentSummary,
SessionHistory: s.SessionHistory,
Params: s.Extra,
Params: s.Params,
IsLast: last,
TotalContacts: totalContacts,
CreatedByID: s.CreatedByID,
// deprecated
Extra: s.Extra,
}
}

Expand All @@ -301,9 +297,6 @@ type FlowStartBatch struct {

IsLast bool `json:"is_last,omitempty"`
TotalContacts int `json:"total_contacts"`

// deprecated
Extra null.JSON `json:"extra,omitempty"`
}

// ReadSessionHistory reads a session history from the given JSON
Expand Down
16 changes: 9 additions & 7 deletions core/models/starts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ func TestStarts(t *testing.T) {
"start_type": "M",
"org_id": %d,
"created_by_id": %d,
"exclusions": {},
"flow_id": %d,
"flow_type": "M",
"contact_ids": [%d, %d],
"group_ids": [%d],
"exclude_group_ids": [%d],
"urns": ["tel:+12025550199"],
"query": null,
"restart_participants": true,
"include_active": true,
"params": {"foo": "bar"},
"parent_summary": {"uuid": "b65b1a22-db6d-4f5a-9b3d-7302368a82e6"},
"session_history": {"parent_uuid": "532a3899-492f-4ffe-aed7-e75ad524efab", "ancestors": 3, "ancestors_since_input": 1},
"extra": {"foo": "bar"}
"extra": {"foo": "bar"},
"restart_participants": true,
"include_active": true
}`, startID, testdata.Org1.ID, testdata.Admin.ID, testdata.SingleMessage.ID, testdata.Cathy.ID, testdata.Bob.ID, testdata.DoctorsGroup.ID, testdata.TestersGroup.ID))

start := &models.FlowStart{}
Expand All @@ -54,15 +56,15 @@ func TestStarts(t *testing.T) {
assert.Equal(t, testdata.SingleMessage.ID, start.FlowID)
assert.Equal(t, models.FlowTypeMessaging, start.FlowType)
assert.Equal(t, null.NullString, start.Query)
assert.False(t, start.ExcludeStartedPreviously())
assert.False(t, start.ExcludeInAFlow())
assert.False(t, start.Exclusions.StartedPreviously)
assert.False(t, start.Exclusions.InAFlow)
assert.Equal(t, []models.ContactID{testdata.Cathy.ID, testdata.Bob.ID}, start.ContactIDs)
assert.Equal(t, []models.GroupID{testdata.DoctorsGroup.ID}, start.GroupIDs)
assert.Equal(t, []models.GroupID{testdata.TestersGroup.ID}, start.ExcludeGroupIDs)

assert.Equal(t, null.JSON(`{"uuid": "b65b1a22-db6d-4f5a-9b3d-7302368a82e6"}`), start.ParentSummary)
assert.Equal(t, null.JSON(`{"parent_uuid": "532a3899-492f-4ffe-aed7-e75ad524efab", "ancestors": 3, "ancestors_since_input": 1}`), start.SessionHistory)
assert.Equal(t, null.JSON(`{"foo": "bar"}`), start.Extra)
assert.Equal(t, null.JSON(`{"foo": "bar"}`), start.Params)

err = models.MarkStartStarted(ctx, rt.DB, startID, 2)
require.NoError(t, err)
Expand All @@ -83,7 +85,7 @@ func TestStarts(t *testing.T) {

assert.Equal(t, null.JSON(`{"uuid": "b65b1a22-db6d-4f5a-9b3d-7302368a82e6"}`), batch.ParentSummary)
assert.Equal(t, null.JSON(`{"parent_uuid": "532a3899-492f-4ffe-aed7-e75ad524efab", "ancestors": 3, "ancestors_since_input": 1}`), batch.SessionHistory)
assert.Equal(t, null.JSON(`{"foo": "bar"}`), batch.Extra)
assert.Equal(t, null.JSON(`{"foo": "bar"}`), batch.Params)

history, err := models.ReadSessionHistory(batch.SessionHistory)
assert.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions core/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ func StartFlowBatch(
}

var params *types.XObject
if !batch.Extra.IsNull() {
params, err = types.ReadXObject(batch.Extra)
if !batch.Params.IsNull() {
params, err = types.ReadXObject(batch.Params)
if err != nil {
return nil, errors.Wrap(err, "unable to read JSON from flow start extra")
return nil, errors.Wrap(err, "unable to read JSON from flow start params")
}
}

Expand All @@ -208,7 +208,7 @@ func StartFlowBatch(
}

tb := triggers.NewBuilder(oa.Env(), flow.Reference(), contact).Manual()
if batch.Extra != nil {
if !batch.Params.IsNull() {
tb = tb.WithParams(params)
}
if batchStart {
Expand Down
4 changes: 2 additions & 2 deletions testsuite/testdata/flows.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func ImportFlows(rt *runtime.Runtime, org *Org, path string) []*Flow {
func InsertFlowStart(rt *runtime.Runtime, org *Org, flow *Flow, contacts []*Contact) models.StartID {
var id models.StartID
must(rt.DB.Get(&id,
`INSERT INTO flows_flowstart(uuid, org_id, flow_id, start_type, created_on, modified_on, restart_participants, include_active, contact_count, status, created_by_id)
VALUES($1, $2, $3, 'M', NOW(), NOW(), TRUE, TRUE, 2, 'P', 1) RETURNING id`, uuids.New(), org.ID, flow.ID,
`INSERT INTO flows_flowstart(uuid, org_id, flow_id, start_type, exclusions, created_on, modified_on, contact_count, status, created_by_id, restart_participants, include_active)
VALUES($1, $2, $3, 'M', '{}', NOW(), NOW(), 2, 'P', 1, TRUE, TRUE) RETURNING id`, uuids.New(), org.ID, flow.ID,
))

for _, c := range contacts {
Expand Down
4 changes: 4 additions & 0 deletions web/ivr/ivr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ func TestVonageIVR(t *testing.T) {
err := models.InsertFlowStarts(ctx, rt.DB, []*models.FlowStart{start})
require.NoError(t, err)

assertdb.Query(t, rt.DB, `SELECT COUNT(*) FROM flows_flowstart`).Returns(1)
assertdb.Query(t, rt.DB, `SELECT COUNT(*) FROM flows_flowstart WHERE params ->> 'ref_id' = '123'`).Returns(1)
assertdb.Query(t, rt.DB, `SELECT COUNT(*) FROM flows_flowstart WHERE extra::jsonb ->> 'ref_id' = '123'`).Returns(1)

err = tasks.Queue(rc, queue.BatchQueue, testdata.Org1.ID, &starts.StartFlowTask{FlowStart: start}, queue.DefaultPriority)
require.NoError(t, err)

Expand Down

0 comments on commit 19e4317

Please sign in to comment.