Make 'view in browser' on emails work correctly

This commit is contained in:
Eli Ribble 2026-02-02 19:34:37 +00:00
parent 00a75a556e
commit 9d7ca81508
No known key found for this signature in database
13 changed files with 106 additions and 85 deletions

View file

@ -27,6 +27,20 @@ func convertToPGData(data map[string]string) pgtypes.HStore {
return result
}
func convertFromPGData(d pgtypes.HStore) map[string]string {
result := make(map[string]string, 0)
for k, v := range d {
var s string
err := v.Scan(&s)
if err != nil {
log.Warn().Str("key", k).Msg("Failed to convert from HSTORE")
continue
}
result[k] = s
}
return result
}
func ensureInDB(ctx context.Context, destination string) (err error) {
_, err = models.FindCommsEmailContact(ctx, db.PGInstance.BobDB, destination)
if err != nil {
@ -61,7 +75,7 @@ func insertEmailLog(ctx context.Context, data map[string]string, destination str
SentAt: omitnull.FromPtr[time.Time](nil),
Source: omit.From(source),
Subject: omit.From(subject),
TemplateID: omitnull.From(templateInitialID),
TemplateID: omit.From(templateInitialID),
TemplateData: omit.From(data_for_insert),
Type: omit.From(enums.CommsMessagetypeemailInitialContact),
}).One(ctx, db.PGInstance.BobDB)

View file

@ -46,7 +46,7 @@ func sendEmailInitialContact(ctx context.Context, destination string) error {
data["url_subscribe"] = config.MakeURLReport("/email/subscribe?email=%s", destination)
data["url_unsubscribe"] = config.MakeURLReport("/email/unsubscribe")
public_id := generatePublicId(enums.CommsMessagetypeemailInitialContact, data)
data["url_browser"] = config.MakeURLReport("/email?id=%s", public_id)
data["url_browser"] = config.MakeURLReport("/email/%s", public_id)
text, html, err := renderEmailTemplates(templateInitialID, data)
if err != nil {

View file

@ -55,7 +55,7 @@ func sendEmailReportConfirmation(ctx context.Context, job Job) error {
data["URLLogo"] = config.MakeURLReport("/static/img/nidus-logo-no-lettering-64.png")
data["URLReportStatus"] = config.MakeURLReport("/foo")
data["URLReportUnsubscribe"] = config.MakeURLReport("/email/unsubscribe")
data["URLViewInBrowser"] = config.MakeURLReport("/email?id=%s", public_id)
data["URLViewInBrowser"] = config.MakeURLReport("/email/%s", public_id)
text, html, err := renderEmailTemplates(templateReportNotificationConfirmationID, data)
if err != nil {
return fmt.Errorf("Failed to render email report notification template: %w", err)

View file

@ -19,6 +19,7 @@ import (
"github.com/Gleipnir-Technology/bob"
"github.com/Gleipnir-Technology/bob/dialect/psql"
"github.com/Gleipnir-Technology/bob/dialect/psql/um"
"github.com/Gleipnir-Technology/bob/types/pgtypes"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
@ -90,6 +91,20 @@ func LoadTemplates() error {
return nil
}
func RenderHTML(template_id int32, s pgtypes.HStore) (html []byte, err error) {
data := convertFromPGData(s)
t, ok := templateByID[template_id]
if !ok {
return []byte{}, fmt.Errorf("Failed to lookup template %d", template_id)
}
buf_html := &bytes.Buffer{}
err = t.executeTemplateHTML(buf_html, data)
if err != nil {
return []byte{}, fmt.Errorf("Failed to render HTML template: %w", err)
}
return buf_html.Bytes(), nil
}
func loadTemplateID(ctx context.Context, tx bob.Tx, t enums.CommsMessagetypeemail) (int32, error) {
templates, err := models.CommsEmailTemplates.Query(
models.SelectWhere.CommsEmailTemplates.MessageType.EQ(t),