Skip to content

Commit

Permalink
Add new 13.5 flow spec
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Apr 17, 2024
1 parent 1a6e27b commit 8e6abe0
Show file tree
Hide file tree
Showing 11 changed files with 405 additions and 11 deletions.
6 changes: 3 additions & 3 deletions flows/actions/testdata/_assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"uuid": "bead76f5-dac4-4c9d-996c-c62b326e8c0a",
"name": "Action Tester",
"spec_version": "13.4.0",
"spec_version": "13.5.0",
"language": "eng",
"type": "messaging",
"revision": 123,
Expand All @@ -23,7 +23,7 @@
{
"uuid": "7a84463d-d209-4d3e-a0ff-79f977cd7bd0",
"name": "Voice Action Tester",
"spec_version": "13.4.0",
"spec_version": "13.5.0",
"language": "eng",
"type": "voice",
"revision": 123,
Expand All @@ -43,7 +43,7 @@
{
"uuid": "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d",
"name": "Collect Age",
"spec_version": "13.4.0",
"spec_version": "13.5.0",
"revision": 123,
"language": "eng",
"type": "messaging",
Expand Down
2 changes: 1 addition & 1 deletion flows/definition/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

// CurrentSpecVersion is the flow spec version supported by this library
var CurrentSpecVersion = semver.MustParse("13.4.0")
var CurrentSpecVersion = semver.MustParse("13.5.0")

// IsVersionSupported checks the given version is supported
func IsVersionSupported(v *semver.Version) bool {
Expand Down
69 changes: 69 additions & 0 deletions flows/definition/migrations/13_x.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,86 @@ package migrations

import (
"github.com/Masterminds/semver"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/excellent/refactor"
)

func init() {
registerMigration(semver.MustParse("13.5.0"), Migrate13_5)
registerMigration(semver.MustParse("13.4.0"), Migrate13_4)
registerMigration(semver.MustParse("13.3.0"), Migrate13_3)
registerMigration(semver.MustParse("13.2.0"), Migrate13_2)
registerMigration(semver.MustParse("13.1.0"), Migrate13_1)
}

// Migrate13_5 converts the `templating` object in [action:send_msg] actions to use a merged list of variables.
//
// @version 13_5 "13.5"
func Migrate13_5(f Flow, cfg *Config) (Flow, error) {
localization := f.Localization()

for _, node := range f.Nodes() {
for _, action := range node.Actions() {
if action.Type() == "send_msg" {
templating, _ := action["templating"].(map[string]any)

if templating != nil {
variables := make([]string, 0, 5)
localizedVariables := make(map[i18n.Language][]string)

// the languages for which any component has param translations
localizedLangs := make(map[i18n.Language]bool)

components, _ := templating["components"].([]any)
for i := range components {
comp, _ := components[i].(map[string]any)
compUUID := GetObjectUUID(comp)
compParams, _ := comp["params"].([]any)
compParamsAsStrings := make([]string, len(compParams))
for j := range compParams {
p, _ := compParams[j].(string)
variables = append(variables, p)
compParamsAsStrings[j] = p
}

if localization != nil {
for _, lang := range localization.Languages() {
langTrans := localization.GetLanguageTranslation(lang)
if langTrans != nil {
params := langTrans.GetTranslation(compUUID, "params")
if params != nil {
localizedVariables[lang] = append(localizedVariables[lang], params...)
langTrans.DeleteTranslation(compUUID, "params")
localizedLangs[lang] = true
} else {
// maybe this component's params aren't translated but others are
localizedVariables[lang] = append(localizedVariables[lang], compParamsAsStrings...)
}
}
}
}
}

action["template"] = templating["template"]
action["template_variables"] = variables
delete(action, "templating")

if localization != nil {
for lang, langVariables := range localizedVariables {
if localizedLangs[lang] {
langTrans := localization.GetLanguageTranslation(lang)
langTrans.SetTranslation(action.UUID(), "template_variables", langVariables)
}
}
}
}
}
}
}
return f, nil
}

// Migrate13_4 converts the `templating` object in [action:send_msg] actions to use a list of components.
//
// @version 13_4 "13.4"
Expand Down
6 changes: 6 additions & 0 deletions flows/definition/migrations/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ func (n Node) Router() Router {
// Action holds an action definition
type Action map[string]any

// UUID returns the uuid of this action
func (a Action) UUID() uuids.UUID {
d, _ := a["uuid"].(string)
return uuids.UUID(d)
}

// Type returns the type of this action
func (a Action) Type() string {
d, _ := a["type"].(string)
Expand Down
89 changes: 89 additions & 0 deletions flows/definition/migrations/specdata/templates.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,93 @@
{
"13.5.0": {
"actions": {
"add_contact_groups": [
".groups[*].name_match"
],
"add_contact_urn": [
".path"
],
"add_input_labels": [
".labels[*].name_match"
],
"call_classifier": [
".input"
],
"call_resthook": [],
"call_webhook": [
".body",
".headers.*",
".url"
],
"enter_flow": [],
"open_ticket": [
".assignee.email_match",
".body"
],
"play_audio": [
".audio_url"
],
"remove_contact_groups": [
".groups[*].name_match"
],
"request_optin": [],
"say_msg": [
".text"
],
"send_broadcast": [
".attachments[*]",
".contact_query",
".groups[*].name_match",
".legacy_vars[*]",
".quick_replies[*]",
".text"
],
"send_email": [
".addresses[*]",
".body",
".subject"
],
"send_msg": [
".attachments[*]",
".quick_replies[*]",
".template_variables[*]",
".text"
],
"set_contact_channel": [],
"set_contact_field": [
".value"
],
"set_contact_language": [
".language"
],
"set_contact_name": [
".name"
],
"set_contact_status": [],
"set_contact_timezone": [
".timezone"
],
"set_run_result": [
".value"
],
"start_session": [
".contact_query",
".groups[*].name_match",
".legacy_vars[*]"
],
"transfer_airtime": []
},
"routers": {
"random": [
".operand",
".cases[*].arguments[*]"
],
"switch": [
".operand",
".cases[*].arguments[*]"
]
}
},
"13.4.0": {
"actions": {
"add_contact_groups": [
Expand Down
Loading

0 comments on commit 8e6abe0

Please sign in to comment.