feat(invitations): accept invitations with C4 consent and contact grants; share the E0 quota across holdings
This commit is contained in:
1 parent
a5990d3fc6
commit
676b3c6e24
59 files changed
+4926
-194
No files matched your search
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
||||
"lincodex/backend/internal/domain"
|
||||
@@ -26,6 +27,20 @@ type companyQuotaRepository struct {
|
||||
|
||||
var _ outbound.CompanyQuotaRepository = companyQuotaRepository{}
|
||||
|
||||
// quotaScope names the usage rows that one reservation counts. An organization
|
||||
// at a tier that shares its holding pool counts the usage of every E0
|
||||
// organization of the holding; any other tier counts its own usage only.
|
||||
type quotaScope struct {
|
||||
organizationID uuid.UUID
|
||||
// holdingID is the holding of the shared pool, and uuid.Nil when the
|
||||
// organization keeps its own pool.
|
||||
holdingID uuid.UUID
|
||||
}
|
||||
|
||||
func (scope quotaScope) sharesHoldingPool() bool {
|
||||
return scope.holdingID != uuid.Nil
|
||||
}
|
||||
|
||||
func (repository companyQuotaRepository) ReserveInvitation(
|
||||
ctx context.Context,
|
||||
quota domain.CompanyQuota,
|
||||
@@ -38,7 +53,8 @@ func (repository companyQuotaRepository) ReserveInvitation(
|
||||
if invitationID == uuid.Nil {
|
||||
return outbound.CompanyQuotaDecision{}, fmt.Errorf("%s - invitation ID must be set: %w", location, domain.ErrInvalidCompanyQuota)
|
||||
}
|
||||
if err := repository.lockAndCheckSnapshot(ctx, location, quota); err != nil {
|
||||
scope, err := repository.lockAndCheckSnapshot(ctx, location, quota)
|
||||
if err != nil {
|
||||
return outbound.CompanyQuotaDecision{}, err
|
||||
}
|
||||
|
||||
@@ -57,18 +73,15 @@ func (repository companyQuotaRepository) ReserveInvitation(
|
||||
return outbound.CompanyQuotaDecision{}, fmt.Errorf("%s - read invitation usage: %w", location, mapOrganizationError(err))
|
||||
}
|
||||
|
||||
usage, err := repository.queries.CountCompanyInvitationQuotaWindow(ctx, sqlc.CountCompanyInvitationQuotaWindowParams{
|
||||
OrganizationID: organizationID,
|
||||
WindowStart: pgTime(quota.InvitationWindowStart()),
|
||||
})
|
||||
count, earliestAt, err := repository.countInvitationUsage(ctx, location, scope, quota.InvitationWindowStart())
|
||||
if err != nil {
|
||||
return outbound.CompanyQuotaDecision{}, fmt.Errorf("%s - count invitation usage: %w", location, mapOrganizationError(err))
|
||||
return outbound.CompanyQuotaDecision{}, err
|
||||
}
|
||||
if usage.UsageCount < 0 {
|
||||
if count < 0 {
|
||||
return outbound.CompanyQuotaDecision{}, fmt.Errorf("%s - invitation usage count is negative", location)
|
||||
}
|
||||
if usage.UsageCount >= quota.InvitationLimit() {
|
||||
earliest, err := quotaWindowEarliest(usage.EarliestAt)
|
||||
if count >= quota.InvitationLimit() {
|
||||
earliest, err := quotaWindowEarliest(earliestAt)
|
||||
if err != nil {
|
||||
return outbound.CompanyQuotaDecision{}, fmt.Errorf("%s - read earliest invitation expiry: %w", location, err)
|
||||
}
|
||||
@@ -92,7 +105,8 @@ func (repository companyQuotaRepository) ReserveCardView(
|
||||
if candidateID == uuid.Nil {
|
||||
return outbound.CompanyQuotaDecision{}, fmt.Errorf("%s - candidate ID must be set: %w", location, domain.ErrInvalidCompanyQuota)
|
||||
}
|
||||
if err := repository.lockAndCheckSnapshot(ctx, location, quota); err != nil {
|
||||
scope, err := repository.lockAndCheckSnapshot(ctx, location, quota)
|
||||
if err != nil {
|
||||
return outbound.CompanyQuotaDecision{}, err
|
||||
}
|
||||
|
||||
@@ -101,7 +115,7 @@ func (repository companyQuotaRepository) ReserveCardView(
|
||||
if err != nil || handled {
|
||||
return decision, err
|
||||
}
|
||||
return repository.reserveNewCardView(ctx, location, quota, candidateID, organizationID)
|
||||
return repository.reserveNewCardView(ctx, location, quota, candidateID, scope)
|
||||
}
|
||||
|
||||
func (repository companyQuotaRepository) reserveExistingCardView(
|
||||
@@ -166,20 +180,17 @@ func (repository companyQuotaRepository) reserveNewCardView(
|
||||
location string,
|
||||
quota domain.CompanyQuota,
|
||||
candidateID uuid.UUID,
|
||||
organizationID uuid.UUID,
|
||||
scope quotaScope,
|
||||
) (outbound.CompanyQuotaDecision, error) {
|
||||
usage, err := repository.queries.CountCompanyCardViewQuotaWindow(ctx, sqlc.CountCompanyCardViewQuotaWindowParams{
|
||||
OrganizationID: pgUUID(organizationID),
|
||||
WindowStart: pgTime(quota.CardViewWindowStart()),
|
||||
})
|
||||
count, earliestAt, err := repository.countCardViewUsage(ctx, location, scope, quota.CardViewWindowStart())
|
||||
if err != nil {
|
||||
return outbound.CompanyQuotaDecision{}, fmt.Errorf("%s - count card-view usage: %w", location, mapOrganizationError(err))
|
||||
return outbound.CompanyQuotaDecision{}, err
|
||||
}
|
||||
if usage.UsageCount < 0 {
|
||||
if count < 0 {
|
||||
return outbound.CompanyQuotaDecision{}, fmt.Errorf("%s - card-view usage count is negative", location)
|
||||
}
|
||||
if usage.UsageCount >= quota.CardViewLimit() {
|
||||
earliest, err := quotaWindowEarliest(usage.EarliestAt)
|
||||
if count >= quota.CardViewLimit() {
|
||||
earliest, err := quotaWindowEarliest(earliestAt)
|
||||
if err != nil {
|
||||
return outbound.CompanyQuotaDecision{}, fmt.Errorf("%s - read earliest card-view expiry: %w", location, err)
|
||||
}
|
||||
@@ -194,30 +205,119 @@ func (repository companyQuotaRepository) reserveNewCardView(
|
||||
return outbound.CompanyQuotaDecision{Allowed: true}, nil
|
||||
}
|
||||
|
||||
// countInvitationUsage returns the invitation usage in the window of scope and
|
||||
// the earliest counted reservation, which sets the retry time of a denial.
|
||||
func (repository companyQuotaRepository) countInvitationUsage(
|
||||
ctx context.Context,
|
||||
location string,
|
||||
scope quotaScope,
|
||||
windowStart time.Time,
|
||||
) (int64, any, error) {
|
||||
if scope.sharesHoldingPool() {
|
||||
usage, err := repository.queries.CountCompanyInvitationQuotaHoldingWindow(ctx, sqlc.CountCompanyInvitationQuotaHoldingWindowParams{
|
||||
HoldingID: pgUUID(scope.holdingID),
|
||||
WindowStart: pgTime(windowStart),
|
||||
})
|
||||
if err != nil {
|
||||
return 0, nil, fmt.Errorf("%s - count invitation usage of the holding pool: %w", location, mapOrganizationError(err))
|
||||
}
|
||||
return usage.UsageCount, usage.EarliestAt, nil
|
||||
}
|
||||
usage, err := repository.queries.CountCompanyInvitationQuotaWindow(ctx, sqlc.CountCompanyInvitationQuotaWindowParams{
|
||||
OrganizationID: pgUUID(scope.organizationID),
|
||||
WindowStart: pgTime(windowStart),
|
||||
})
|
||||
if err != nil {
|
||||
return 0, nil, fmt.Errorf("%s - count invitation usage: %w", location, mapOrganizationError(err))
|
||||
}
|
||||
return usage.UsageCount, usage.EarliestAt, nil
|
||||
}
|
||||
|
||||
// countCardViewUsage is countInvitationUsage for distinct card views.
|
||||
func (repository companyQuotaRepository) countCardViewUsage(
|
||||
ctx context.Context,
|
||||
location string,
|
||||
scope quotaScope,
|
||||
windowStart time.Time,
|
||||
) (int64, any, error) {
|
||||
if scope.sharesHoldingPool() {
|
||||
usage, err := repository.queries.CountCompanyCardViewQuotaHoldingWindow(ctx, sqlc.CountCompanyCardViewQuotaHoldingWindowParams{
|
||||
HoldingID: pgUUID(scope.holdingID),
|
||||
WindowStart: pgTime(windowStart),
|
||||
})
|
||||
if err != nil {
|
||||
return 0, nil, fmt.Errorf("%s - count card-view usage of the holding pool: %w", location, mapOrganizationError(err))
|
||||
}
|
||||
return usage.UsageCount, usage.EarliestAt, nil
|
||||
}
|
||||
usage, err := repository.queries.CountCompanyCardViewQuotaWindow(ctx, sqlc.CountCompanyCardViewQuotaWindowParams{
|
||||
OrganizationID: pgUUID(scope.organizationID),
|
||||
WindowStart: pgTime(windowStart),
|
||||
})
|
||||
if err != nil {
|
||||
return 0, nil, fmt.Errorf("%s - count card-view usage: %w", location, mapOrganizationError(err))
|
||||
}
|
||||
return usage.UsageCount, usage.EarliestAt, nil
|
||||
}
|
||||
|
||||
// LockOrganization takes the organization lock and, for an E0 organization,
|
||||
// the lock of its holding, in that order. Every caller that reserves usage
|
||||
// later takes it first, so the pool is serialized before any other lock.
|
||||
func (repository companyQuotaRepository) LockOrganization(ctx context.Context, organizationID uuid.UUID) error {
|
||||
const location = "companyQuotaRepository.LockOrganization"
|
||||
if _, err := repository.queries.LockCompanyQuotaOrganizationFirst(ctx, pgUUID(organizationID)); err != nil {
|
||||
organization, err := repository.queries.LockCompanyQuotaOrganizationFirst(ctx, pgUUID(organizationID))
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return fmt.Errorf("%s - organization was removed: %w", location, outbound.ErrCompanyQuotaSnapshotChanged)
|
||||
}
|
||||
return fmt.Errorf("%s - lock organization: %w", location, mapOrganizationError(err))
|
||||
}
|
||||
// The level is read under the organization lock, which a trust change
|
||||
// must also take, so the tier cannot change before the holding is locked.
|
||||
if tier, ok := domain.OrganizationVerificationLevel(organization.VerificationLevel).QuotaTier(); ok && tier.SharesHoldingPool() {
|
||||
if _, err := lockQuotaHolding(ctx, repository.queries, location, organization.HoldingID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// lockAndCheckSnapshot takes the organization lock for the reservation, checks
|
||||
// the stored trust and effective policy against the snapshot, and returns the
|
||||
// usage scope. The holding of an E0 organization is locked after the
|
||||
// organization and before the usage is counted.
|
||||
func (repository companyQuotaRepository) lockAndCheckSnapshot(
|
||||
ctx context.Context,
|
||||
location string,
|
||||
quota domain.CompanyQuota,
|
||||
) error {
|
||||
organizationID := pgUUID(quota.OrganizationID())
|
||||
organization, err := repository.queries.LockCompanyQuotaOrganization(ctx, organizationID)
|
||||
) (quotaScope, error) {
|
||||
organization, err := repository.queries.LockCompanyQuotaOrganization(ctx, pgUUID(quota.OrganizationID()))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return fmt.Errorf("%s - organization was removed after quota resolution: %w", location, outbound.ErrCompanyQuotaSnapshotChanged)
|
||||
return quotaScope{}, fmt.Errorf("%s - organization was removed after quota resolution: %w", location, outbound.ErrCompanyQuotaSnapshotChanged)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - lock organization: %w", location, mapOrganizationError(err))
|
||||
return quotaScope{}, fmt.Errorf("%s - lock organization: %w", location, mapOrganizationError(err))
|
||||
}
|
||||
if err := checkStoredTrust(location, quota, organization); err != nil {
|
||||
return quotaScope{}, err
|
||||
}
|
||||
|
||||
scope := quotaScope{organizationID: quota.OrganizationID()}
|
||||
if quota.Tier().SharesHoldingPool() {
|
||||
scope.holdingID, err = lockQuotaHolding(ctx, repository.queries, location, organization.HoldingID)
|
||||
if err != nil {
|
||||
return quotaScope{}, err
|
||||
}
|
||||
}
|
||||
if err := repository.checkEffectivePolicy(ctx, location, quota); err != nil {
|
||||
return quotaScope{}, err
|
||||
}
|
||||
return scope, nil
|
||||
}
|
||||
|
||||
// checkStoredTrust refuses a reservation whose snapshot no longer matches the
|
||||
// verification of the locked organization.
|
||||
func checkStoredTrust(location string, quota domain.CompanyQuota, organization sqlc.LockCompanyQuotaOrganizationRow) error {
|
||||
if !organization.ID.Valid || organization.ID.Bytes != quota.OrganizationID() ||
|
||||
!organization.VerificationChangedAt.Valid ||
|
||||
organization.VerificationLevel != string(quota.VerificationLevel()) ||
|
||||
@@ -225,7 +325,12 @@ func (repository companyQuotaRepository) lockAndCheckSnapshot(
|
||||
!organization.VerificationChangedAt.Time.Equal(quota.VerificationChangedAt()) {
|
||||
return fmt.Errorf("%s - stored organization trust changed: %w", location, outbound.ErrCompanyQuotaSnapshotChanged)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkEffectivePolicy refuses a reservation whose policy version is no longer
|
||||
// the effective product policy at the application clock snapshot.
|
||||
func (repository companyQuotaRepository) checkEffectivePolicy(ctx context.Context, location string, quota domain.CompanyQuota) error {
|
||||
policy, err := repository.queries.GetEffectivePolicyVersion(ctx, sqlc.GetEffectivePolicyVersionParams{
|
||||
PolicyKey: string(domain.ProductPolicyKey),
|
||||
At: pgTime(quota.At()),
|
||||
@@ -242,6 +347,30 @@ func (repository companyQuotaRepository) lockAndCheckSnapshot(
|
||||
return nil
|
||||
}
|
||||
|
||||
// lockQuotaHolding takes the pool lock of an E0 holding. The caller must hold
|
||||
// the lock of the organization that names the holding.
|
||||
func lockQuotaHolding(
|
||||
ctx context.Context,
|
||||
queries *sqlc.Queries,
|
||||
location string,
|
||||
holdingID pgtype.UUID,
|
||||
) (uuid.UUID, error) {
|
||||
if !holdingID.Valid {
|
||||
return uuid.Nil, fmt.Errorf("%s - organization has no holding", location)
|
||||
}
|
||||
locked, err := queries.LockCompanyQuotaHolding(ctx, holdingID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return uuid.Nil, fmt.Errorf("%s - holding was removed: %w", location, outbound.ErrCompanyQuotaSnapshotChanged)
|
||||
}
|
||||
if err != nil {
|
||||
return uuid.Nil, fmt.Errorf("%s - lock holding: %w", location, mapOrganizationError(err))
|
||||
}
|
||||
if !locked.Valid {
|
||||
return uuid.Nil, fmt.Errorf("%s - locked holding has no identifier", location)
|
||||
}
|
||||
return uuid.UUID(locked.Bytes), nil
|
||||
}
|
||||
|
||||
func (repository companyQuotaRepository) cleanupExpiredCardViews(
|
||||
ctx context.Context,
|
||||
location string,
|
||||
|
||||
Reference in new issue
Block a user