feat(invitations): accept invitations with C4 consent and contact grants; share the E0 quota across holdings

This commit is contained in:
shira committed 2026-10-09 18:03:35 +03:00
1 parent a5990d3fc6
commit 676b3c6e24
59 files changed
+4926 -194

No files matched your search

+28 -18
View File
@@ -28,12 +28,18 @@ type CompanyQuotaDecision struct {
// creates the invitation or applies the visibility change in the same
// transaction, so a failed business operation cannot leave quota charged.
//
// Writers lock rows in this order: organization, active membership,
// invitation or visibility record, then quota usage. A repository may take an
// organization lock again inside the same transaction, but must not invert
// this order. Each method rechecks the organization's stored verification
// level, basis, and changed_at against the snapshot in CompanyQuota before it
// writes; a caller cannot grant itself a tier by supplying a different level.
// Writers lock rows in this order: organization, then for an E0 organization
// its holding, then active membership, invitation or visibility record, and
// last quota usage. A repository may take an organization lock again inside
// the same transaction, but must not invert this order. Each method rechecks
// the organization's stored verification level, basis, and changed_at against
// the snapshot in CompanyQuota before it writes; a caller cannot grant itself
// a tier by supplying a different level.
//
// The E0 limits are pooled per holding: the usage of every E0 organization of
// one holding counts toward the same window. Usage rows stay attributed to the
// organization that consumed them, and idempotency stays per organization.
// Organizations at E1 and above count only their own usage.
//
// Invitation usage is keyed by organization and invitation ID and remains
// until the organization is deleted. The unique key makes retries idempotent
@@ -49,24 +55,28 @@ type CompanyQuotaRepository interface {
// LockOrganization takes the organization lock that serializes the
// reservations of one organization (FOR NO KEY UPDATE, which does not block
// the foreign-key lock of a candidate's own hide), ahead of the share locks
// of a membership check. A
// caller that checks the membership first and reserves later would hold a
// share lock and then ask for the exclusive one, and two such callers of one
// organization deadlock. A caller that reserves after a share lock takes
// this lock first, in the documented order. It returns
// of a membership check. A caller that checks the membership first and
// reserves later would hold a share lock and then ask for the exclusive
// one, and two such callers of one organization deadlock. A caller that
// reserves after a share lock takes this lock first, in the documented
// order. For an E0 organization it also takes the lock of its holding,
// which serializes the shared E0 pool; the holding is locked right after
// the organization, before any other lock of the caller, so the callers of
// one holding cannot deadlock on it. It returns
// ErrCompanyQuotaSnapshotChanged for a removed organization.
LockOrganization(ctx context.Context, organizationID uuid.UUID) error
// ReserveInvitation counts a new invitation against the current policy
// limit. An existing invitation ID is returned as allowed and reused
// without a second charge, regardless of its age or the request key. A
// denied new invitation writes no usage and reports RetryAt after the
// earliest currently counted invitation expires.
// limit of the organization's usage scope. An existing invitation ID is
// returned as allowed and reused without a second charge, regardless of
// its age or the request key. A denied new invitation writes no usage and
// reports RetryAt after the earliest currently counted invitation expires.
ReserveInvitation(ctx context.Context, quota domain.CompanyQuota, invitationID uuid.UUID) (CompanyQuotaDecision, error)
// ReserveCardView counts a distinct candidate within the rolling card-view
// window. A repeat while the prior view is active is allowed and reused,
// and refreshes last_seen with a monotonic update. A denied new view writes
// no usage and reports RetryAt after the earliest active view expires.
// window of the organization's usage scope. A repeat while the prior view
// of the same organization is active is allowed and reused, and refreshes
// last_seen with a monotonic update. A denied new view writes no usage and
// reports RetryAt after the earliest active view expires.
ReserveCardView(ctx context.Context, quota domain.CompanyQuota, candidateID uuid.UUID) (CompanyQuotaDecision, error)
}
@@ -0,0 +1,29 @@
package outbound
import (
"context"
"errors"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
// ErrContactGrantNotFound reports that the invitation has no contact grant of
// the organization. An accepted invitation always has one, so a missing grant
// of an accepted invitation is a storage defect.
var ErrContactGrantNotFound = errors.New("contact grant not found")
// ContactGrantRepository stores the contact grants of accepted invitations. A
// grant is created in the acceptance transaction next to its consent C4
// record; it is never updated here, and a read is always scoped by the
// organization.
type ContactGrantRepository interface {
// Create inserts the grant in the surrounding transaction. The database
// checks that the grant agrees with its consent record and invitation.
Create(ctx context.Context, grant domain.ContactGrant) error
// GetByInvitation returns the grant of one invitation of one organization,
// or ErrContactGrantNotFound.
GetByInvitation(ctx context.Context, organizationID, invitationID uuid.UUID) (domain.ContactGrant, error)
}
@@ -36,6 +36,10 @@ var (
// 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
@@ -49,6 +53,8 @@ type InvitationRepositories interface {
InvitationCreationResults() InvitationCreationResultRepository
InvitationSendResults() InvitationSendResultRepository
InvitationDeclineResults() InvitationDeclineResultRepository
InvitationAcceptResults() InvitationAcceptResultRepository
ContactGrants() ContactGrantRepository
}
// InvitationRepository stores invitations, their immutable terms versions, and
@@ -130,6 +136,14 @@ type InvitationRepository interface {
// 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
@@ -246,3 +260,30 @@ 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)
}
@@ -61,11 +61,14 @@ type OrganizationRepositories interface {
type OrganizationRepository interface {
// CreateWithOwner stores a new organization and the active owner
// membership of its creator. The two inserts run in the surrounding
// transaction, so a failure between them leaves neither. The database also
// refuses to commit an organization that has no active owner (constraint
// organizations_owner_required, SQLSTATE 23514, raised by the commit of
// the transaction), whatever code inserted it. owner must be an active
// owner membership of organization; an unknown account fails with
// transaction, so a failure between them leaves neither. The organization
// joins the holding of the owner account: the holding is created in the
// same transaction when that account has none, and an existing one is
// reused, so concurrent creations of one account end in one holding. The
// database also refuses to commit an organization that has no active owner
// (constraint organizations_owner_required, SQLSTATE 23514, raised by the
// commit of the transaction), whatever code inserted it. owner must be an
// active owner membership of organization; an unknown account fails with
// ErrUserNotFound, and the caller's transaction is aborted.
CreateWithOwner(ctx context.Context, organization domain.Organization, owner domain.Membership) error