Skip to content

Conversation

icecrasher321
Copy link
Collaborator

@icecrasher321 icecrasher321 commented Aug 25, 2025

Summary

Fix billing system. Consolidate code into better auth hooks. Have two invoices -- one for overage and one for base bill.

Type of Change

  • Bug fix

Testing

Fixed manually using Stripe Sandbox Client.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

Copy link

vercel bot commented Aug 25, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
sim Ready Ready Preview Comment Aug 29, 2025 0:04am
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs Skipped Skipped Aug 29, 2025 0:04am

Copy link
Contributor

@greptile-apps greptile-apps bot left a 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

Edit Code Review Bot Settings | Greptile

@vercel vercel bot temporarily deployed to Preview – docs August 25, 2025 22:35 Inactive
@vercel vercel bot temporarily deployed to Preview – docs August 25, 2025 22:42 Inactive
@icecrasher321 icecrasher321 changed the title fix(billing): team usage tracking cleanup, shared pool of limits for team fix(billing): team usage tracking cleanup, shared pool of limits for team/enterprise Aug 25, 2025
@vercel vercel bot temporarily deployed to Preview – docs August 28, 2025 23:47 Inactive
@vercel vercel bot temporarily deployed to Preview – docs August 28, 2025 23:55 Inactive
@icecrasher321 icecrasher321 merged commit 56543da into staging Aug 29, 2025
4 of 5 checks passed
waleedlatif1 added a commit that referenced this pull request Aug 29, 2025
…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>
@waleedlatif1 waleedlatif1 deleted the fix/billing-cleanup branch August 29, 2025 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants