feat(mail): add versioned service email templates
This commit is contained in:
1 parent
4cd7d060bd
commit
65d66b6f7d
26 files changed
+1481
-4
No files matched your search
@@ -0,0 +1,116 @@
|
||||
package mailtemplate
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
// ContentHash is the SHA-256 digest of a rendering: its template version,
|
||||
// subject, text part, and HTML part. The same version and data always give
|
||||
// the same digest, and any change to any part gives another one. The delivery
|
||||
// layer can store it with the idempotency key to pin the body of one logical
|
||||
// delivery and to detect a different body offered under the same key.
|
||||
type ContentHash [sha256.Size]byte
|
||||
|
||||
// String returns the lowercase hex digest. It is not secret: SHA-256 over a
|
||||
// body that holds a 256-bit token cannot be reversed.
|
||||
func (hash ContentHash) String() string { return hex.EncodeToString(hash[:]) }
|
||||
|
||||
// RenderedMail is one immutable rendering, built only by Renderer. The
|
||||
// subject and bodies are what a transport sends; the link in a verification
|
||||
// or reset body carries a bearer token.
|
||||
//
|
||||
// The content sits behind a pointer with read-only accessors, so a caller
|
||||
// cannot change a body after its ContentHash was computed, and printing an
|
||||
// enclosing struct, even through an unexported field, shows only an address.
|
||||
// String, GoString, Format, and LogValue show the kind, the template version,
|
||||
// and the content hash, never the subject or the bodies.
|
||||
type RenderedMail struct {
|
||||
content *renderedContent
|
||||
}
|
||||
|
||||
type renderedContent struct {
|
||||
kind Kind
|
||||
version TemplateVersion
|
||||
subject string
|
||||
text string
|
||||
html string
|
||||
contentHash ContentHash
|
||||
}
|
||||
|
||||
func newRenderedMail(kind Kind, version TemplateVersion, subject, text, html string) RenderedMail {
|
||||
return RenderedMail{content: &renderedContent{
|
||||
kind: kind,
|
||||
version: version,
|
||||
subject: subject,
|
||||
text: text,
|
||||
html: html,
|
||||
contentHash: contentHash(string(version), subject, text, html),
|
||||
}}
|
||||
}
|
||||
|
||||
// IsZero reports a RenderedMail not built by Renderer.
|
||||
func (mail RenderedMail) IsZero() bool { return mail.content == nil }
|
||||
|
||||
func (mail RenderedMail) Kind() Kind { return mail.read().kind }
|
||||
|
||||
func (mail RenderedMail) TemplateVersion() TemplateVersion { return mail.read().version }
|
||||
|
||||
func (mail RenderedMail) Subject() string { return mail.read().subject }
|
||||
|
||||
// Text returns the plain-text part. It may contain a bearer token; never log it.
|
||||
func (mail RenderedMail) Text() string { return mail.read().text }
|
||||
|
||||
// HTML returns the HTML part. It may contain a bearer token; never log it.
|
||||
func (mail RenderedMail) HTML() string { return mail.read().html }
|
||||
|
||||
func (mail RenderedMail) ContentHash() ContentHash { return mail.read().contentHash }
|
||||
|
||||
func (mail RenderedMail) read() renderedContent {
|
||||
if mail.content == nil {
|
||||
return renderedContent{}
|
||||
}
|
||||
return *mail.content
|
||||
}
|
||||
|
||||
// ToOutgoingMail addresses the rendering for the MailSender port. The
|
||||
// recipient and the idempotency key come from the delivery, never from the
|
||||
// template data.
|
||||
func (mail RenderedMail) ToOutgoingMail(to domain.EmailAddress, idempotencyKey string) outbound.OutgoingMail {
|
||||
content := mail.read()
|
||||
return outbound.OutgoingMail{
|
||||
To: to,
|
||||
Subject: content.subject,
|
||||
TextBody: content.text,
|
||||
HTMLBody: content.html,
|
||||
IdempotencyKey: idempotencyKey,
|
||||
}
|
||||
}
|
||||
|
||||
func (mail RenderedMail) String() string {
|
||||
if mail.IsZero() {
|
||||
return "[empty rendered mail]"
|
||||
}
|
||||
return fmt.Sprintf("[rendered mail %s %s, content redacted]", mail.content.version, mail.content.contentHash)
|
||||
}
|
||||
|
||||
func (mail RenderedMail) GoString() string { return mail.String() }
|
||||
|
||||
// Format applies String to every verb; without it %+v would print the fields.
|
||||
func (mail RenderedMail) Format(state fmt.State, _ rune) {
|
||||
_, _ = state.Write([]byte(mail.String()))
|
||||
}
|
||||
|
||||
func (mail RenderedMail) LogValue() slog.Value {
|
||||
content := mail.read()
|
||||
return slog.GroupValue(
|
||||
slog.String("kind", string(content.kind)),
|
||||
slog.String("template_version", string(content.version)),
|
||||
slog.String("content_hash", content.contentHash.String()),
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user