Skip to content

Commit

Permalink
Merge pull request #425 from nyaruka/fires_fix
Browse files Browse the repository at this point in the history
Fix contact fires being created before sessions have ids
  • Loading branch information
rowanseymour authored Jan 28, 2025
2 parents afa7131 + 0a96e41 commit ea63c71
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
11 changes: 5 additions & 6 deletions core/models/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,6 @@ func InsertSessions(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa *O
completedCallIDs = append(completedCallIDs, session.call.ID())
}
}

fires = append(fires, session.GetNewFires(oa, sprints[i])...)
}

// call our global pre commit hook if present
Expand Down Expand Up @@ -637,14 +635,15 @@ func InsertSessions(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa *O
return nil, fmt.Errorf("error inserting waiting sessions: %w", err)
}

// gather all runs across all sessions
// now that sessions have ids, set it on runs and generates fires
runs := make([]*FlowRun, 0, len(sessions))
for _, s := range sessions {
for i, s := range sessions {
for _, r := range s.runs {
r.SessionID = s.ID() // set our session id now that it is written

r.SessionID = s.ID()
runs = append(runs, r)
}

fires = append(fires, s.GetNewFires(oa, sprints[i])...)
}

// insert all runs
Expand Down
4 changes: 2 additions & 2 deletions core/models/sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func TestSessionCreationAndUpdating(t *testing.T) {
})

// check we have contact fires for wait expiration and timeout
assertdb.Query(t, rt.DB, `SELECT count(*) FROM contacts_contactfire WHERE contact_id = $1 AND fire_type = 'E' AND scope = ''`, testdata.Bob.ID).Returns(1)
assertdb.Query(t, rt.DB, `SELECT count(*) FROM contacts_contactfire WHERE contact_id = $1 AND fire_type = 'T' AND scope = ''`, testdata.Bob.ID).Returns(1)
assertdb.Query(t, rt.DB, `SELECT count(*) FROM contacts_contactfire WHERE contact_id = $1 AND fire_type = 'E' AND scope = '' AND extra->'session_id' = $2`, testdata.Bob.ID, modelSessions[0].ID()).Returns(1)
assertdb.Query(t, rt.DB, `SELECT count(*) FROM contacts_contactfire WHERE contact_id = $1 AND fire_type = 'T' AND scope = '' AND extra->'session_id' = $2`, testdata.Bob.ID, modelSessions[0].ID()).Returns(1)

// reload contact and check current flow is set
modelContact, _, _ = testdata.Bob.Load(rt, oa)
Expand Down

0 comments on commit ea63c71

Please sign in to comment.