Enable template selection on admin single/bulk and scheduled emails, send welcome emails only after email registration OTP verification, and use the password_reset template for email-based reset links. Co-authored-by: Cursor <cursoragent@cursor.com>
51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"Yimaru-Backend/internal/domain"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func parseOutboundEmailFromForm(c *fiber.Ctx) (domain.OutboundEmailContent, error) {
|
|
content := domain.OutboundEmailContent{
|
|
TemplateSlug: strings.TrimSpace(c.FormValue("email_template_slug")),
|
|
Subject: strings.TrimSpace(c.FormValue("subject")),
|
|
Message: strings.TrimSpace(c.FormValue("message")),
|
|
HTML: strings.TrimSpace(c.FormValue("html")),
|
|
}
|
|
|
|
varsRaw := strings.TrimSpace(c.FormValue("template_variables"))
|
|
if varsRaw != "" {
|
|
var vars map[string]any
|
|
if err := json.Unmarshal([]byte(varsRaw), &vars); err != nil {
|
|
return domain.OutboundEmailContent{}, fiber.NewError(fiber.StatusBadRequest, "template_variables must be valid JSON")
|
|
}
|
|
content.TemplateVariables = vars
|
|
}
|
|
|
|
if content.TemplateSlug == "" {
|
|
if content.Subject == "" {
|
|
return domain.OutboundEmailContent{}, fiber.NewError(fiber.StatusBadRequest, "subject is required when no email_template_slug is provided")
|
|
}
|
|
if content.Message == "" && content.HTML == "" {
|
|
return domain.OutboundEmailContent{}, fiber.NewError(fiber.StatusBadRequest, "message or html is required when no email_template_slug is provided")
|
|
}
|
|
}
|
|
|
|
return content, nil
|
|
}
|
|
|
|
func outboundEmailTemplateVariablesJSON(content domain.OutboundEmailContent) json.RawMessage {
|
|
if len(content.TemplateVariables) == 0 {
|
|
return nil
|
|
}
|
|
raw, err := json.Marshal(content.TemplateVariables)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return raw
|
|
}
|