134 lines
5.0 KiB
Go
134 lines
5.0 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")
|
|
)
|
|
|
|
// 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.
|
|
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 }
|