The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
simplify back to payoutsToBeSent logic
Steven Teya65de6137a8b
diff --git a/apps/web/app/(ee)/api/cron/trigger-withdrawal/route.ts b/apps/web/app/(ee)/api/cron/trigger-withdrawal/route.tsindex 66119c6f37..5362ea113a 100644--- a/apps/web/app/(ee)/api/cron/trigger-withdrawal/route.ts+++ b/apps/web/app/(ee)/api/cron/trigger-withdrawal/route.ts@@ -15,51 +15,13 @@ export async function GET(req: Request) {try {await verifyVercelSignature(req);- const THREE_DAYS_AGO = new Date(- new Date().setDate(new Date().getDate() - 3),- );- const ONE_DAY_AGO = new Date(new Date().setDate(new Date().getDate() - 1));-- const [- stripeBalanceData,- processingInvoicesDirectDebit,- processingInvoicesDirectDebitFast,- processedPayouts,- ] = await Promise.all([+ const [stripeBalanceData, payoutsToBeSentData] = await Promise.all([stripe.balance.retrieve(),- prisma.invoice.aggregate({- where: {- status: "processing",- paymentMethod: {- in: ["ach", "sepa", "acss"],- },- createdAt: {- // Why we're doing this: Direct debit payments usually take up to 4 business days to settle- // but in case it settles earlier, we'd want to keep the balance on Stripe for payouts- // So, we're including "processing" invoices created more than 3 days ago in the reserve balance as well- lt: THREE_DAYS_AGO,- },- },- _sum: {- amount: true,- },- }),- prisma.invoice.aggregate({- where: {- status: "processing",- paymentMethod: "ach_fast",- createdAt: {- // Same reasoning as above, but fast ACH can settle in 2 business days instead of 4- lt: ONE_DAY_AGO,- },- },- _sum: {- amount: true,- },- }),prisma.payout.aggregate({where: {- status: "processed",+ status: {+ in: ["processing", "processed"],+ },},_sum: {amount: true,@@ -69,30 +31,28 @@ export async function GET(req: Request) {const currentAvailableBalance = stripeBalanceData.available[0].amount; // available to withdrawconst currentPendingBalance = stripeBalanceData.pending[0].amount; // balance waiting to settle+ // x-slack-ref: https://dub.slack.com/archives/C074P7LMV9C/p1750185638973479+ const currentNetBalance =+ currentPendingBalance < 0+ ? currentAvailableBalance + currentPendingBalance+ : currentAvailableBalance;- const processingInvoicesAmount =- (processingInvoicesDirectDebit._sum.amount ?? 0) +- (processingInvoicesDirectDebitFast._sum.amount ?? 0);- const processedPayoutsAmount = processedPayouts._sum.amount ?? 0;-- const amountToKeepOnStripe =- processingInvoicesAmount + processedPayoutsAmount;-- const balanceToWithdraw = currentAvailableBalance - amountToKeepOnStripe;+ const payoutsToBeSent = payoutsToBeSentData._sum.amount ?? 0;+ const reservedBalance = 30_000_00; // keep at least $30,000 in the account+ const balanceToWithdraw =+ currentNetBalance - payoutsToBeSent - reservedBalance;console.log({currentAvailableBalance: `${currencyFormatter(currentAvailableBalance / 100)}`,currentPendingBalance: `${currencyFormatter(currentPendingBalance / 100)}`,- processingInvoicesAmount: `${currencyFormatter(processingInvoicesAmount / 100)}`,- processedPayoutsAmount: `${currencyFormatter(processedPayoutsAmount / 100)}`,- amountToKeepOnStripe: `${currencyFormatter(amountToKeepOnStripe / 100)}`,+ currentNetBalance: `${currencyFormatter(currentNetBalance / 100)}`,+ payoutsToBeSent: `${currencyFormatter(payoutsToBeSent / 100)}`,balanceToWithdraw: `${currencyFormatter(balanceToWithdraw / 100)}`,});- const reservedBalance = 30_000_00; // keep at least $30,000 in the account- if (balanceToWithdraw <= reservedBalance) {+ if (balanceToWithdraw <= 0) {return logAndRespond(- `Balance to withdraw is less than ${currencyFormatter(reservedBalance / 100)}, skipping...`,+ `Balance to withdraw (after deducting payouts to be sent and reserved balance) is less than $0, skipping...`,);}