package templates
import (
"bytes"
"html/template"
"regexp"
"strings"
"github.com/warmbly/warmbly/internal/observability/errs"
)
// ClientReportEmail is a scheduled client report summary. Every name in it must already be display-safe.
type ClientReportEmail struct {
BrandName string
LogoURL string
AccentColor string
ReportName string
Period string
Metrics []ClientReportMetric
Campaigns []ClientReportEmailCampaign
ReportURL string
// PoweredByName and PoweredByURL are the deployment's attribution, empty when the brand removed it.
PoweredByName string
PoweredByURL string
}
// ClientReportMetric is one KPI tile.
type ClientReportMetric struct {
Label string
Value string
Note string
}
// ClientReportEmailCampaign is one row of the email's campaign summary.
type ClientReportEmailCampaign struct {
Name string
Sent string
Replies string
}
var hexColor = regexp.MustCompile(`^#[0-9a-fA-F]{6}$`)
const clientReportFont = `-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif`
const clientReportHTML = `
{{.ReportName}}
{{if .LogoURL}} {{else}}{{.BrandName}}{{end}}
|
|
{{.Period}}
{{.ReportName}}
|
{{range .Metrics}}
{{.Label}}
{{.Value}}
{{if .Note}} {{.Note}} {{end}}
| {{end}}
|
{{if .Campaigns}}
| Campaign | Sent | Replies |
{{range .Campaigns}}| {{.Name}} | {{.Sent}} | {{.Replies}} | {{end}}
| {{end}}
|
|
|
|
{{.BrandName}}{{if .PoweredByName}} ยท {{if .PoweredByURL}}Powered by {{.PoweredByName}}{{else}}Powered by {{.PoweredByName}}{{end}}{{end}}
|
`
var clientReportTmpl = template.Must(template.New("client_report").Parse(clientReportHTML))
// GenerateClientReportHTML renders a scheduled client report email.
func GenerateClientReportHTML(in ClientReportEmail) (string, error) {
accent := "#0f172a"
if hexColor.MatchString(in.AccentColor) {
accent = in.AccentColor
}
if !strings.HasPrefix(in.LogoURL, "https://") {
in.LogoURL = ""
}
data := struct {
ClientReportEmail
Accent template.CSS
Font template.CSS
}{ClientReportEmail: in, Accent: template.CSS(accent), Font: template.CSS(clientReportFont)}
var buf bytes.Buffer
if err := clientReportTmpl.Execute(&buf, data); err != nil {
errs.CaptureException(err)
return "", err
}
return buf.String(), nil
}