Files
lincodex/backend/internal/domain/company_quota.go
T

145 lines
5.6 KiB
Go

package domain
import (
"errors"
"fmt"
"time"
"github.com/google/uuid"
)
var (
// ErrInvalidCompanyQuota reports a quota request that has incomplete or
// inconsistent trusted inputs.
ErrInvalidCompanyQuota = errors.New("invalid company quota")
// ErrCompanyQuotaDenied reports an organization level that has no active
// quota tier. Callers must refuse the action instead of falling back.
ErrCompanyQuotaDenied = errors.New("company quota denied")
)
// SharesHoldingPool reports whether the usage of an organization at this tier
// is counted in the pool of its holding instead of its own. Only the
// unverified tier E0 shares a pool: the organizations of one holding at E0
// draw on one allowance, so creating more of them does not multiply it. A
// verified tier keeps its own pool, and usage of any organization that is no
// longer E0 leaves the shared pool.
func (tier OrganizationTier) SharesHoldingPool() bool {
return tier == OrganizationTierE0
}
// CompanyQuota is an immutable snapshot of the limits that apply to one
// organization at one instant. It carries the stored verification and
// effective policy identities so the transactional repository can verify
// that neither changed before it reserves usage. Its usage scope is the
// organization, or the holding pool for a tier that SharesHoldingPool.
type CompanyQuota struct {
organizationID uuid.UUID
policyVersionID PolicyVersionID
verificationLevel OrganizationVerificationLevel
verificationBasis OrganizationVerificationBasis
verificationChanged time.Time
tier OrganizationTier
at time.Time
invitationWindow time.Duration
invitationLimit int64
cardViewWindow time.Duration
cardViewLimit int64
}
// NewCompanyQuota resolves the organization's trusted verification against
// an effective product policy. at is the application clock snapshot for the
// whole operation; a request from before the organization or its current
// verification record is rejected so a stale timestamp cannot use older
// trust.
func NewCompanyQuota(organization Organization, version PolicyVersion, at time.Time) (CompanyQuota, error) {
const location = "domain.NewCompanyQuota"
if organization.IsZero() {
return CompanyQuota{}, fmt.Errorf("%s - organization must be restored from trusted storage: %w", location, ErrInvalidCompanyQuota)
}
if version.IsZero() || version.Key() != ProductPolicyKey {
return CompanyQuota{}, fmt.Errorf("%s - an effective product policy is required: %w", location, ErrInvalidCompanyQuota)
}
if at.IsZero() || at.Unix() < 0 {
return CompanyQuota{}, fmt.Errorf("%s - application time must be set and non-negative: %w", location, ErrInvalidCompanyQuota)
}
at = at.UTC()
verification := organization.Verification()
if at.Before(organization.CreatedAt()) || at.Before(verification.ChangedAt()) {
return CompanyQuota{}, fmt.Errorf("%s - application time predates organization trust: %w", location, ErrInvalidCompanyQuota)
}
if version.EffectiveAt().After(at) {
return CompanyQuota{}, fmt.Errorf("%s - policy is not effective at application time: %w", location, ErrInvalidCompanyQuota)
}
tier, ok := verification.Level().QuotaTier()
if !ok {
return CompanyQuota{}, fmt.Errorf("%s - verification level has no quota tier: %w", location, ErrCompanyQuotaDenied)
}
policy := version.Policy().OrganizationQuotas
tierPolicy, err := policy.Tier(tier)
if err != nil {
return CompanyQuota{}, fmt.Errorf("%s - read the organization tier: %w", location, err)
}
if !tierPolicy.Active {
return CompanyQuota{}, fmt.Errorf("%s - organization tier is not active: %w", location, ErrCompanyQuotaDenied)
}
return CompanyQuota{
organizationID: organization.ID(),
policyVersionID: version.ID(),
verificationLevel: verification.Level(),
verificationBasis: verification.Basis(),
verificationChanged: verification.ChangedAt().UTC(),
tier: tier,
at: at,
invitationWindow: policy.InvitationWindow.Duration(),
invitationLimit: int64(tierPolicy.InvitationsPerWindow),
cardViewWindow: policy.CardViewWindow.Duration(),
cardViewLimit: int64(tierPolicy.DistinctCardViewsPerWindow),
}, nil
}
func (quota CompanyQuota) OrganizationID() uuid.UUID { return quota.organizationID }
func (quota CompanyQuota) PolicyVersionID() PolicyVersionID { return quota.policyVersionID }
func (quota CompanyQuota) VerificationLevel() OrganizationVerificationLevel {
return quota.verificationLevel
}
func (quota CompanyQuota) VerificationBasis() OrganizationVerificationBasis {
return quota.verificationBasis
}
func (quota CompanyQuota) VerificationChangedAt() time.Time { return quota.verificationChanged }
func (quota CompanyQuota) Tier() OrganizationTier { return quota.tier }
func (quota CompanyQuota) At() time.Time { return quota.at }
func (quota CompanyQuota) InvitationWindow() time.Duration { return quota.invitationWindow }
func (quota CompanyQuota) InvitationWindowStart() time.Time {
if quota.invitationWindow <= 0 {
return time.Time{}
}
return quota.at.Add(-quota.invitationWindow)
}
func (quota CompanyQuota) InvitationLimit() int64 { return quota.invitationLimit }
func (quota CompanyQuota) CardViewWindow() time.Duration { return quota.cardViewWindow }
func (quota CompanyQuota) CardViewWindowStart() time.Time {
if quota.cardViewWindow <= 0 {
return time.Time{}
}
return quota.at.Add(-quota.cardViewWindow)
}
func (quota CompanyQuota) CardViewLimit() int64 { return quota.cardViewLimit }
// IsZero reports a snapshot that was not built by NewCompanyQuota.
func (quota CompanyQuota) IsZero() bool { return quota.organizationID == uuid.Nil }