179 lines
7.2 KiB
Go
179 lines
7.2 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var ErrInvalidNotificationInboxEntry = errors.New("invalid notification inbox entry")
|
|
|
|
// NotificationInboxEventType is an allowlisted source event that can appear
|
|
// in the internal inbox.
|
|
type NotificationInboxEventType string
|
|
|
|
const (
|
|
// NotificationInboxInvitationDelivered records that an invitation reached
|
|
// the candidate's inbox. Its source is the invitation.
|
|
NotificationInboxInvitationDelivered NotificationInboxEventType = "invitation.delivered"
|
|
// NotificationInboxSyntheticDelivered is a synthetic event type. It
|
|
// carries no product meaning: it exists to exercise the whole inbox path
|
|
// (outbox, consumer, read-time visibility check, listing, read state)
|
|
// before real producers exist and in browser tests. Its source is the
|
|
// sending organization, and its content is a fixed notice that names that
|
|
// organization.
|
|
NotificationInboxSyntheticDelivered NotificationInboxEventType = "notification.synthetic_delivered"
|
|
)
|
|
|
|
// NotificationInboxSourceType identifies the current source-of-truth record
|
|
// used to resolve an inbox item's content.
|
|
type NotificationInboxSourceType string
|
|
|
|
const (
|
|
NotificationInboxSourceInvitation NotificationInboxSourceType = "invitation"
|
|
NotificationInboxSourceOrganization NotificationInboxSourceType = "organization"
|
|
)
|
|
|
|
// NotificationInboxSourceFor returns the only source type that an event type
|
|
// may refer to, and false for an event type that is not allowlisted. The
|
|
// database repeats the same pairs in a CHECK constraint.
|
|
func NotificationInboxSourceFor(eventType NotificationInboxEventType) (NotificationInboxSourceType, bool) {
|
|
switch eventType {
|
|
case NotificationInboxInvitationDelivered:
|
|
return NotificationInboxSourceInvitation, true
|
|
case NotificationInboxSyntheticDelivered:
|
|
return NotificationInboxSourceOrganization, true
|
|
default:
|
|
return "", false
|
|
}
|
|
}
|
|
|
|
// NotificationInboxEntryInput contains identifiers and event metadata only.
|
|
// It deliberately has no content snapshot: presentation is resolved from the
|
|
// current source and its visibility rules when the item is read.
|
|
type NotificationInboxEntryInput struct {
|
|
EventID uuid.UUID
|
|
RecipientUserID uuid.UUID
|
|
OrganizationID uuid.UUID
|
|
EventType NotificationInboxEventType
|
|
SchemaVersion int32
|
|
SourceType NotificationInboxSourceType
|
|
SourceID uuid.UUID
|
|
OccurredAt time.Time
|
|
ReadAt *time.Time
|
|
}
|
|
|
|
// NotificationInboxEntry is an immutable delivered event plus its
|
|
// recipient-specific read state. EventID is the idempotency identity for one
|
|
// recipient; the same source event can be delivered to different recipients.
|
|
type NotificationInboxEntry struct {
|
|
eventID uuid.UUID
|
|
recipientUserID uuid.UUID
|
|
organizationID uuid.UUID
|
|
eventType NotificationInboxEventType
|
|
schemaVersion int32
|
|
sourceType NotificationInboxSourceType
|
|
sourceID uuid.UUID
|
|
occurredAt time.Time
|
|
readAt *time.Time
|
|
}
|
|
|
|
// NewNotificationInboxEntry validates an event record before it is stored.
|
|
func NewNotificationInboxEntry(input NotificationInboxEntryInput) (NotificationInboxEntry, error) {
|
|
const location = "domain.NewNotificationInboxEntry"
|
|
if input.ReadAt != nil {
|
|
return NotificationInboxEntry{}, fmt.Errorf("%s - new entries cannot start as read: %w", location, ErrInvalidNotificationInboxEntry)
|
|
}
|
|
return restoreNotificationInboxEntry(input)
|
|
}
|
|
|
|
// RestoreNotificationInboxEntry validates inbox metadata loaded from storage.
|
|
func RestoreNotificationInboxEntry(input NotificationInboxEntryInput) (NotificationInboxEntry, error) {
|
|
const location = "domain.RestoreNotificationInboxEntry"
|
|
return restoreNotificationInboxEntryWithLocation(input, location)
|
|
}
|
|
|
|
func restoreNotificationInboxEntry(input NotificationInboxEntryInput) (NotificationInboxEntry, error) {
|
|
return restoreNotificationInboxEntryWithLocation(input, "domain.NewNotificationInboxEntry")
|
|
}
|
|
|
|
func restoreNotificationInboxEntryWithLocation(
|
|
input NotificationInboxEntryInput,
|
|
location string,
|
|
) (NotificationInboxEntry, error) {
|
|
if input.EventID == uuid.Nil || input.RecipientUserID == uuid.Nil || input.OrganizationID == uuid.Nil || input.SourceID == uuid.Nil {
|
|
return NotificationInboxEntry{}, fmt.Errorf("%s - event, recipient, organization, and source IDs are required: %w", location, ErrInvalidNotificationInboxEntry)
|
|
}
|
|
sourceType, known := NotificationInboxSourceFor(input.EventType)
|
|
if !known || input.SchemaVersion != 1 || input.SourceType != sourceType {
|
|
return NotificationInboxEntry{}, fmt.Errorf("%s - event type, schema version, and source type are unsupported: %w", location, ErrInvalidNotificationInboxEntry)
|
|
}
|
|
if input.OccurredAt.IsZero() {
|
|
return NotificationInboxEntry{}, fmt.Errorf("%s - occurrence time is required: %w", location, ErrInvalidNotificationInboxEntry)
|
|
}
|
|
occurredAt := canonicalNotificationInboxTime(input.OccurredAt)
|
|
var readAt *time.Time
|
|
if input.ReadAt != nil {
|
|
if input.ReadAt.IsZero() {
|
|
return NotificationInboxEntry{}, fmt.Errorf("%s - read time cannot be zero: %w", location, ErrInvalidNotificationInboxEntry)
|
|
}
|
|
canonical := canonicalNotificationInboxTime(*input.ReadAt)
|
|
readAt = &canonical
|
|
}
|
|
return NotificationInboxEntry{
|
|
eventID: input.EventID,
|
|
recipientUserID: input.RecipientUserID,
|
|
organizationID: input.OrganizationID,
|
|
eventType: input.EventType,
|
|
schemaVersion: input.SchemaVersion,
|
|
sourceType: input.SourceType,
|
|
sourceID: input.SourceID,
|
|
occurredAt: occurredAt,
|
|
readAt: readAt,
|
|
}, nil
|
|
}
|
|
|
|
// MarkRead returns the same entry with its first read timestamp. A retry does
|
|
// not replace the original timestamp.
|
|
func (entry NotificationInboxEntry) MarkRead(at time.Time) (NotificationInboxEntry, error) {
|
|
const location = "NotificationInboxEntry.MarkRead"
|
|
if entry.IsZero() || at.IsZero() {
|
|
return NotificationInboxEntry{}, fmt.Errorf("%s - entry and read time are required: %w", location, ErrInvalidNotificationInboxEntry)
|
|
}
|
|
if entry.readAt != nil {
|
|
return entry, nil
|
|
}
|
|
readAt := canonicalNotificationInboxTime(at)
|
|
entry.readAt = &readAt
|
|
return entry, nil
|
|
}
|
|
|
|
func canonicalNotificationInboxTime(value time.Time) time.Time {
|
|
return value.UTC().Truncate(time.Microsecond)
|
|
}
|
|
|
|
func (entry NotificationInboxEntry) EventID() uuid.UUID { return entry.eventID }
|
|
func (entry NotificationInboxEntry) RecipientUserID() uuid.UUID { return entry.recipientUserID }
|
|
func (entry NotificationInboxEntry) OrganizationID() uuid.UUID { return entry.organizationID }
|
|
func (entry NotificationInboxEntry) EventType() NotificationInboxEventType {
|
|
return entry.eventType
|
|
}
|
|
func (entry NotificationInboxEntry) SchemaVersion() int32 { return entry.schemaVersion }
|
|
func (entry NotificationInboxEntry) SourceType() NotificationInboxSourceType {
|
|
return entry.sourceType
|
|
}
|
|
func (entry NotificationInboxEntry) SourceID() uuid.UUID { return entry.sourceID }
|
|
func (entry NotificationInboxEntry) OccurredAt() time.Time { return entry.occurredAt }
|
|
func (entry NotificationInboxEntry) ReadAt() (time.Time, bool) {
|
|
if entry.readAt == nil {
|
|
return time.Time{}, false
|
|
}
|
|
return *entry.readAt, true
|
|
}
|
|
|
|
func (entry NotificationInboxEntry) IsZero() bool {
|
|
return entry.eventID == uuid.Nil || entry.recipientUserID == uuid.Nil || entry.organizationID == uuid.Nil || entry.sourceID == uuid.Nil
|
|
}
|