Skip to content

refactor(credits): consume the api credits endpoint; delete chat's duplicated stack#1842

Merged
sweetmantech merged 1 commit into
mainfrom
feat/enterprise-pro-credits
Jul 3, 2026
Merged

refactor(credits): consume the api credits endpoint; delete chat's duplicated stack#1842
sweetmantech merged 1 commit into
mainfrom
feat/enterprise-pro-credits

Conversation

@sweetmantech

@sweetmantech sweetmantech commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Rescope decision (owner, 2026-07-03)

This PR originally mirrored the api repo's enterprise-pro credits change into chat's own duplicated credits stack. The owner decided to replace that with a consolidation: chat stops computing credits itself and consumes the api's credits endpoint. Credits logic (monthly reset, pro and enterprise tiers, totals) now lives only in the api repo. The old mirror commit (7bca1cb) was discarded and the branch force-pushed.

Related: #1841 (tracking) and recoupable/api#747 (merged api side: enterprise-domain accounts resolve pro, PRO_CREDITS 9999).

Rewired

  • lib/recoup/getAccountCredits.ts (new): calls GET {API_BASE}/api/accounts/{accountId}/credits with the user's Privy bearer token, following the lib/recoup/fetchPosts.ts and useProStatus patterns. Returns the api's flat shape: account_id, remaining_credits, total_credits, used_credits, is_pro, timestamp.
  • hooks/useCredits.ts: now acquires the Privy access token (like useProStatus) and calls the new helper. Query key, staleTime, and enabled gating unchanged; the data type changed from the credits_usage DB row to the api response (field remaining_credits is common to both).
  • hooks/usePayment.tsx: totalCredits now comes from the api response (total_credits) instead of the local PRO_CREDITS/DEFAULT_CREDITS constants (which were stale: chat said 1000, the api grants 9999 for pro). External contract of usePayment (credits, totalCredits, isSubscribed, isLoading, refetchCredits) is unchanged, so no component changes.

Deleted (dead after consolidation, zero remaining callers)

  • app/api/credits/get/route.ts (chat's own credits endpoint)
  • lib/credits/checkAndResetCredits.ts (reset/refill logic, now api-side)
  • lib/supabase/getCredits.tsx (replaced by lib/recoup/getAccountCredits.ts)
  • lib/supabase/credits_usage/: initializeAccountCredits.ts, selectCreditsUsage.ts, updateCreditsUsage.ts
  • lib/stripe/: getOrgSubscription.ts, getActiveSubscriptionDetails.ts, getActiveSubscriptions.ts, isActiveSubscription.ts, client.tsx (the Stripe SDK client; the surviving stripe helpers call the api over HTTP)
  • DEFAULT_CREDITS / PRO_CREDITS from lib/consts.ts
  • stripe npm dependency (all importers deleted; chat no longer needs STRIPE_SK)

Kept: useProStatus (still the isSubscribed source in usePayment), getAccountOrganizations (used by lib/organizations/formatAccountOrganizations.ts), createClientCheckoutSession / createClientPortalSession (used by useSubscribeClick).

Verification

  • Tests: 23 files, 74 tests passed (includes new lib/recoup/__tests__/getAccountCredits.test.ts, written red-first)
  • pnpm exec tsc --noEmit: 7 errors, all pre-existing in lib/emails/__tests__/extractSendEmailResults.test.ts — zero new
  • eslint on all touched files: clean (next lint is broken repo-wide, pre-existing)
  • pnpm build fails locally on missing env (pre-existing)

🤖 Generated with Claude Code

@vercel

vercel Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

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

Project Deployment Actions Updated (UTC)
chat Ready Ready Preview Jul 3, 2026 7:48pm

Request Review

@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@sweetmantech, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 59 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a72d1257-9d6e-4d80-9371-69d1fd89cc13

📥 Commits

Reviewing files that changed from the base of the PR and between 26c8ff9 and b8cb180.

⛔ Files ignored due to path filters (3)
  • lib/recoup/__tests__/getAccountCredits.test.ts is excluded by !**/*.test.* and included by lib/**
  • package.json is excluded by none and included by none
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml and included by none
📒 Files selected for processing (15)
  • app/api/credits/get/route.ts
  • hooks/useCredits.ts
  • hooks/usePayment.tsx
  • lib/consts.ts
  • lib/credits/checkAndResetCredits.ts
  • lib/recoup/getAccountCredits.ts
  • lib/stripe/client.tsx
  • lib/stripe/getActiveSubscriptionDetails.ts
  • lib/stripe/getActiveSubscriptions.ts
  • lib/stripe/getOrgSubscription.ts
  • lib/stripe/isActiveSubscription.ts
  • lib/supabase/credits_usage/initializeAccountCredits.ts
  • lib/supabase/credits_usage/selectCreditsUsage.ts
  • lib/supabase/credits_usage/updateCreditsUsage.ts
  • lib/supabase/getCredits.tsx
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/enterprise-pro-credits

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1 issue found across 8 files

Confidence score: 3/5

  • In lib/credits/checkAndResetCredits.ts, using Promise.all means an enterprise-email lookup error can reject the whole refill path, so otherwise eligible paid users may not get credits reset/refilled as expected. This creates a concrete user-facing billing/entitlement regression risk—handle lookup failures by degrading enterprise detection to false (or otherwise isolating that failure) before merging.

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread lib/credits/checkAndResetCredits.ts Outdated
…plicated credits stack

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@sweetmantech sweetmantech force-pushed the feat/enterprise-pro-credits branch from 7bca1cb to b8cb180 Compare July 3, 2026 19:44
@sweetmantech sweetmantech changed the title feat(credits): enterprise-domain accounts get pro credits; PRO_CREDITS → 9,999 refactor(credits): consume the api credits endpoint; delete chat's duplicated stack Jul 3, 2026
@sweetmantech

Copy link
Copy Markdown
Collaborator Author

Preview verification — 2026-07-03 (rescoped consolidation)

Preview https://chat-kfylyld1k-recoup.vercel.app, built from PR head b8cb180b. Note: the Vercel build succeeding is itself a key check here — this PR deletes 10 files + the stripe dependency, and the local pnpm build can't run (pre-existing env gap), so the preview build is the compile-time proof no deleted import leaked.

# Check Actual Result
1 Preview build (deletion-heavy diff + removed npm dep) Vercel build succeeded, deployment Ready
2 Chat's own credits route is gone GET /api/credits/get?accountId=…404
3 App renders / → 200
4 The endpoint the sidebar now consumes GET api.recoupable.dev/api/accounts/{id}/credits — live-verified 4/4 earlier today on api#747 (enterprise seeding at 9,999, tier flip, lazy refill, free-tier control)
5 Unit level 23 files / 74 tests green (new fetch-helper test red-first); tsc 7 = pre-existing baseline, 0 new; eslint clean

Not exercised live: the rendered sidebar number (needs a logged-in Privy session on the preview domain). The data path it renders is #4, verified; usePayment's external contract is unchanged so no component-level behavior shifts. A post-merge glance at the prod sidebar for a Seeker account (expect 9,999 total, pro) is the last-mile confirmation.

Once merged, the sidebar tier-mismatch window closes retroactively — chat displays whatever the api computes, including the enterprise-pro 9,999 already live on prod.

@sweetmantech

Copy link
Copy Markdown
Collaborator Author

Full-flow browser verification — 2026-07-03

Follow-up to the earlier table: drove the complete user flow on the preview via browser automation (Chrome DevTools MCP) — Privy email login as sweetmantech@gmail.com (OTP), workspace load, user menu open.

Check Actual Result
Login → sidebar identity block renders credits from the api endpoint GET .../api/accounts/fb678396…/credits with the session's Privy bearer (captured in the network panel), response rendered by the new getAccountCreditsuseCreditsusePayment chain
Displayed value matches the api response exactly 999,996,481 / 9,999 credits + Pro badge (screenshot on file)
No calls to the deleted chat route network panel shows zero requests to /api/credits/get; all credits traffic goes to the api

Environment finding (process, not a PR defect): chat previews point at test-recoup-api.vercel.app — the api repo's test-branch deployment. It had gone stale (3 commits behind main, still serving total_credits: 1000) because test stopped receiving code when PRs moved to main-only. Synced testmain (0550e9fd) and re-verified: sidebar flipped 1,000 → 9,999. Until chat's preview env is repointed (or test gets auto-synced), api merges to main need a test sync for chat previews to see them — tracked on #1841.

@sweetmantech sweetmantech merged commit 8d2f7ad into main Jul 3, 2026
3 checks passed
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