Files
lincodex/backend/internal/port/outbound/company_quota.go
T

83 lines
4.3 KiB
Go

package outbound
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
var ErrCompanyQuotaSnapshotChanged = errors.New("company quota snapshot changed")
// CompanyQuotaDecision is the result of an atomic invitation or card-view
// reservation. Reused means the same invitation or a recent view already
// counted, so it is allowed without another usage charge. A denied decision
// has Reused false and RetryAt set to the earliest expiry that can free room,
// in UTC.
type CompanyQuotaDecision struct {
Allowed bool
Reused bool
RetryAt time.Time
}
// CompanyQuotaRepository atomically reserves per-organization invitation and
// candidate-card usage. Implementations are transaction-bound: a caller
// 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, 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
// even when they arrive with a different request idempotency key; pruning by
// the rolling quota window would allow an old invitation to count again.
// Card-view usage is keyed by organization and candidate, counts only when
// last_seen is strictly after the rolling-window cutoff, and expires
// logically at that boundary. Bounded cleanup is opportunistic after a
// successful reservation, so physical row removal has no exact-time promise.
// Repeated views update last_seen monotonically with the greater of the
// stored and current times.
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. 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 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 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)
}