package outbound import ( "context" "errors" "time" "github.com/google/uuid" "lincodex/backend/internal/domain" ) var ( // ErrInvitationNotFound reports that the invitation does not exist in the // organization. An invitation of another organization is reported the same // way, so an identifier reveals nothing. ErrInvitationNotFound = errors.New("organization invitation not found") // ErrInvitationVersionConflict reports that the draft is no longer at the // version the caller read, or is no longer a draft. The surrounding // transaction stays usable and the caller decides which conflict applies. ErrInvitationVersionConflict = errors.New("organization invitation changed concurrently") // ErrInvitationCreationResultNotFound reports a missing original response // for a live idempotency record. ErrInvitationCreationResultNotFound = errors.New("organization invitation creation result not found") ) // MaxInvitationPageSize is the largest organization-scoped page in the // contract. const MaxInvitationPageSize = 100 // InvitationRepositories exposes the invitation storage bound to one // UnitOfWork transaction. type InvitationRepositories interface { Invitations() InvitationRepository InvitationCreationResults() InvitationCreationResultRepository } // InvitationRepository stores invitations, their immutable terms versions, and // their audit events. Every read scopes by the organization and the // invitation, so a foreign identifier finds nothing. type InvitationRepository interface { // Create inserts version 1 of a draft, its first terms version, and its // first audit event in the surrounding transaction. Create(ctx context.Context, invitation domain.Invitation, event domain.InvitationEvent) error // Get returns the current invitation or ErrInvitationNotFound. Get(ctx context.Context, organizationID, invitationID uuid.UUID) (domain.Invitation, error) // GetForUpdate returns the current invitation and holds its row lock until // the surrounding transaction ends. GetForUpdate(ctx context.Context, organizationID, invitationID uuid.UUID) (domain.Invitation, error) // GetTermsVersion returns one immutable historical terms version or // ErrInvitationNotFound. GetTermsVersion(ctx context.Context, organizationID, invitationID uuid.UUID, version int64) (domain.InvitationTerms, error) // List returns one newest-first page of the organization's invitations. List(ctx context.Context, query InvitationListQuery) (InvitationPage, error) // SaveDraft appends the next version of a draft in the surrounding // transaction: the invitation row moves from expectedVersion to the version // of next, a changed terms version is appended as a new immutable snapshot, // and the audit event is recorded. It returns ErrInvitationVersionConflict // when the stored row is not a draft at expectedVersion. SaveDraft(ctx context.Context, expectedVersion int64, next domain.Invitation, termsChanged bool, event domain.InvitationEvent) error // NeedVersionExists reports whether the need of the organization has had // the requirements version. NeedVersionExists(ctx context.Context, organizationID, needID uuid.UUID, requirementsVersion int64) (bool, error) } // InvitationCreationResultRepository stores the original response of a // successful idempotent create. The scope references the generic idempotency // record, so the response is removed when that record is purged. type InvitationCreationResultRepository interface { SaveCreationResult(ctx context.Context, scope [IdempotencyDigestSize]byte, result domain.OrganizationInvitationView) error GetCreationResult(ctx context.Context, scope [IdempotencyDigestSize]byte) (domain.OrganizationInvitationView, error) } // InvitationListQuery selects one page within a single organization. type InvitationListQuery struct { OrganizationID uuid.UUID After *InvitationListPosition Limit int32 } // InvitationListPosition is the stable newest-first ordering key. type InvitationListPosition struct { CreatedAt time.Time InvitationID uuid.UUID } // InvitationPage contains one organization-scoped page of invitations. type InvitationPage struct { Items []domain.Invitation HasMore bool } // InvitationCreationPosition returns the pagination key of one invitation. func InvitationCreationPosition(invitation domain.Invitation) InvitationListPosition { return InvitationListPosition{CreatedAt: invitation.CreatedAt(), InvitationID: invitation.ID()} }