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

DXCDT-323: Allow updating an email template when it does not exist #611

Merged
merged 5 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions internal/auth0/email_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package auth0
import "github.com/auth0/go-auth0/management"

type EmailTemplateAPI interface {
// Create an email template.
//
// See: https://auth0.com/docs/api/management/v2#!/Email_Templates/post_email_templates
Create(template *management.EmailTemplate, opts ...management.RequestOption) error

// Read an email template by pre-defined name.
//
// These names are `verify_email`, `reset_email`, `welcome_email`,
Expand Down
35 changes: 29 additions & 6 deletions internal/cli/email_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package cli

import (
"fmt"
"net/http"

"github.com/auth0/go-auth0/management"
"github.com/spf13/cobra"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/auth0/auth0-cli/internal/prompt"
)

Expand Down Expand Up @@ -133,13 +135,11 @@ func showEmailTemplateCmd(cli *cli) *cobra.Command {
}

var email *management.EmailTemplate

if err := ansi.Waiting(func() error {
var err error
if err := ansi.Waiting(func() (err error) {
email, err = cli.api.EmailTemplate.Read(apiEmailTemplateFor(inputs.Template))
return err
}); err != nil {
return fmt.Errorf("Unable to get the email template '%s': %w", inputs.Template, err)
return fmt.Errorf("failed to get the email template '%s': %w", inputs.Template, err)
}

cli.renderer.EmailTemplateShow(email)
Expand Down Expand Up @@ -192,12 +192,30 @@ func updateEmailTemplateCmd(cli *cli) *cobra.Command {
}

var oldTemplate *management.EmailTemplate
templateExists := true
err := ansi.Waiting(func() (err error) {
oldTemplate, err = cli.api.EmailTemplate.Read(apiEmailTemplateFor(inputs.Template))
return err
})
if err != nil {
return fmt.Errorf("failed to get the email template '%s': %w", inputs.Template, err)
wrappedError := fmt.Errorf("failed to get the email template '%s': %w", inputs.Template, err)

mErr, ok := err.(management.Error)
if !ok {
sergiught marked this conversation as resolved.
Show resolved Hide resolved
return wrappedError
}

if mErr.Status() != http.StatusNotFound {
return wrappedError
} else {
templateExists = false
oldTemplate = &management.EmailTemplate{
From: auth0.String(""),
Subject: auth0.String(""),
Enabled: auth0.Bool(false),
Syntax: auth0.String("liquid"),
}
}
}

if err := emailTemplateFrom.AskU(cmd, &inputs.From, oldTemplate.From); err != nil {
Expand Down Expand Up @@ -234,6 +252,7 @@ func updateEmailTemplateCmd(cli *cli) *cobra.Command {
emailTemplate := &management.EmailTemplate{
Enabled: &inputs.Enabled,
Template: &template,
Syntax: oldTemplate.Syntax,
}
if inputs.Body != "" {
emailTemplate.Body = &inputs.Body
Expand All @@ -252,7 +271,11 @@ func updateEmailTemplateCmd(cli *cli) *cobra.Command {
}

if err = ansi.Waiting(func() error {
return cli.api.EmailTemplate.Update(template, emailTemplate)
if templateExists {
return cli.api.EmailTemplate.Update(template, emailTemplate)
}

return cli.api.EmailTemplate.Create(emailTemplate)
}); err != nil {
return fmt.Errorf("failed to update the email template '%s': %w", inputs.Template, err)
}
Expand Down