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

🌐 Add endpoint to change a flow language #292

Merged
merged 5 commits into from
Jun 5, 2020
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
33 changes: 33 additions & 0 deletions web/flow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"

"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/goflow/utils/uuids"
Expand All @@ -20,6 +21,7 @@ func init() {
web.RegisterJSONRoute(http.MethodPost, "/mr/flow/migrate", web.RequireAuthToken(handleMigrate))
web.RegisterJSONRoute(http.MethodPost, "/mr/flow/inspect", web.RequireAuthToken(handleInspect))
web.RegisterJSONRoute(http.MethodPost, "/mr/flow/clone", web.RequireAuthToken(handleClone))
web.RegisterJSONRoute(http.MethodPost, "/mr/flow/change_language", web.RequireAuthToken(handleChangeLanguage))
}

// Migrates a flow to the latest flow specification
Expand Down Expand Up @@ -128,3 +130,34 @@ func handleClone(ctx context.Context, s *web.Server, r *http.Request) (interface

return cloneJSON, http.StatusOK, nil
}

// Changes the language of a flow by replacing the text with a translation.
//
// {
// "language": "spa",
// "flow": { "uuid": "468621a8-32e6-4cd2-afc1-04416f7151f0", "nodes": [...]}
// }
//
type changeLanguageRequest struct {
Language envs.Language `json:"language" validate:"required"`
Flow json.RawMessage `json:"flow" validate:"required"`
}

func handleChangeLanguage(ctx context.Context, s *web.Server, r *http.Request) (interface{}, int, error) {
request := &changeLanguageRequest{}
if err := utils.UnmarshalAndValidateWithLimit(r.Body, request, web.MaxRequestBytes); err != nil {
return errors.Wrapf(err, "request failed validation"), http.StatusBadRequest, nil
}

flow, err := goflow.ReadFlow(request.Flow)
if err != nil {
return errors.Wrapf(err, "unable to read flow"), http.StatusUnprocessableEntity, nil
}

copy, err := flow.ChangeLanguage(request.Language)
if err != nil {
return errors.Wrapf(err, "unable to change flow language"), http.StatusUnprocessableEntity, nil
}

return copy, http.StatusOK, nil
}
1 change: 1 addition & 0 deletions web/flow/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func TestServer(t *testing.T) {
web.RunWebTests(t, "testdata/change_language.json")
web.RunWebTests(t, "testdata/clone.json")
web.RunWebTests(t, "testdata/inspect.json")
web.RunWebTests(t, "testdata/migrate.json")
Expand Down
Loading