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") // ErrInvitationSendResultNotFound reports a missing outcome of a send for // a live idempotency record. ErrInvitationSendResultNotFound = errors.New("organization invitation send result not found") // ErrInvitationDeclineNotFound reports a declined invitation without its // stored decline. ErrInvitationDeclineNotFound = errors.New("invitation decline not found") // ErrInvitationDeclineResultNotFound reports a missing outcome of a // decline for a live idempotency record. ErrInvitationDeclineResultNotFound = errors.New("invitation decline result not found") // ErrInvitationAcceptResultNotFound reports a missing outcome of an // acceptance for a live idempotency record. ErrInvitationAcceptResultNotFound = errors.New("invitation accept result not found") ) // MaxInvitationPageSize is the largest organization-scoped or candidate page // in the contract. const MaxInvitationPageSize = 100 // InvitationRepositories exposes the invitation storage bound to one // UnitOfWork transaction. type InvitationRepositories interface { Invitations() InvitationRepository InvitationCreationResults() InvitationCreationResultRepository InvitationSendResults() InvitationSendResultRepository InvitationDeclineResults() InvitationDeclineResultRepository InvitationAcceptResults() InvitationAcceptResultRepository ContactGrants() ContactGrantRepository } // 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) // Deliver moves a draft to its delivered version in the surrounding // transaction: the invitation row moves from expectedVersion to the // version of next with the delivery time and the deadline of next, and // the audit event is recorded. The terms are not touched. It returns // ErrInvitationVersionConflict when the stored row is not a draft at // expectedVersion. Deliver(ctx context.Context, expectedVersion int64, next domain.Invitation, event domain.InvitationEvent) error // ListForCandidate returns one newest-first page of the invitations // delivered to the candidate, in any later state, with the name of the // organization that sent each of them. Drafts are never listed. ListForCandidate(ctx context.Context, query CandidateInvitationListQuery) (CandidateInvitationPage, error) // GetForCandidate returns a delivered invitation addressed to the // candidate. A draft, an invitation of another candidate, and an unknown // identifier return ErrInvitationNotFound alike. GetForCandidate(ctx context.Context, candidateID, invitationID uuid.UUID) (domain.Invitation, error) // GetCandidateEntry returns a delivered invitation addressed to the // candidate with the current name of the organization that sent it. A // draft, an invitation of another candidate, and an unknown identifier // return ErrInvitationNotFound alike. GetCandidateEntry(ctx context.Context, candidateID, invitationID uuid.UUID) (CandidateInvitationEntry, error) // GetCandidateEntryForUpdate is GetCandidateEntry that also holds the // row lock of the invitation until the surrounding transaction ends. It // locks nothing else, so a candidate command never waits for an // organization lock while it holds the invitation row. GetCandidateEntryForUpdate(ctx context.Context, candidateID, invitationID uuid.UUID) (CandidateInvitationEntry, error) // RecordView moves a delivered invitation from expectedVersion to the // viewed version of next before its deadline and records the audit // event. It returns ErrInvitationVersionConflict when the stored row is // not delivered at expectedVersion or its deadline has passed. RecordView(ctx context.Context, expectedVersion int64, next domain.Invitation, event domain.InvitationEvent) error // Decline moves an answerable invitation from expectedVersion to the // declined version of next before its deadline, stores the decline, and // records the audit event. It returns ErrInvitationVersionConflict when // the stored row is not answerable at expectedVersion or its deadline has // passed. Decline(ctx context.Context, expectedVersion int64, next domain.Invitation, decline domain.InvitationDecline, event domain.InvitationEvent) error // GetDecline returns the stored decline of the invitation or // ErrInvitationDeclineNotFound. GetDecline(ctx context.Context, organizationID, invitationID uuid.UUID) (domain.InvitationDecline, error) // Accept moves a delivered or viewed invitation from expectedVersion to the // accepted version of next before its deadline, keeps the deadline and the // terms, and records the audit event. It returns // ErrInvitationVersionConflict when the stored row is not answerable at // expectedVersion, at its terms version, or before its deadline. The consent // C4 and the contact grant are stored by the caller in the same transaction. Accept(ctx context.Context, expectedVersion int64, next domain.Invitation, event domain.InvitationEvent) 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) } // InvitationSendOutcome is the stored result of a send under an // Idempotency-Key. The contract stores a success, a missing invitation, and a // state conflict; other failures are not stored. type InvitationSendOutcome string const ( InvitationSendSent InvitationSendOutcome = "sent" InvitationSendNotFound InvitationSendOutcome = "not_found" InvitationSendStateConflict InvitationSendOutcome = "state_conflict" ) // InvitationSendResult is the outcome of one send. View is set only for // InvitationSendSent and is the original response. type InvitationSendResult struct { OrganizationID uuid.UUID InvitationID uuid.UUID Outcome InvitationSendOutcome View *domain.OrganizationInvitationView } // InvitationSendResultRepository stores the outcome of an idempotent send. // The scope references the generic idempotency record, so the outcome is // removed when that record is purged. type InvitationSendResultRepository interface { SaveSendResult(ctx context.Context, scope [IdempotencyDigestSize]byte, result InvitationSendResult) error GetSendResult(ctx context.Context, scope [IdempotencyDigestSize]byte) (InvitationSendResult, error) } // CandidateInvitationListQuery selects one page of one candidate's inbox. type CandidateInvitationListQuery struct { CandidateID uuid.UUID After *CandidateInvitationPosition Limit int32 } // CandidateInvitationPosition is the stable newest-first ordering key of the // candidate's inbox. type CandidateInvitationPosition struct { DeliveredAt time.Time InvitationID uuid.UUID } // CandidateInvitationEntry is one invitation of the candidate's inbox with // the current name of the organization that sent it. type CandidateInvitationEntry struct { Invitation domain.Invitation OrganizationName string } // CandidateInvitationPage contains one page of the candidate's inbox. type CandidateInvitationPage struct { Items []CandidateInvitationEntry HasMore bool } // 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()} } // InvitationDeclineOutcome is the stored result of a decline under an // Idempotency-Key. The contract stores a success, a missing invitation, and a // state conflict; other failures are not stored. type InvitationDeclineOutcome string const ( InvitationDeclineDeclined InvitationDeclineOutcome = "declined" InvitationDeclineNotFound InvitationDeclineOutcome = "not_found" InvitationDeclineStateConflict InvitationDeclineOutcome = "state_conflict" ) // InvitationDeclineResult is the outcome of one decline. It keeps no copy of // the response: a declined invitation is terminal and its decline is // immutable, so a replay rebuilds the same response from them, and the // comment is stored once. type InvitationDeclineResult struct { InvitationID uuid.UUID Outcome InvitationDeclineOutcome } // InvitationDeclineResultRepository stores the outcome of an idempotent // decline. The scope references the generic idempotency record, so the // outcome is removed when that record is purged. type InvitationDeclineResultRepository interface { SaveDeclineResult(ctx context.Context, scope [IdempotencyDigestSize]byte, result InvitationDeclineResult) error GetDeclineResult(ctx context.Context, scope [IdempotencyDigestSize]byte) (InvitationDeclineResult, error) } // InvitationAcceptOutcome is the stored result of an acceptance under an // Idempotency-Key. The contract stores a success, a missing invitation, and a // state conflict; a stale version and a consent refusal are not stored. type InvitationAcceptOutcome string const ( InvitationAcceptAccepted InvitationAcceptOutcome = "accepted" InvitationAcceptNotFound InvitationAcceptOutcome = "not_found" InvitationAcceptStateConflict InvitationAcceptOutcome = "state_conflict" ) // InvitationAcceptResult is the outcome of one acceptance. It keeps no copy of // the response: an accepted invitation keeps its answer and its grant, so a // replay rebuilds the response from them. type InvitationAcceptResult struct { InvitationID uuid.UUID Outcome InvitationAcceptOutcome } // InvitationAcceptResultRepository stores the outcome of an idempotent // acceptance. The scope references the generic idempotency record, so the // outcome is removed when that record is purged. type InvitationAcceptResultRepository interface { SaveAcceptResult(ctx context.Context, scope [IdempotencyDigestSize]byte, result InvitationAcceptResult) error GetAcceptResult(ctx context.Context, scope [IdempotencyDigestSize]byte) (InvitationAcceptResult, error) }