package outbound import ( "context" "errors" "time" "github.com/google/uuid" "lincodex/backend/internal/domain" ) var ( // ErrNotificationInboxEntryNotFound covers a missing event and an event // owned by a different recipient or organization context. ErrNotificationInboxEntryNotFound = errors.New("notification inbox entry not found") // ErrNotificationInboxEventConflict reports an event ID reused with // different immutable recipient or source metadata. ErrNotificationInboxEventConflict = errors.New("notification inbox event metadata conflicts") // ErrNotificationInboxReferenceMissing reports an event that names an // organization that does not exist. A retry cannot fix it. ErrNotificationInboxReferenceMissing = errors.New("notification inbox event references a missing record") // ErrNotificationInboxRecipientMissing reports a recipient account that // does not exist now. The recipient may have been deleted a moment ago, // so a handler retries and then skips instead of rejecting the event. ErrNotificationInboxRecipientMissing = errors.New("notification inbox recipient does not exist") // ErrNotificationInboxSourceNotVisible reports a source that does not // exist or is not visible to the recipient now. The two cases are one on // purpose. ErrNotificationInboxSourceNotVisible = errors.New("notification source is not visible to the recipient") ) const MaxNotificationInboxPageSize = 100 // NotificationInboxRepository stores recipient-scoped event references and // read state. Implementations can bind these methods to a transaction; the // standalone PostgreSQL store uses one SQL statement per operation. type NotificationInboxRepository interface { // Store inserts one recipient-specific event. It returns false for an // identical event already stored under the same recipient and event ID. Store(ctx context.Context, entry domain.NotificationInboxEntry) (created bool, err error) // List returns one newest-first page for the exact recipient. A nil // OrganizationID means all organization contexts of that recipient. List(ctx context.Context, query NotificationInboxListQuery) (NotificationInboxPage, error) // MarkRead records the first read time for the recipient. A non-nil // organizationID narrows the match to that organization context; nil // matches any context of the recipient, which is the candidate cabinet. // Missing and unauthorized identities share one not-found result. MarkRead( ctx context.Context, recipientUserID uuid.UUID, organizationID *uuid.UUID, eventID uuid.UUID, at time.Time, ) (domain.NotificationInboxEntry, error) // OrganizationNameForRecipient returns the current name of an // organization under the recipient's current standing: a candidate who has // not hidden it, or an employer with an active membership in it. It // returns ErrNotificationInboxSourceNotVisible otherwise. Content that // names an organization is resolved through it at read time, so a hiding // or a revoked membership removes the name from every later read. OrganizationNameForRecipient(ctx context.Context, recipientUserID, organizationID uuid.UUID) (string, error) } // NotificationInboxListQuery selects a bounded recipient-owned page. type NotificationInboxListQuery struct { RecipientUserID uuid.UUID OrganizationID *uuid.UUID After *NotificationInboxPosition Limit int32 } // NotificationInboxPosition continues a newest-first inbox page. type NotificationInboxPosition struct { OccurredAt time.Time EventID uuid.UUID } // NotificationInboxPage is a bounded page plus whether another row exists. type NotificationInboxPage struct { Items []domain.NotificationInboxEntry HasMore bool } // NotificationInboxPositionOf returns the continuation key for an entry. func NotificationInboxPositionOf(entry domain.NotificationInboxEntry) NotificationInboxPosition { return NotificationInboxPosition{OccurredAt: entry.OccurredAt(), EventID: entry.EventID()} }