-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix sendStripePayouts
#2641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix sendStripePayouts
#2641
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git βοΈ
|
|
Warning Rate limit exceeded@steven-tey has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 10 minutes and 56 seconds before requesting another review. β 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. π Files selected for processing (1)
WalkthroughThe changes refactor the payout processing logic in the Stripe payouts API. The Changes
Poem
β¨ Finishing Touches
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. πͺ§ TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
π§Ή Nitpick comments (1)
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts (1)
68-82: Consider using a more explicit type for the Map.While the current implementation works, consider defining a more explicit type for better readability:
- }, new Map<string, typeof currentInvoicePayouts>()); + }, new Map<string, (typeof currentInvoicePayouts)[number][]>());
π Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
π Files selected for processing (2)
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/route.ts(0 hunks)apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts(9 hunks)
π€ Files with no reviewable changes (1)
- apps/web/app/(ee)/api/cron/payouts/charge-succeeded/route.ts
π§° Additional context used
π§ Learnings (2)
π Common learnings
Learnt from: devkiran
PR: dubinc/dub#2635
File: packages/prisma/schema/payout.prisma:24-25
Timestamp: 2025-07-11T16:28:55.654Z
Learning: In the Dub codebase, multiple payout records can now share the same stripeTransferId because payouts are grouped by partner and processed as single Stripe transfers. This is why the @unique constraint was removed from the stripeTransferId field in the Payout model - a single transfer can include multiple payouts for the same partner.
Learnt from: steven-tey
PR: dubinc/dub#0
File: :0-0
Timestamp: 2025-06-25T21:20:59.837Z
Learning: In the Dub codebase, payout limit validation uses a two-stage pattern: server actions perform quick sanity checks (payoutsUsage > payoutsLimit) for immediate user feedback, while the cron job (/cron/payouts) performs authoritative validation (payoutsUsage + payoutAmount > payoutsLimit) with actual calculated amounts before processing. This design provides fast user feedback while ensuring accurate limit enforcement at transaction time.
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts (1)
Learnt from: devkiran
PR: dubinc/dub#2635
File: packages/prisma/schema/payout.prisma:24-25
Timestamp: 2025-07-11T16:28:55.654Z
Learning: In the Dub codebase, multiple payout records can now share the same stripeTransferId because payouts are grouped by partner and processed as single Stripe transfers. This is why the @unique constraint was removed from the stripeTransferId field in the Payout model - a single transfer can include multiple payouts for the same partner.
𧬠Code Graph Analysis (1)
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts (4)
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/utils.ts (1)
Payload(9-9)packages/prisma/client.ts (1)
Prisma(16-16)packages/utils/src/functions/currency-formatter.ts (1)
currencyFormatter(1-11)apps/web/lib/stripe/index.ts (1)
stripe(3-9)
β° Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build
π Additional comments (5)
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts (5)
16-32: Good refactoring for code reusability.The introduction of
commonIncludeusingPrisma.validatorimproves code maintainability and ensures type safety while avoiding repetition.
55-66: Logic correctly implements grouped payout processing.The addition of previously processed payouts query aligns with the codebase design where multiple payouts are grouped by partner and processed as single Stripe transfers.
99-118: Correct handling of minimum withdrawal logic.The implementation properly updates only current invoice payouts to "processed" status while considering the combined total for minimum withdrawal validation.
154-176: Correct batch update of all related payouts.The use of
allPayoutIdsensures both current and previously processed payouts are properly marked as sent with the same transfer ID, which aligns with the grouped payout processing design.
10-13: sendStripePayouts signature removal verified β no callers need updatesA global search for
sendStripePayoutsshows only its definition and a single invocation in
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/route.ts:
- Route.ts:
sendStripePayouts({ payload: body })No other references were found that pass an
invoiceparameter. The functionβs signature change is safe.
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts
Outdated
Show resolved
Hide resolved
|
@CodeRabbit final review |
|
Tip For best results, initiate chat on the files or code changes. π§© Analysis chain@steven-tey Great work on this refactor! After reviewing the changes, here's my final assessment: β Strengths of the Implementation
π Key Improvements Made
π‘ Technical CorrectnessThe refactoring aligns well with the retrieved learning that multiple payout records can share the same The code handles edge cases appropriately:
π― Overall AssessmentThis is a solid refactor that improves code clarity, correctness, and maintainability. The changes properly address the payout grouping requirements while simplifying the function interface. The implementation correctly handles the complex logic of combining multiple payouts per partner while respecting withdrawal thresholds and fee calculations. Status: Ready for merge β The refactoring successfully fixes the π Script executed: Length of output: 8054 π Script executed: Length of output: 37276 π Script executed: Length of output: 182 @steven-tey Excellent refactoring! After thoroughly examining the implementation, here's my comprehensive final review: β Code Quality & ArchitectureFunction Signature Simplification
Code Organization
π― Business Logic ImprovementsEnhanced Payout Grouping // Combines current processing + previously processed payouts per partner
const payoutsByPartner = [
...currentInvoicePayouts,
...previouslyProcessedPayouts,
].reduce((map, payout) => { /* grouping logic */ });Proper Threshold Handling
Accurate Fee Calculation
π§ Technical ExcellenceDatabase Operations
Error Handling & Logging
Stripe Integration
π Alignment with System DesignThe refactoring perfectly implements the retrieved learning that "multiple payout records can share the same stripeTransferId because payouts are grouped by partner and processed as single Stripe transfers." The new logic:
π§ User ExperienceEmail Notifications
π Performance & ScalabilityOptimized Queries
Rate Limiting
π― Final AssessmentThis refactoring represents a significant improvement in:
The implementation correctly handles complex payout scenarios while maintaining clean, readable code. All edge cases are properly addressed, and the database operations are atomic and safe. β APPROVED FOR MERGE This is production-ready code that successfully fixes the π§ Learnings used |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
β»οΈ Duplicate comments (1)
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts (1)
187-188: Use partner-specific payout data for email notification.The email notification uses
currentInvoicePayouts[0]which refers to the first payout globally, not necessarily from the current partner. This could send Partner A an email with Partner B's program/payout data.Use a payout specific to the current partner:
- program: currentInvoicePayouts[0].program, - payout: currentInvoicePayouts[0], + program: payouts[0].program, + payout: payouts.find(p => p.invoiceId === invoiceId) || payouts[0],
π§Ή Nitpick comments (1)
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts (1)
55-82: Solid logic for combining payouts with minor type annotation improvement.The approach to combine current and previously processed payouts aligns well with the grouped transfer strategy.
Consider improving the type annotation:
- }, new Map<string, typeof currentInvoicePayouts>()); + }, new Map<string, (typeof currentInvoicePayouts)[number][]>());This more accurately represents that the map values are arrays of individual payout objects.
π Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
π Files selected for processing (1)
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts(9 hunks)
π§° Additional context used
π§ Learnings (2)
π Common learnings
Learnt from: devkiran
PR: dubinc/dub#2635
File: packages/prisma/schema/payout.prisma:24-25
Timestamp: 2025-07-11T16:28:55.654Z
Learning: In the Dub codebase, multiple payout records can now share the same stripeTransferId because payouts are grouped by partner and processed as single Stripe transfers. This is why the @unique constraint was removed from the stripeTransferId field in the Payout model - a single transfer can include multiple payouts for the same partner.
Learnt from: steven-tey
PR: dubinc/dub#0
File: :0-0
Timestamp: 2025-06-25T21:20:59.837Z
Learning: In the Dub codebase, payout limit validation uses a two-stage pattern: server actions perform quick sanity checks (payoutsUsage > payoutsLimit) for immediate user feedback, while the cron job (/cron/payouts) performs authoritative validation (payoutsUsage + payoutAmount > payoutsLimit) with actual calculated amounts before processing. This design provides fast user feedback while ensuring accurate limit enforcement at transaction time.
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts (3)
Learnt from: devkiran
PR: dubinc/dub#2635
File: packages/prisma/schema/payout.prisma:24-25
Timestamp: 2025-07-11T16:28:55.654Z
Learning: In the Dub codebase, multiple payout records can now share the same stripeTransferId because payouts are grouped by partner and processed as single Stripe transfers. This is why the @unique constraint was removed from the stripeTransferId field in the Payout model - a single transfer can include multiple payouts for the same partner.
Learnt from: devkiran
PR: dubinc/dub#2177
File: apps/web/lib/api/links/bulk-create-links.ts:66-84
Timestamp: 2025-06-06T07:59:03.120Z
Learning: In apps/web/lib/api/links/bulk-create-links.ts, the team accepts the risk of potential undefined results from links.find() operations when building invalidLinks arrays, because existing links are fetched from the database based on the input links, so matches are expected to always exist.
Learnt from: steven-tey
PR: dubinc/dub#0
File: :0-0
Timestamp: 2025-06-25T21:20:59.837Z
Learning: In the Dub codebase, payout limit validation uses a two-stage pattern: server actions perform quick sanity checks (payoutsUsage > payoutsLimit) for immediate user feedback, while the cron job (/cron/payouts) performs authoritative validation (payoutsUsage + payoutAmount > payoutsLimit) with actual calculated amounts before processing. This design provides fast user feedback while ensuring accurate limit enforcement at transaction time.
𧬠Code Graph Analysis (1)
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts (4)
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/utils.ts (1)
Payload(9-9)packages/prisma/client.ts (1)
Prisma(16-16)packages/utils/src/functions/currency-formatter.ts (1)
currencyFormatter(1-11)apps/web/lib/stripe/index.ts (1)
stripe(3-9)
β° Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: build
π Additional comments (4)
apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts (4)
10-10: Good type safety improvement and interface simplification.The Prisma import enables type-safe include validation, and the simplified function signature makes the interface cleaner.
Also applies to: 13-13
16-32: Excellent refactor for type safety and code reuse.Using
Prisma.validatorensures type-safe include objects while eliminating duplication across queries.
34-53: Clean query refactor with improved naming.The use of
commonIncludeensures consistency, and the more descriptive variable name improves readability.
89-153: Robust calculation logic for combined payout processing.The separation of payout IDs, total amount calculation, and withdrawal fee handling correctly implements the grouped transfer approach.
|
@CodeRabbit made a few final changes, one last review please |
|
Tip For best results, initiate chat on the files or code changes. π§© Analysis chainπ Script executed: Length of output: 8497 @steven-tey Perfect! After examining the final state of your changes, I can confirm that everything looks excellent and consistent with my previous comprehensive review. β Final State VerificationCode Structure: All the key improvements from my previous review are intact:
Business Logic: The implementation correctly:
Technical Excellence:
π― Final ApprovalThe codebase is in excellent shape! The refactoring successfully addresses all the original issues while maintaining:
β FINAL APPROVAL - READY FOR MERGE π Your π§ Learnings used |
Summary by CodeRabbit
Refactor
Chores