chore: checkpoint assessment and inbox integration
This commit is contained in:
1 parent
88a31ba0d5
commit
5c52fd8b91
31 files changed
+4608
-181
No files matched your search
@@ -0,0 +1,149 @@
|
||||
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 NotificationInboxEventType = "invitation.delivered"
|
||||
|
||||
// NotificationInboxSourceType identifies the current source-of-truth record
|
||||
// used to resolve an inbox item's content.
|
||||
type NotificationInboxSourceType string
|
||||
|
||||
const NotificationInboxSourceInvitation NotificationInboxSourceType = "invitation"
|
||||
|
||||
// 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)
|
||||
}
|
||||
if input.EventType != NotificationInboxInvitationDelivered || input.SchemaVersion != 1 || input.SourceType != NotificationInboxSourceInvitation {
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user