Skip to content

Conversation

@Sg312
Copy link
Contributor

@Sg312 Sg312 commented Oct 29, 2025

Summary

Uses helper for usage limit validation

Type of Change

  • Bug fix

Testing

How has this been tested? What should reviewers focus on?

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 Oct 29, 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 Oct 29, 2025 7:04pm

@Sg312 Sg312 changed the base branch from main to staging October 29, 2025 19:04
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 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 billingBlocked flag 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
Loading

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@Sg312 Sg312 merged commit b95ea91 into staging Oct 29, 2025
13 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/usage-limit-validation-route branch October 30, 2025 06:21
waleedlatif1 pushed a commit that referenced this pull request Nov 12, 2025
* Use helper for api key validation

* Lint
@waleedlatif1 waleedlatif1 mentioned this pull request Nov 12, 2025
10 tasks
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