Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

reset usage on transition from free -> paid plan, so if someone goes from free to pro plan, their usage restarts at 0, previously it would stay where it was

Type of Change

  • Bug fix

Testing

N/A

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)

@vercel
Copy link

vercel bot commented Sep 20, 2025

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

Project Deployment Preview Comments Updated (UTC)
sim Ready Ready Preview Comment Sep 22, 2025 8:55pm
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs Skipped Skipped Sep 22, 2025 8:55pm

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 addresses a billing issue where users upgrading from free to paid plans would retain their accumulated usage statistics instead of starting fresh with their new subscription. The change implements a usage reset mechanism that triggers when users transition from free tier to paid plans (pro, team, or enterprise).

The implementation involves three key changes:

  1. Exporting the usage reset function (apps/sim/lib/billing/webhooks/invoices.ts): Makes the existing resetUsageForSubscription function exportable so it can be reused across the billing system. This function moves current period costs to last period costs and sets current usage to 0.

  2. Adding subscription creation handling (apps/sim/lib/auth.ts): Integrates a call to handleSubscriptionCreated in the onSubscriptionComplete hook to trigger usage reset when subscriptions are created through the authentication flow.

  3. Creating the subscription webhook handler (apps/sim/lib/billing/webhooks/subscription.ts): Implements the core logic that detects free-to-paid transitions by checking if users have other active subscriptions. When a user with no previous active subscriptions creates a paid plan subscription, their usage gets reset.

This change integrates with the existing usage monitoring system documented in sim/lib/usage-monitor.ts, which tracks user usage against subscription limits and provides the UsageData interface used throughout the application. The reset ensures that paying customers get the full value of their subscription limits from day one rather than being penalized for free tier usage.

Confidence score: 4/5

  • This PR addresses a legitimate billing fairness issue with a well-structured solution
  • Score reflects solid implementation using existing patterns and proper error handling
  • Pay close attention to the subscription webhook handler logic and database query conditions

3 files reviewed, 1 comment

Edit Code Review Bot Settings | Greptile

status: string
}) {
try {
const otherActiveSubscriptions = await db
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it shouldn't be possible to have multiple active subscriptions tbh -- is this the case rn if I a pro user is added to a team?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or is this something to do with cancelling paid subscriptions?

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.

3 participants