-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Update trigger-withdrawal cron #2494
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
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git βοΈ
|
WalkthroughThe code modifies the process of handling pending payouts by switching from fetching individual payout records to aggregating their total sum directly. It introduces a check to skip withdrawal creation if the available balance is $100 or less, returning a message when this threshold is not met. No exported function signatures were changed. Changes
Sequence Diagram(s)sequenceDiagram
participant User (cron)
participant API Route
participant Database
User (cron)->>API Route: Trigger withdrawal
API Route->>Database: Aggregate sum of pending payouts
Database-->>API Route: Return total sum
API Route->>API Route: Calculate balanceToWithdraw
alt balanceToWithdraw > $100
API Route->>Database: Create payout
Database-->>API Route: Confirm payout creation
API Route-->>User (cron): Return payout creation result
else balanceToWithdraw <= $100
API Route-->>User (cron): Return skip message (insufficient balance)
end
Poem
π Recent review detailsConfiguration used: CodeRabbit UI π Files selected for processing (1)
β° Context from checks skipped due to timeout of 90000ms (2)
π Additional comments (3)
β¨ Finishing Touches
πͺ§ 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 (
|
|
@CodeRabbit final review |
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.
Pull Request Overview
This PR refactors the trigger-withdrawal cron to use a Prisma aggregate for pending payouts, updates reserved balance calculation, and prevents creating tiny withdrawals.
- Replace
findMany+ manual reduce withaggregateto sum pending payouts - Adjust
reservedBalanceby adding the aggregated sum - Add guard to skip payouts when available balance β€ $100
Comments suppressed due to low confidence (1)
apps/web/app/(ee)/api/cron/trigger-withdrawal/route.ts:17
- [nitpick] The name
pendingPayoutssuggests an array, but it now holds the aggregate result. Consider renaming it to something likepayoutsAggregateorprocessingPayoutSumfor clarity.
const [currentBalance, pendingPayouts] = await Promise.all([
| 0, | ||
| ); | ||
|
|
||
| const totalPendingPayouts = pendingPayouts._sum.amount; |
Copilot
AI
Jun 6, 2025
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.
Prisma aggregate may return null for _sum.amount when there are no matching records, which can lead to totalPendingPayouts being null. Consider defaulting to zero: const totalPendingPayouts = pendingPayouts._sum.amount ?? 0;.
| const totalPendingPayouts = pendingPayouts._sum.amount; | |
| const totalPendingPayouts = pendingPayouts._sum.amount ?? 0; |
|
|
||
| if (balanceToWithdraw <= 10000) { | ||
| return NextResponse.json({ | ||
| message: "Balance to withdraw is less than $100, skipping...", |
Copilot
AI
Jun 6, 2025
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.
The code skips when balanceToWithdraw <= 10000 (i.e., β€ $100), but the message says "less than $100." Consider updating to "less than or equal to $100" for accuracy.
| message: "Balance to withdraw is less than $100, skipping...", | |
| message: "Balance to withdraw is less than or equal to $100, skipping...", |
|
Tip For best results, initiate chat on the files or code changes. π§© Analysis chain@steven-tey I'll conduct a final review of this withdrawal cron PR. Let me examine the changes first. π Script executed: Length of output: 2325 π Script executed: Length of output: 83902 @steven-tey Here's my final review of the withdrawal cron changes: β Excellent ImprovementsThe changes in this PR represent solid optimizations to the withdrawal cron functionality: π Performance Optimization// Before: Inefficient - fetches all records then sums in JavaScript
prisma.payout.findMany({
where: { status: "processing" },
})
const totalPendingPayouts = pendingPayouts.reduce((acc, payout) => acc + payout.amount, 0);
// After: Efficient - calculates sum at database level
prisma.payout.aggregate({
where: { status: "processing" },
_sum: { amount: true },
})
const totalPendingPayouts = pendingPayouts._sum.amount;Benefits:
π° Smart Business Logic Additionif (balanceToWithdraw <= 10000) {
return NextResponse.json({
message: "Balance to withdraw is less than $100, skipping...",
});
}Benefits:
π‘οΈ Code Quality & Reliability
π― Overall AssessmentThis is a well-executed optimization that:
The changes are production-ready and represent best practices for database optimization and business logic implementation. Recommendation: β Approved for merge |
Summary by CodeRabbit
New Features
Bug Fixes