Skip to content

Commit

Permalink
Merge pull request rapidpro#207 from nyaruka/globals_in_bcasts
Browse files Browse the repository at this point in the history
📣 Update CreateBroadcastMessages to include globals in context
  • Loading branch information
rowanseymour authored Dec 6, 2019
2 parents c50bcf5 + 347eea4 commit 932df9f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 9 deletions.
Binary file modified mailroom_test.dump
Binary file not shown.
9 changes: 2 additions & 7 deletions models/globals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@ func TestGlobals(t *testing.T) {
assert.NoError(t, err)
defer tx.Rollback()

tx.MustExec(`INSERT INTO globals_global(uuid, is_active, created_on, modified_on, key, name, value, created_by_id, modified_by_id, org_id)
VALUES('f09d4cbe-08fa-4854-9691-d43035e9fdeb', TRUE, NOW(), NOW(), 'org_name', 'Org Name', 'Acme Ltd', 1, 1, 1);`)
tx.MustExec(`INSERT INTO globals_global(uuid, is_active, created_on, modified_on, key, name, value, created_by_id, modified_by_id, org_id)
VALUES('93ab549d-92b7-41cb-a57c-d4c8fa0fd18f', TRUE, NOW(), NOW(), 'access_token', 'Access Token', 'ab2452', 1, 1, 1);`)

globals, err := loadGlobals(ctx, tx, 1)
assert.NoError(t, err)

assert.Equal(t, 2, len(globals))
assert.Equal(t, "access_token", globals[0].Key())
assert.Equal(t, "Access Token", globals[0].Name())
assert.Equal(t, "ab2452", globals[0].Value())
assert.Equal(t, "A213CD78", globals[0].Value())
assert.Equal(t, "org_name", globals[1].Key())
assert.Equal(t, "Org Name", globals[1].Name())
assert.Equal(t, "Acme Ltd", globals[1].Value())
assert.Equal(t, "Nyaruka", globals[1].Value())
}
1 change: 1 addition & 0 deletions models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ func CreateBroadcastMessages(ctx context.Context, db Queryer, rp *redis.Pool, or
templateCtx := types.NewXObject(map[string]types.XValue{
"contact": flows.Context(org.Env(), contact),
"fields": flows.Context(org.Env(), contact.Fields()),
"globals": flows.Context(org.Env(), sa.Globals()),
"urns": flows.ContextFunc(org.Env(), contact.URNs().MapContext),
})
text, _ = excellent.EvaluateTemplate(org.Env(), templateCtx, template, nil)
Expand Down
4 changes: 2 additions & 2 deletions tasks/broadcasts/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func TestBroadcastTask(t *testing.T) {

template := map[envs.Language]*models.BroadcastTranslation{
eng: &models.BroadcastTranslation{
Text: "hi @(title(contact.name)) goflow URN: @urns.tel Gender: @fields.gender",
Text: "hi @(title(contact.name)) from @globals.org_name goflow URN: @urns.tel Gender: @fields.gender",
Attachments: nil,
QuickReplies: nil,
},
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestBroadcastTask(t *testing.T) {
}{
{models.NilBroadcastID, evaluated, models.TemplateStateEvaluated, eng, doctorsOnly, cathyOnly, nil, queue.BatchQueue, 2, 121, "hello world"},
{legacyID, legacy, models.TemplateStateLegacy, eng, nil, cathyOnly, nil, queue.HandlerQueue, 1, 1, "hi Cathy legacy URN: +12065551212 Gender: F"},
{models.NilBroadcastID, template, models.TemplateStateUnevaluated, eng, nil, cathyOnly, nil, queue.HandlerQueue, 1, 1, "hi Cathy goflow URN: tel:+12065551212 Gender: F"},
{models.NilBroadcastID, template, models.TemplateStateUnevaluated, eng, nil, cathyOnly, nil, queue.HandlerQueue, 1, 1, "hi Cathy from Nyaruka goflow URN: tel:+12065551212 Gender: F"},
}

lastNow := time.Now()
Expand Down

0 comments on commit 932df9f

Please sign in to comment.