Skip to content

Commit

Permalink
Replace flows.MsgTemplating variables by params
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Jan 17, 2024
1 parent fc6dde4 commit 0642e9a
Show file tree
Hide file tree
Showing 2 changed files with 43 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
24 changes: 17 additions & 7 deletions flows/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,38 @@ 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_ }

// 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

0 comments on commit 0642e9a

Please sign in to comment.