Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace flows.MsgTemplating variables by params #1204

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
Loading