fix(copilot): consolidate usage limit validation#1763
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
Contributor
There was a problem hiding this comment.
Greptile Overview
Greptile Summary
Refactored usage limit validation in the Copilot API key validation endpoint to use the centralized checkServerSideUsageLimits helper function.
Key improvements:
- Eliminates duplicate database query logic for usage limits
- Adds
billingBlockedflag checking (previously missing) - Incorporates organization-level pooled usage validation for team/enterprise plans
- Provides better error handling with fail-safe blocking when usage cannot be determined
- Maintains identical API behavior (returns 402 when limits exceeded)
The helper function is already in use across 11 files in the codebase, making this a straightforward consolidation that aligns with established patterns.
Confidence Score: 5/5
- This PR is safe to merge with minimal risk
- The change replaces custom validation logic with a well-established helper function already used in 11 locations across the codebase. The refactor improves correctness by adding billingBlocked checks and organization-level usage validation while maintaining identical API behavior
- No files require special attention
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| apps/sim/app/api/copilot/api-keys/validate/route.ts | 5/5 | Replaced inline usage limit validation with checkServerSideUsageLimits helper, improving consistency and adding organization-level checks |
Sequence Diagram
sequenceDiagram
participant Client
participant Route as /api/copilot/api-keys/validate
participant Helper as checkServerSideUsageLimits
participant DB as Database
participant UsageCheck as checkUsageStatus
Client->>Route: POST {userId}
Route->>Route: Authenticate via checkInternalApiKey
Route->>Helper: checkServerSideUsageLimits(userId)
Helper->>DB: Query userStats for billingBlocked flag
alt billingBlocked is true
Helper-->>Route: {isExceeded: true, message: "Billing issue"}
Route-->>Client: 402 Payment Required
else billingBlocked is false
Helper->>UsageCheck: checkUsageStatus(userId)
UsageCheck->>DB: Query userStats for currentPeriodCost
UsageCheck->>DB: Query organization membership
UsageCheck->>DB: Query organization pooled usage
UsageCheck-->>Helper: {isExceeded, currentUsage, limit}
Helper-->>Route: {isExceeded, currentUsage, limit}
alt isExceeded is true
Route-->>Client: 402 Payment Required
else isExceeded is false
Route-->>Client: 200 OK
end
end
1 file reviewed, no comments
waleedlatif1
pushed a commit
that referenced
this pull request
Nov 12, 2025
* Use helper for api key validation * Lint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Uses helper for usage limit validation
Type of Change
Testing
How has this been tested? What should reviewers focus on?
Checklist