Skip to content

Conversation

@icecrasher321
Copy link
Collaborator

@icecrasher321 icecrasher321 commented Nov 21, 2025

Summary

Simplify check for billed overages.

Type of Change

  • Other: Simplification

Testing

Tested manually

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 Nov 21, 2025

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

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs Skipped Skipped Nov 21, 2025 2:16am

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Nov 21, 2025

Greptile Overview

Greptile Summary

Fixed a critical billing bug in getBilledOverageForSubscription for team plans. Previously, the function incorrectly summed billedOverageThisPeriod from all team members, but this field is only stored on the owner's userStats record (as seen in threshold-billing.ts:314). This would cause the system to think more overage was already billed than actually was, potentially leading to under-billing.

Key changes:

  • For team plans, now queries only the owner's billedOverageThisPeriod instead of summing all members
  • Added proper error handling when organization has no owner
  • Simplified code structure and removed unnecessary intermediate variables
  • Added and to imports from drizzle-orm for the compound WHERE clause

Confidence Score: 5/5

  • This PR is safe to merge with high confidence
  • The fix correctly addresses a critical billing bug where billedOverageThisPeriod was incorrectly aggregated from all team members instead of just the owner. The implementation aligns perfectly with how checkAndBillOrganizationOverageThreshold stores this value (only on the owner's stats). The change is well-scoped, includes proper error handling, and follows existing patterns in the codebase.
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
apps/sim/lib/billing/webhooks/invoices.ts 5/5 Fixed critical billing bug where getBilledOverageForSubscription incorrectly summed all team members' billed overages instead of only checking the owner

Sequence Diagram

sequenceDiagram
    participant Webhook as Invoice Webhook
    participant GBO as getBilledOverageForSubscription
    participant DB as Database
    participant Owner as Owner Stats

    Webhook->>GBO: Check billed overage for team
    GBO->>DB: Query owner by role='owner'
    DB-->>GBO: Return owner userId
    
    alt Owner found
        GBO->>Owner: Get billedOverageThisPeriod
        Owner-->>GBO: Return owner's billed amount
        GBO-->>Webhook: Return single owner overage
    else No owner
        GBO->>GBO: Log warning
        GBO-->>Webhook: Return 0
    end
    
    Note over GBO,Owner: Before: incorrectly summed ALL members<br/>After: correctly uses ONLY owner
Loading

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.

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@icecrasher321 icecrasher321 merged commit c6482f2 into staging Nov 24, 2025
9 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/billing-checks branch November 26, 2025 03:53
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