Platform Management

Concurrency-safe quota counters with pooled enforcement, trial resource snapshots, and the non-Connect commission ledger for PayPal and Paymob gateway commissions.

Platform Management covers the operational infrastructure that keeps SCRIPE's multi-tenant platform numerically consistent: quota counters that prevent resource over-provisioning, trial snapshots that enable accurate downgrade enforcement, and the commission ledger that tracks platform revenue from non-Stripe-Connect payment gateways.

What Is Platform Management?

Platform Management is the collection of domain entities responsible for enforcing tenant resource limits (quotas), capturing resource state at trial start, and tracking platform commissions from PayPal and Paymob gateway payments. These components work together to ensure billing integrity and fair resource allocation across the multi-tenant hierarchy.

QuotaCounter Entity

QuotaCounter tracks resource usage per tenant with optional pooled enforcement. One row exists per tenant per resource type (admin, role, subtenant, usergroup). The reservation pattern prevents race conditions under concurrent creation requests.

FieldTypeDescription
TenantIdGuidThe tenant this counter belongs to.
ResourceTypestring (max 50)Resource being tracked: "admin", "role", "subtenant", "usergroup".
UsedintNumber of confirmed resources (actual count after commit).
ReservedintNumber of in-flight reservations (being created but not yet committed).
MaxintMaximum allowed for this tenant. -1 = unlimited.
PoolRootTenantIdGuid?Links to the root tenant of a pooled quota. Null = no pooling (per-tenant limit only). When set, the SUM of (Used + Reserved) across all counters sharing this root must not exceed PoolMax.
PoolMaxintMaximum for the entire pool. Only meaningful on the root counter row. -1 = unlimited.

Atomic Reservation Pattern

The reservation pattern is a three-phase protocol that prevents quota over-provisioning even under high concurrency.

TryReserveSlotAsync — increment Reserved
Create resource (e.g. new Admin)
success
Commit — increment Used, decrement Reserved
Release on failure — decrement Reserved

Quota Enforcement Modes

QuotaCounter supports two enforcement modes controlled by PoolRootTenantId. Per-tenant enforcement is the default; pooled enforcement enables resource sharing across a tenant hierarchy (e.g. a parent tenant that allocates admins across its sub-tenants).

ModeEnforcement
Per-Tenant (PoolRootTenantId = null)Each tenant is enforced independently: (Used + Reserved) < Max.
Pooled (PoolRootTenantId set)SUM(Used + Reserved) across all rows sharing the same PoolRootTenantId must not exceed PoolMax (stored on the root's counter row).

TrialSnapshot Entity

A TrialSnapshot captures the resource counts (admin, role, sub-tenant, user-group) at the exact moment a trial subscription begins. At trial expiry, the system compares current counts against the snapshot to determine if the tenant provisioned resources beyond the base edition's limits during the trial period.

FieldTypeDescription
SubscriptionIdGuidFK to the TenantSubscription this snapshot belongs to.
TenantIdGuidThe tenant this snapshot is for.
AdminCountintNumber of admins at trial start.
RoleCountintNumber of roles at trial start.
SubTenantCountintNumber of sub-tenants at trial start.
UserGroupCountintNumber of user groups at trial start.
CapturedAtDateTimeUTC timestamp when the snapshot was captured.

CommissionLedgerEntry Entity

CommissionLedgerEntry records commissions from non-Connect gateway payments (PayPal, Paymob). For Stripe Connect payments, commissions are collected instantly via application_fee_amount — this entity is only for post-billing gateway flows that require deferred commission collection.

FieldTypeDescription
TenantIdGuidThe tenant whose user made the payment.
UserSubscriptionIdGuidThe UserSubscription that generated this commission.
PaymentTransactionIdGuid?FK to the PaymentTransaction for this user payment.
CommissionInvoiceIdGuid?FK to CommissionInvoice once invoiced. Null while Unbilled.
GatewayPaymentGatewayTypeWhich gateway processed the payment (PayPal or Paymob). Not used for Stripe Connect — those use application_fee_amount directly.
GatewayTransactionIdstring (max 255)Gateway-specific transaction ID for reconciliation and deduplication.
GrossAmountdecimal (18,2)Full amount paid by the user (arrived in the tenant's account).
CommissionRatedecimal (5,4)Commission rate applied at the time of payment (e.g. 0.10 = 10%).
CommissionAmountdecimal (18,2)Platform commission = GrossAmount × CommissionRate.
CurrencyCurrency enumISO 4217 currency code. Should match the tenant's billing currency.
StatusCommissionLedgerStatusLifecycle: Unbilled → Invoiced → Paid / Waived / WriteOff.
Notesstring? (max 1000)Admin notes — used when waiving or writing off a commission.

Revenue Analytics Integration

The commission ledger feeds directly into the Revenue Analytics module for platform-wide financial reporting.

MetricSource
Total commission collectedSUM(CommissionLedgerEntry.CommissionAmount) WHERE Status = Paid
Unbilled commission exposureSUM(CommissionLedgerEntry.CommissionAmount) WHERE Status = Unbilled
Tenant payout totalsTenantStripeAccount.TotalPayoutsAmount
Trial resource usageTrialSnapshot compared to current QuotaCounter.Used at expiry

API Endpoints

Platform management endpoints are restricted to super-admin roles. Quota data is read-only for standard platform admins.

MethodEndpointDescriptionAuth
GET/api/v1/platform/quotasList all quota counters (filterable by tenant, resource type)🔒 Required
GET/api/v1/platform/quotas/{tenantId}/{resourceType}Get the quota counter for a specific tenant and resource type🔒 Required
POST/api/v1/platform/quotas/resetReset a quota counter to zero or a specified baseline🔒 Required
GET/api/v1/platform/trial-snapshots/{subscriptionId}Get the trial resource snapshot captured at trial start🔒 Required
GET/api/v1/platform/commission-ledgerList commission ledger entries (filterable by tenant, gateway, status)🔒 Required
PUT/api/v1/platform/commission-ledger/{id}/waiveWaive a commission ledger entry with an admin note🔒 Required
GET/api/v1/platform/dashboardGet platform management dashboard summary metrics🔒 Required