feat(api): migrate POST /api/stripe/portal/create#499
feat(api): migrate POST /api/stripe/portal/create#499arpitgupta1214 wants to merge 2 commits intotestfrom
Conversation
Adds POST /api/subscriptions/portal-sessions — Stripe billing portal
parity for chat's local /api/stripe/portal/create. Derives accountId
from validateAuthContext, looks up the Stripe customer via the local
billing_customers mirror, returns { id, url }. Pairs with the existing
POST /api/subscriptions/sessions for chat's stripe-outbound migration
(group 4).
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Warning Rate limit exceeded
To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (12)
📒 Files selected for processing (7)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 0/1 reviews remaining, refill in 24 minutes and 5 seconds.Comment |
There was a problem hiding this comment.
2 issues found across 11 files
Confidence score: 3/5
- There is a concrete regression risk in
lib/supabase/billing_customers/getStripeCustomerIdByAccountId.ts: returningnullon query errors can mask real Supabase/DB failures as “not linked,” which may lead to incorrect billing flows and harder incident detection. - Given the high confidence (9/10) and moderate-high severity (7/10) of that behavior, this sits at some merge risk until error handling distinguishes operational failures from valid empty results.
- The issue in
app/api/subscriptions/portal-sessions/__tests__/route.post.outcomes.test.tsis low-severity and maintainability-focused (file length/style), so it should not block merge by itself. - Pay close attention to
lib/supabase/billing_customers/getStripeCustomerIdByAccountId.ts,app/api/subscriptions/portal-sessions/__tests__/route.post.outcomes.test.ts- error/result semantics in billing lookup and test maintainability limits.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="lib/supabase/billing_customers/getStripeCustomerIdByAccountId.ts">
<violation number="1" location="lib/supabase/billing_customers/getStripeCustomerIdByAccountId.ts:18">
P1: Do not return `null` on query errors; it conflates failures with "customer not linked" and hides operational errors from callers.</violation>
</file>
<file name="app/api/subscriptions/portal-sessions/__tests__/route.post.outcomes.test.ts">
<violation number="1" location="app/api/subscriptions/portal-sessions/__tests__/route.post.outcomes.test.ts:1">
P3: Custom agent: **Enforce Clear Code Style and Maintainability Practices**
File exceeds the repository’s 100-line limit for maintainability.</violation>
</file>
Architecture diagram
sequenceDiagram
participant Client
participant API as API Handler (Subscriptions)
participant Auth as Auth Service
participant DB as Supabase (billing_customers)
participant Stripe as Stripe API
Note over Client,Stripe: NEW: Create Billing Portal Session Flow
Client->>API: POST /api/subscriptions/portal-sessions
API->>API: NEW: validate JSON body (returnUrl)
API->>Auth: NEW: validateAuthContext()
alt Auth Success
Auth-->>API: return accountId
else Auth Failure
Auth-->>API: 401 Unauthorized
API-->>Client: Error response
end
API->>DB: NEW: getStripeCustomerIdByAccountId(accountId)
alt Customer Found
DB-->>API: return customerId (cus_...)
else Customer Not Found
DB-->>API: null
API-->>Client: 404 No Stripe customer found
end
API->>Stripe: NEW: stripe.billingPortal.sessions.create()
alt Stripe Success
Stripe-->>API: return { id, url }
API-->>Client: 200 OK { id, url }
else Stripe Error / Missing URL
Stripe-->>API: error
API-->>Client: 400/500 Error response
end
Note over API: All responses include getCorsHeaders()
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
|
|
||
| if (error) { | ||
| console.error("Error fetching billing_customers:", error); | ||
| return null; |
There was a problem hiding this comment.
P1: Do not return null on query errors; it conflates failures with "customer not linked" and hides operational errors from callers.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At lib/supabase/billing_customers/getStripeCustomerIdByAccountId.ts, line 18:
<comment>Do not return `null` on query errors; it conflates failures with "customer not linked" and hides operational errors from callers.</comment>
<file context>
@@ -0,0 +1,22 @@
+
+ if (error) {
+ console.error("Error fetching billing_customers:", error);
+ return null;
+ }
+
</file context>
| @@ -0,0 +1,119 @@ | |||
| import "./routeTestMocks"; | |||
There was a problem hiding this comment.
P3: Custom agent: Enforce Clear Code Style and Maintainability Practices
File exceeds the repository’s 100-line limit for maintainability.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At app/api/subscriptions/portal-sessions/__tests__/route.post.outcomes.test.ts, line 1:
<comment>File exceeds the repository’s 100-line limit for maintainability.</comment>
<file context>
@@ -0,0 +1,119 @@
+import "./routeTestMocks";
+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
+import { NextRequest, NextResponse } from "next/server";
</file context>
Per CHAT_API_MIGRATION_FINDINGS REST cleanup section, the dedicated endpoints land at the REST-aligned paths from day one: /api/subscriptions/sessions -> /api/stripe/checkout-sessions /api/subscriptions/portal-sessions -> /api/stripe/portal-sessions Pure path move — no behavior change.
Adds
POST /api/subscriptions/portal-sessions— Stripe billing portal parity for chat's local/api/stripe/portal/create. Pairs with the existingPOST /api/subscriptions/sessionsto complete chat's stripe-outbound migration (group 4 of the chat→api migration plan).Derives
accountIdfromvalidateAuthContext, looks up the Stripe customer via the localbilling_customersmirror, returns{ id, url }.Test plan
pnpm test— 2380/2380 greenpnpm lint:check,pnpm format:checkclean