-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #617 from nyaruka/flow_history_changes
Update modified_on whenever flow history changes
- Loading branch information
Showing
11 changed files
with
131 additions
and
64 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/nyaruka/goflow/flows" | ||
"github.com/nyaruka/goflow/flows/events" | ||
"github.com/nyaruka/mailroom/core/hooks" | ||
"github.com/nyaruka/mailroom/core/models" | ||
"github.com/nyaruka/mailroom/runtime" | ||
|
||
"github.com/jmoiron/sqlx" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func init() { | ||
models.RegisterEventHandler(events.TypeFlowEntered, handleFlowEntered) | ||
} | ||
|
||
func handleFlowEntered(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa *models.OrgAssets, scene *models.Scene, e flows.Event) error { | ||
event := e.(*events.FlowEnteredEvent) | ||
|
||
logrus.WithFields(logrus.Fields{ | ||
"contact_uuid": scene.ContactUUID(), | ||
"session_id": scene.SessionID(), | ||
"flow_name": event.Flow.Name, | ||
"flow_uuid": event.Flow.UUID, | ||
}).Debug("flow entered") | ||
|
||
// we've potentially changed contact flow history.. only way to be sure would be loading contacts with their | ||
// flow history, but not sure that is worth it given how likely we are to be updating modified_on anyway | ||
scene.AppendToEventPreCommitHook(hooks.ContactModifiedHook, event) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package handlers_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nyaruka/goflow/flows" | ||
"github.com/nyaruka/goflow/flows/actions" | ||
"github.com/nyaruka/mailroom/core/handlers" | ||
"github.com/nyaruka/mailroom/testsuite" | ||
"github.com/nyaruka/mailroom/testsuite/testdata" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFlowEntered(t *testing.T) { | ||
ctx, rt, _, _ := testsuite.Get() | ||
|
||
defer testsuite.Reset(testsuite.ResetAll) | ||
|
||
oa := testdata.Org1.Load(rt) | ||
|
||
flow, err := oa.FlowByID(testdata.PickANumber.ID) | ||
assert.NoError(t, err) | ||
|
||
tcs := []handlers.TestCase{ | ||
{ | ||
Actions: handlers.ContactActionMap{ | ||
testdata.Cathy: []flows.Action{ | ||
actions.NewEnterFlow(handlers.NewActionUUID(), flow.Reference(), false), | ||
}, | ||
}, | ||
SQLAssertions: []handlers.SQLAssertion{ | ||
{ | ||
SQL: `select count(*) from contacts_contact where current_flow_id = $1`, | ||
Args: []interface{}{flow.ID()}, | ||
Count: 1, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
handlers.RunTestCases(t, ctx, rt, tcs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/nyaruka/goflow/flows" | ||
"github.com/nyaruka/mailroom/core/hooks" | ||
"github.com/nyaruka/mailroom/core/models" | ||
"github.com/nyaruka/mailroom/runtime" | ||
|
||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
func init() { | ||
models.RegisterEventHandler(models.TypeSprintEnded, handleSprintEnded) | ||
} | ||
|
||
func handleSprintEnded(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa *models.OrgAssets, scene *models.Scene, e flows.Event) error { | ||
event := e.(*models.SprintEndedEvent) | ||
|
||
// if we're in a flow type that can wait then contact current flow has potentially changed | ||
currentFlowChanged := scene.Session().SessionType().Interrupts() && event.Contact.CurrentFlowID() != scene.Session().CurrentFlowID() | ||
|
||
if currentFlowChanged { | ||
scene.AppendToEventPreCommitHook(hooks.CommitFlowChangesHook, scene.Session().CurrentFlowID()) | ||
} | ||
|
||
// if current flow has changed then we need to update modified_on, but also if this is a new session | ||
// then flow history may have changed too in a way that won't be captured by a flow_entered event | ||
if currentFlowChanged || !event.Resumed { | ||
scene.AppendToEventPostCommitHook(hooks.ContactModifiedHook, event) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters