Skip to content

feat(builder): show plan and usage quotas in sidebar (cloud-only) - #609

Merged
realcodesiman merged 6 commits into
mainfrom
feat/workspace-quota-usage-display
Jun 23, 2026
Merged

realcodesiman merged 6 commits into
mainfrom
feat/workspace-quota-usage-display

Conversation

@realcodesiman

Copy link
Copy Markdown
Contributor

Summary

Surfaces plan and usage quotas in the builder UI, gated to the hosted cloud edition. Self-hosted community/enterprise installs keep every feature free — no quota gating, no upgrade prompts.

  • NavUsage (new sidebar component) renders per-metric progress bars for monthly active contacts, workspaces, channels, team members. Only metrics that actually have a numeric limit are shown; nothing renders on the free tier or when the sidebar is collapsed to icons.
  • NavUser dropdown now shows the current plan label and an "Upgrade plan" link to /pricing — only when isCloud().
  • Workspaces page shows a used / limit counter next to the heading, highlighted in text-destructive when the limit is reached.
  • Layout resolves quota via userQuotaService.getForUser only when isCloud(); otherwise skips the call entirely (Promise.resolve(null)).
  • UserQuota schema gains planName / planStatus text columns (written by the enterprise billing layer's publishEntitlements; null = free tier / not yet synced), with the accompanying Drizzle migration.
  • i18n keys added for billing.plan.*, billing.usage.*, and actions.upgradePlan in en and vi.

Notes

  • Quota gating is read-only here — usage bars and the limit counter display state; enforcement (blocking actions at the limit) is intentionally out of scope and handled separately.
  • The new planName / planStatus columns require running the migration: pnpm --filter @chatbotx.io/database db:migrate.

Test plan

  • Cloud mode: sidebar shows usage bars for metrics with limits; "Upgrade plan" link points to /pricing; plan label reflects planName (or "Free").
  • Self-hosted (community/enterprise): no usage bars, no plan label, no upgrade link.
  • Workspaces page shows used / limit; counter turns red at the limit; hidden when no limit set.
  • Collapsed sidebar hides usage bars.
  • Migration applies cleanly; planName/planStatus default to null.
  • pnpm lint and pnpm --filter builder check-types pass.

columns: { tenantId: true },
})

let tenantId = ROOT_TENANT_ID
Add quota/usage display to the builder, gated to the hosted cloud edition
(self-hosted community/enterprise installs keep every feature free):

- NavUsage sidebar component renders per-metric progress bars (contacts,
  workspaces, channels, team members), only for metrics that have a limit.
- NavUser dropdown shows the current plan label and an "Upgrade plan" link
  to /pricing when running in cloud mode.
- Workspaces page shows a "used / limit" counter, highlighting when the
  workspace limit is reached.
- Layout resolves quota via userQuotaService only when isCloud().
- Add planName / planStatus columns to UserQuota (written by the enterprise
  billing layer; null = free tier) with the accompanying Drizzle migration.
- Add billing.plan / billing.usage / actions.upgradePlan i18n strings (en, vi).
Extend the cloud quota system from per-user limits to a dual-level
(user + reseller pool) model, add trial visibility, and wire default-plan
provisioning through the private billing portal. Cloud-only — self-hosted
community/enterprise installs keep every feature free.

Enforcement
- New quotaEnforcementService gates resource creation against BOTH the
  user's own UserQuota and the pooled tenant (reseller) limit. All call
  sites (contact, inbox, workspace, invitation, received-message, contact
  import) migrate from userQuotaService.* to it.
- New tenantQuotaService + TenantQuotaUsage table track pooled per-tenant
  usage (Redis live counter + DB high-water-mark reconciliation, mirroring
  UserQuotaService), with a syncTenantQuota scheduled job.
- userQuotaService gains hasCapacity/consume/getLimit/metricValues and
  clearWhiteLabelEntitlements; tenantService gains suspend/reactivate/
  downgrade for white-label lifecycle.

Billing & trials
- onUserCreated auth hook (packages/auth) provisions a new user's default
  plan via billingService, reached only through the builder's same-origin
  /portal/* micro-frontend proxy — no portal URL or secret in this repo.
- planName/planStatus columns on UserQuota; TrialBanner + quota-metrics
  derive trial state and escalate info -> warning -> expired.

UI
- NavUsage/UsageBars sidebar bars, redesigned WorkspacesList + AccountRail,
  iframe UpgradePlanDialog, and an alert "warning" variant.

Review fixes
- Remove debug console.log from quotaEnforcementService.
- Lock pooled contact-import on the tenant-pool key (resolveQuotaLockKey)
  so two sub-accounts can't both overrun the shared pool.
- Serialize the non-pooled tryConsume path under a per-user lock.
- Gate getAtLimitMap behind isCloud() on the homepage.
- TrialBanner waits for hydration before hiding a dismissed banner (no flash).
…rlay

- Gate workspace access on cloud once a trial is consumed/expired, redirecting
  to a new /trial-expired page; derive access state from the already-fetched
  quota to avoid a redundant query.
- Add UserQuotaService.getAccessStateFromQuota plus default-plan entitlement
  overlay so free-tier users inherit published platform limits.
- Show trial days-left banner in the sidebar usage nav.
- Enqueue publishEntitlements on the cross-repo quota queue at sign-up instead
  of importing the billing service directly.
…t reconciliation

Add Redis-backed live usage counters (workspaces, channels, team members,
contacts, mac) in a shared quota counter store that fails closed on corrupt
values, with monthly-active-contact (MAC) tracking wired through the analytics
repository/service and the sync-user-quota schedule.

Provision and suspend reseller tenants via a scheduled reconcile-tenants
safety net behind upgrade-time provisioning, with an enterprise
reconcile-tenant-entitlement action and tenant-unique-owner-id migration.

Replace the trial banner with a usage-ring UI in the sidebar/account rail and
centralize trial messaging.
The usage display read `used` from the DB columns synced by the
`sync-user-quota` job (default every 60s) plus a 60s row cache, so a
metric like `macUsed` could lag real activity by up to ~120s even though
the fresh value already sat in the Redis live counter that enforcement
reads.

Add a batched `getLiveCounts` to the shared LiveCounterStore (one HMGET
for all metrics, single-fetch cold-seeding of missing/corrupt fields,
fail-closed to the DB on Redis error) and expose `getLiveUsage` on the
user and tenant quota services. `getUsageSummary` now sources `used` from
the live counters (limit still from the quota row), and `atLimitForMetric`
uses the live `isLimitReached` so the badge matches the gates and display.
Declare the backfillDefaultPlan action and its job-data variant on the
quota queue. Enqueued by the enterprise admin after a default-plan change
to reconcile every existing user's entitlement snapshot (existing users
hold a UserQuota row, so the published default-plan snapshot alone never
reaches them); consumed by the enterprise quota-worker's backfill loop.
@realcodesiman
realcodesiman force-pushed the feat/workspace-quota-usage-display branch from 97f79e0 to a21a0c8 Compare June 23, 2026 23:33
@realcodesiman
realcodesiman merged commit 7d4668c into main Jun 23, 2026
8 checks passed
@realcodesiman
realcodesiman deleted the feat/workspace-quota-usage-display branch June 23, 2026 23:40
viethung0o0 pushed a commit that referenced this pull request Sep 7, 2026
* feat(builder): show plan and usage quotas in sidebar (cloud-only)

Add quota/usage display to the builder, gated to the hosted cloud edition
(self-hosted community/enterprise installs keep every feature free):

- NavUsage sidebar component renders per-metric progress bars (contacts,
  workspaces, channels, team members), only for metrics that have a limit.
- NavUser dropdown shows the current plan label and an "Upgrade plan" link
  to /pricing when running in cloud mode.
- Workspaces page shows a "used / limit" counter, highlighting when the
  workspace limit is reached.
- Layout resolves quota via userQuotaService only when isCloud().
- Add planName / planStatus columns to UserQuota (written by the enterprise
  billing layer; null = free tier) with the accompanying Drizzle migration.
- Add billing.plan / billing.usage / actions.upgradePlan i18n strings (en, vi).

* feat(business): two-level quota enforcement, trials, and billing seam

Extend the cloud quota system from per-user limits to a dual-level
(user + reseller pool) model, add trial visibility, and wire default-plan
provisioning through the private billing portal. Cloud-only — self-hosted
community/enterprise installs keep every feature free.

Enforcement
- New quotaEnforcementService gates resource creation against BOTH the
  user's own UserQuota and the pooled tenant (reseller) limit. All call
  sites (contact, inbox, workspace, invitation, received-message, contact
  import) migrate from userQuotaService.* to it.
- New tenantQuotaService + TenantQuotaUsage table track pooled per-tenant
  usage (Redis live counter + DB high-water-mark reconciliation, mirroring
  UserQuotaService), with a syncTenantQuota scheduled job.
- userQuotaService gains hasCapacity/consume/getLimit/metricValues and
  clearWhiteLabelEntitlements; tenantService gains suspend/reactivate/
  downgrade for white-label lifecycle.

Billing & trials
- onUserCreated auth hook (packages/auth) provisions a new user's default
  plan via billingService, reached only through the builder's same-origin
  /portal/* micro-frontend proxy — no portal URL or secret in this repo.
- planName/planStatus columns on UserQuota; TrialBanner + quota-metrics
  derive trial state and escalate info -> warning -> expired.

UI
- NavUsage/UsageBars sidebar bars, redesigned WorkspacesList + AccountRail,
  iframe UpgradePlanDialog, and an alert "warning" variant.

Review fixes
- Remove debug console.log from quotaEnforcementService.
- Lock pooled contact-import on the tenant-pool key (resolveQuotaLockKey)
  so two sub-accounts can't both overrun the shared pool.
- Serialize the non-pooled tryConsume path under a per-user lock.
- Gate getAtLimitMap behind isCloud() on the homepage.
- TrialBanner waits for hydration before hiding a dismissed banner (no flash).

* feat(builder): self-managed trial gate and default-plan free-tier overlay

- Gate workspace access on cloud once a trial is consumed/expired, redirecting
  to a new /trial-expired page; derive access state from the already-fetched
  quota to avoid a redundant query.
- Add UserQuotaService.getAccessStateFromQuota plus default-plan entitlement
  overlay so free-tier users inherit published platform limits.
- Show trial days-left banner in the sidebar usage nav.
- Enqueue publishEntitlements on the cross-repo quota queue at sign-up instead
  of importing the billing service directly.

* feat(quota): live usage counters, MAC tracking, and tenant entitlement reconciliation

Add Redis-backed live usage counters (workspaces, channels, team members,
contacts, mac) in a shared quota counter store that fails closed on corrupt
values, with monthly-active-contact (MAC) tracking wired through the analytics
repository/service and the sync-user-quota schedule.

Provision and suspend reseller tenants via a scheduled reconcile-tenants
safety net behind upgrade-time provisioning, with an enterprise
reconcile-tenant-entitlement action and tenant-unique-owner-id migration.

Replace the trial banner with a usage-ring UI in the sidebar/account rail and
centralize trial messaging.

* feat(business): serve quota usage display from live counters

The usage display read `used` from the DB columns synced by the
`sync-user-quota` job (default every 60s) plus a 60s row cache, so a
metric like `macUsed` could lag real activity by up to ~120s even though
the fresh value already sat in the Redis live counter that enforcement
reads.

Add a batched `getLiveCounts` to the shared LiveCounterStore (one HMGET
for all metrics, single-fetch cold-seeding of missing/corrupt fields,
fail-closed to the DB on Redis error) and expose `getLiveUsage` on the
user and tenant quota services. `getUsageSummary` now sources `used` from
the live counters (limit still from the quota row), and `atLimitForMetric`
uses the live `isLimitReached` so the badge matches the gates and display.

* feat(worker-config): add backfillDefaultPlan quota job action

Declare the backfillDefaultPlan action and its job-data variant on the
quota queue. Enqueued by the enterprise admin after a default-plan change
to reconcile every existing user's entitlement snapshot (existing users
hold a UserQuota row, so the published default-plan snapshot alone never
reaches them); consumed by the enterprise quota-worker's backfill loop.

This branch was previously deployed

1 inactive deployment
dev — a21a0c8b Deployed Jun 23, 2026 by realcodesiman via Build realtime #1570
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant