-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(billing): usage tracking cleanup, shared pool of limits for team/enterprise #1131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Summary
This PR implements a fundamental architectural shift in the billing system from individual user-based usage limits to organization-level shared pool limits for team subscriptions. The changes replace the complex per-member usage tracking model with a unified team approach where all members draw from a collective usage allowance.
Key changes include:
Database Schema Updates: Added org_usage_limit
column to the organization table and removed user-level billing period fields (billing_period_start
, billing_period_end
, usage_limit_set_by
) from the user_stats table. This consolidates usage limit management at the organization level.
Billing Constants Centralization: Introduced centralized pricing constants (DEFAULT_PRO_TIER_COST_LIMIT = $20
, DEFAULT_TEAM_TIER_COST_LIMIT = $40
, DEFAULT_ENTERPRISE_TIER_COST_LIMIT = $200
) replacing hardcoded values throughout the codebase.
Core Billing Logic Refactor: Replaced calculateDefaultUsageLimit
with getSubscriptionAllowance
and added getPerUserMinimumLimit
which returns 0 for team plans, enabling shared pool behavior. The billing system now uses licensed seats from Stripe subscriptions as the source of truth rather than actual member count.
UI Component Updates: Added new OrganizationUsageCap
component for managing team-wide usage limits and removed individual member limit management (MemberLimit
component). The team usage display now shows "Pool Contribution" instead of individual limits, and member invitations are disabled when no seats are available.
API Endpoint Changes: Modified the usage limits API to handle 'organization' context instead of 'member' context, implementing organization-level permission checks and limit updates through updateOrganizationUsageLimit
.
Usage Monitoring Enhancements: Extended the usage monitor to check organization-level pooled usage in addition to individual user limits, with fallback logic to derive usage caps from Stripe subscription pricing when not explicitly set.
This refactor simplifies team billing management by removing the administrative overhead of per-member limit setting while providing better cost control and aligning with standard SaaS team billing practices where organizations purchase capacity that is shared among team members.
PR Description Notes:
- The PR is marked "IN PROGRESS" and lacks specific issue reference
- All checklist items remain unchecked
- No testing information provided
- Missing screenshots/videos for UI changes
Confidence score: 2/5
- This PR requires very careful review due to significant architectural changes in critical billing systems
- Score lowered due to complex database schema changes, performance issues in usage monitoring, and incomplete PR documentation
- Pay special attention to
apps/sim/lib/billing/calculations/usage-monitor.ts
which has N+1 query problems and silent error handling
24 files reviewed, 4 comments
...odal/components/team-management/components/organization-usage-cap/organization-usage-cap.tsx
Outdated
Show resolved
Hide resolved
…ulation of data and consolidation
…enterprise (#1131) * fix(billing): team usage tracking cleanup, shared pool of limits for team * address greptile commments * fix lint * remove usage of deprecated cols" * update periodStart and periodEnd correctly * fix lint * fix type issue * fix(billing): cleaned up billing, still more work to do on UI and population of data and consolidation * fix upgrade * cleanup * progress * works * Remove 78th migration to prepare for merge with staging * fix migration conflict * remove useless test file * fix * Fix undefined seat pricing display and handle cancelled subscription seat updates * cleanup code * cleanup to use helpers for pulling pricing limits * cleanup more things * cleanup * restore environment ts file * remove unused files * fix(team-management): fix team management UI, consolidate components * use session data instead of subscription data in settings navigation * remove unused code * fix UI for enterprise plans * added enterprise plan support * progress * billing state machine * split overage and base into separate invoices * fix badge logic --------- Co-authored-by: waleedlatif1 <walif6@gmail.com>
Summary
Fix billing system. Consolidate code into better auth hooks. Have two invoices -- one for overage and one for base bill.
Type of Change
Testing
Fixed manually using Stripe Sandbox Client.
Checklist