Skip to content

Commit

Permalink
Merge pull request #1204 from nyaruka/variables-params-alt
Browse files Browse the repository at this point in the history
Replace flows.MsgTemplating variables by params
  • Loading branch information
rowanseymour authored Jan 17, 2024
2 parents fc6dde4 + 25d01c8 commit db985db
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 15 deletions.
34 changes: 26 additions & 8 deletions flows/actions/testdata/send_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,19 @@
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
},
"variables": [
"Ryan Lewis",
"boy"
],
"params": {
"body": [
{
"type": "text",
"value": "Ryan Lewis"
},
{
"type": "text",
"value": "boy"
}

]
},
"namespace": ""
},
"topic": "account",
Expand Down Expand Up @@ -529,10 +538,19 @@
"uuid": "5722e1fd-fe32-4e74-ac78-3cf41a6adb7e",
"name": "affirmation"
},
"variables": [
"Ryan Lewis",
"niño"
],
"params": {
"body": [
{
"type": "text",
"value": "Ryan Lewis"
},
{
"type": "text",
"value": "niño"
}

]
},
"namespace": ""
},
"locale": "spa"
Expand Down
27 changes: 20 additions & 7 deletions flows/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,41 @@ func (m *MsgOut) Locale() i18n.Locale { return m.Locale_ }
// UnsendableReason returns the reason this message can't be sent (if any)
func (m *MsgOut) UnsendableReason() UnsendableReason { return m.UnsendableReason_ }

type TemplateParam struct {
Type string `json:"type"`
Value string `json:"value"`
}

// MsgTemplating represents any substituted message template that should be applied when sending this message
type MsgTemplating struct {
Template_ *assets.TemplateReference `json:"template"`
Variables_ []string `json:"variables,omitempty"`
Namespace_ string `json:"namespace"`
Template_ *assets.TemplateReference `json:"template"`
Params_ map[string][]TemplateParam `json:"params,omitempty"`
Namespace_ string `json:"namespace"`
}

// Template returns the template this msg template is for
func (t MsgTemplating) Template() *assets.TemplateReference { return t.Template_ }

// Variables returns the variables that should be substituted in the template
func (t MsgTemplating) Variables() []string { return t.Variables_ }

// Namespace returns the namespace that should be for the template
func (t MsgTemplating) Namespace() string { return t.Namespace_ }

// Params returns the params that should be used for the template
func (t MsgTemplating) Params() map[string][]TemplateParam { return t.Params_ }

// NewMsgTemplating creates and returns a new msg template
func NewMsgTemplating(template *assets.TemplateReference, variables []string, namespace string) *MsgTemplating {
params := map[string][]TemplateParam{}
if len(variables) > 0 {
params = map[string][]TemplateParam{"body": make([]TemplateParam, len(variables))}
for i, v := range variables {
params["body"][i] = TemplateParam{Type: "text", Value: v}
}
}

return &MsgTemplating{
Template_: template,
Variables_: variables,
Namespace_: namespace,
Params_: params,
}
}

Expand Down
37 changes: 37 additions & 0 deletions flows/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,40 @@ func TestBroadcastTranslations(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, flows.BroadcastTranslations{"spa": {Text: "Adios"}}, bt)
}

func TestMsgTemplating(t *testing.T) {
uuids.SetGenerator(uuids.NewSeededGenerator(12345))
defer uuids.SetGenerator(uuids.DefaultGenerator)

templateRef := assets.NewTemplateReference("61602f3e-f603-4c70-8a8f-c477505bf4bf", "Affirmation")

msgTemplating := flows.NewMsgTemplating(templateRef, []string{"Ryan Lewis", "boy"}, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b")

assert.Equal(t, templateRef, msgTemplating.Template())
assert.Equal(t, "0162a7f4_dfe4_4c96_be07_854d5dba3b2b", msgTemplating.Namespace())
assert.Equal(t, map[string][]flows.TemplateParam{"body": {{Type: "text", Value: "Ryan Lewis"}, {Type: "text", Value: "boy"}}}, msgTemplating.Params())

// test marshaling our msg
marshaled, err := jsonx.Marshal(msgTemplating)
require.NoError(t, err)

test.AssertEqualJSON(t, []byte(`{
"namespace":"0162a7f4_dfe4_4c96_be07_854d5dba3b2b",
"params":{
"body":[
{
"type": "text",
"value": "Ryan Lewis"
},
{
"type": "text",
"value": "boy"
}
]
},
"template":{
"name":"Affirmation",
"uuid":"61602f3e-f603-4c70-8a8f-c477505bf4bf"
}
}`), marshaled, "JSON mismatch")
}

0 comments on commit db985db

Please sign in to comment.