Files
lincodex/backend/internal/domain/mail_subject.go
T

84 lines
3.1 KiB
Go

package domain
import (
"errors"
"fmt"
"log/slog"
"github.com/google/uuid"
)
// ErrInvalidMailSubject reports a subject that names no record or breaks the
// rule of its kind. Errors name the rule, never an identifier.
var ErrInvalidMailSubject = errors.New("invalid mail subject")
// MailSubjectKind names the kind of record a notification is about. The
// values are stored, and each one has a rights checker in the sending usecase.
type MailSubjectKind string
const (
// MailSubjectInvitation is an invitation addressed to the recipient. The
// subject ID is the invitation and the organization is its sender.
MailSubjectInvitation MailSubjectKind = "invitation"
)
// MailSubject links a notification to the record whose rights must still hold
// when the mail is sent. The sending worker re-checks those rights before the
// transport is called, so a notification that outlives its basis is cancelled
// instead of sent. The zero value names no subject; the worker then has no
// rights to check and cancels the notification with source_closed.
type MailSubject struct {
kind MailSubjectKind
id uuid.UUID
organizationID uuid.UUID
}
// NewInvitationMailSubject names an invitation and the organization that sent
// it. Both IDs must be set.
func NewInvitationMailSubject(invitationID, organizationID uuid.UUID) (MailSubject, error) {
if invitationID == uuid.Nil || organizationID == uuid.Nil {
return MailSubject{}, fmt.Errorf("domain.NewInvitationMailSubject - invitation and organization IDs must be set: %w", ErrInvalidMailSubject)
}
return MailSubject{kind: MailSubjectInvitation, id: invitationID, organizationID: organizationID}, nil
}
// RestoreMailSubject rebuilds a stored subject. A kind of "" with nil IDs is
// the zero subject; any other combination must be a complete known subject.
func RestoreMailSubject(kind string, id, organizationID uuid.UUID) (MailSubject, error) {
if kind == "" && id == uuid.Nil && organizationID == uuid.Nil {
return MailSubject{}, nil
}
switch MailSubjectKind(kind) {
case MailSubjectInvitation:
return NewInvitationMailSubject(id, organizationID)
default:
return MailSubject{}, fmt.Errorf("domain.RestoreMailSubject - unknown subject kind: %w", ErrInvalidMailSubject)
}
}
// IsZero reports a subject that names nothing.
func (subject MailSubject) IsZero() bool { return subject.kind == "" }
func (subject MailSubject) Kind() MailSubjectKind { return subject.kind }
// ID is the identifier of the subject record.
func (subject MailSubject) ID() uuid.UUID { return subject.id }
// OrganizationID is the organization the subject record belongs to.
func (subject MailSubject) OrganizationID() uuid.UUID { return subject.organizationID }
// String shows the kind only; the identifiers stay out of printed values.
func (subject MailSubject) String() string {
if subject.IsZero() {
return "[empty mail subject]"
}
return fmt.Sprintf("[mail subject %s]", subject.kind)
}
func (subject MailSubject) GoString() string { return subject.String() }
// LogValue carries the kind only.
func (subject MailSubject) LogValue() slog.Value {
return slog.StringValue(string(subject.kind))
}