Skip to content

fix(subscription-state): remove dead code, change token route check#4062

Merged
icecrasher321 merged 4 commits intostagingfrom
fix/billing
Apr 9, 2026
Merged

fix(subscription-state): remove dead code, change token route check#4062
icecrasher321 merged 4 commits intostagingfrom
fix/billing

Conversation

@icecrasher321
Copy link
Copy Markdown
Collaborator

Summary

Cleanup dead helpers for subscription state.

Type of Change

  • Bug fix
  • Other: Code quality improvement

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
Copy Markdown

vercel bot commented Apr 8, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Apr 8, 2026 11:54pm

Request Review

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 8, 2026

PR Summary

Medium Risk
Touches billing/usage-enforcement paths for speech-to-text token issuance and feature gating; mistakes could incorrectly block access or fail to enforce limits.

Overview
Updates the /api/speech/token endpoint to enforce usage limits via checkServerSideUsageLimits (returning a 402 with an optional server-provided message) instead of the old cost-limit helper.

Adds client handling for 402 responses in useSpeechToText and wires UserInput to redirect users to subscription settings when voice usage is exceeded.

Removes dead subscription-state/cost-limit helpers and the UserSubscriptionState type, and refactors copilot tool description logic and table plan-limit selection to rely on getHighestPrioritySubscription plus plan helpers (isPaid, getPlanTypeForLimits), updating tests accordingly.

Reviewed by Cursor Bugbot for commit 5b9a5c5. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 8, 2026

Greptile Summary

This PR removes dead helper code from the subscription state layer and adjusts the billing check in the speech token route (billingUserId-guarded usage check). The billing barrel (lib/billing/index.ts) and types file are trimmed accordingly. All remaining findings are P2.

Confidence Score: 5/5

Safe to merge — all findings are P2 style/type-cleanliness issues that don't affect runtime behaviour.

The dead-code removal is clean, the token route auth flow is correct, and the updated imports in chat-payload are properly tested. Only P2 issues remain: redundant logger re-mock in the test, and any types that were already present in the codebase and not introduced by this PR.

No files require special attention.

Vulnerabilities

No security concerns identified. Auth flows in the speech token route are unchanged — session and chat-cookie validation still guard the endpoint before any billing or ElevenLabs interaction.

Important Files Changed

Filename Overview
apps/sim/app/api/speech/token/route.ts Token route now guards usage-limit check behind billingUserId; logic and auth flow look correct.
apps/sim/lib/billing/core/subscription.ts Dead code removed; sendPlanWelcomeEmail still uses any for the subscription parameter, violating the project's no-any rule.
apps/sim/lib/billing/index.ts Barrel updated to remove deleted re-exports; explicit named exports alongside export * from the same module is valid TS (explicit takes precedence).
apps/sim/lib/billing/types/index.ts Types file cleaned up; still contains any in BillingEntity.metadata, SubscriptionAPIResponse.metadata, and BillingError.details.
apps/sim/lib/copilot/chat-payload.ts Now imports getHighestPrioritySubscription directly from the core subscription module; subscription-error path correctly defaults shouldAppendEmailTagline to false.
apps/sim/lib/copilot/chat-payload.test.ts New tests for subscription-path; unnecessarily re-mocks @sim/logger which is already globally mocked in vitest.setup.ts.
apps/sim/lib/table/billing.ts No functional changes; continues to use getHighestPrioritySubscription correctly via the core module.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[POST /api/speech/token] --> B{chatId present?}
    B -- Yes --> C[validateChatAuth]
    C -- invalid --> D[401 Unauthorized]
    C -- valid --> E[billingUserId = chat.ownerId]
    B -- No --> F[getSession]
    F -- no session --> D
    F -- valid --> G[billingUserId = session.user.id]
    E --> H{isBillingEnabled?}
    G --> H
    H -- Yes --> I[checkRateLimitDirect]
    I -- exceeded --> J[429 Rate Limit]
    I -- ok --> K{billingUserId set?}
    H -- No --> K
    K -- Yes --> L[checkServerSideUsageLimits]
    L -- exceeded --> M[402 Limit Exceeded]
    L -- ok --> N[Fetch ElevenLabs token]
    K -- No --> N
    N -- error --> O[502 Bad Gateway]
    N -- ok --> P[recordUsage async]
    P --> Q[200 token response]
Loading

Comments Outside Diff (2)

  1. apps/sim/lib/billing/core/subscription.ts, line 486 (link)

    P2 any parameter type

    subscription: any violates the project's no-any TypeScript rule. Use the schema type from @sim/db (e.g. the inferred Subscription row type) or at minimum unknown with a type guard.

  2. apps/sim/lib/billing/types/index.ts, line 88 (link)

    P2 any in type definitions

    The open-ended index signature here and in SubscriptionAPIResponse.metadata / BillingError.details violates the project no-any rule. Swap all three catch-all fields to unknown so callers must narrow the type before use.

Reviews (1): Last reviewed commit: "fix(subscription-state): remove dead cod..." | Re-trigger Greptile

@icecrasher321
Copy link
Copy Markdown
Collaborator Author

bugbot run

@icecrasher321
Copy link
Copy Markdown
Collaborator Author

bugbot run

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

1 issue from previous review remains unresolved.

Fix All in Cursor

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 5b9a5c5. Configure here.

@icecrasher321 icecrasher321 merged commit c5cc336 into staging Apr 9, 2026
12 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/billing branch April 9, 2026 07:09
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.

1 participant