The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Refactor payout invoice creation
Kiran K9b327620f9c2
diff --git a/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts b/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.tsindex 8c86f24e4d..ac2425912c 100644--- a/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts+++ b/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts@@ -166,61 +166,53 @@ export async function confirmPayouts({const invoiceNumber = `${workspace.invoicePrefix}-${paddedNumber}`;// Create the invoice for the payouts- const newInvoice = await prisma.$transaction(async (tx) => {- const invoice = await tx.invoice.create({- data: {- id: createId({ prefix: "inv_" }),- number: invoiceNumber,- programId: program.id,- workspaceId: workspace.id,- amount: payoutAmount,- fee: totalFee,- total,- },- });-- if (!invoice) {- throw new Error("Failed to create payout invoice.");- }+ const invoice = await prisma.invoice.create({+ data: {+ id: createId({ prefix: "inv_" }),+ number: invoiceNumber,+ programId: program.id,+ workspaceId: workspace.id,+ amount: payoutAmount,+ fee: totalFee,+ total,+ },+ });- await stripe.paymentIntents.create({- amount: convertedTotal,- customer: workspace.stripeId!,- payment_method_types: [paymentMethod.type],- payment_method: paymentMethod.id,- currency,- confirmation_method: "automatic",- confirm: true,- transfer_group: invoice.id,- statement_descriptor: "Dub Partners",- description: `Dub Partners payout invoice (${invoice.id})`,- });+ await stripe.paymentIntents.create({+ amount: convertedTotal,+ customer: workspace.stripeId!,+ payment_method_types: [paymentMethod.type],+ payment_method: paymentMethod.id,+ currency,+ confirmation_method: "automatic",+ confirm: true,+ transfer_group: invoice.id,+ statement_descriptor: "Dub Partners",+ description: `Dub Partners payout invoice (${invoice.id})`,+ });- await tx.payout.updateMany({- where: {- id: {- in: payouts.map((p) => p.id),- },- },- data: {- invoiceId: invoice.id,- status: "processing",- userId,+ await prisma.payout.updateMany({+ where: {+ id: {+ in: payouts.map((p) => p.id),},- });+ },+ data: {+ invoiceId: invoice.id,+ status: "processing",+ userId,+ },+ });- await tx.project.update({- where: {- id: workspace.id,- },- data: {- payoutsUsage: {- increment: payoutAmount,- },+ await prisma.project.update({+ where: {+ id: workspace.id,+ },+ data: {+ payoutsUsage: {+ increment: payoutAmount,},- });-- return invoice;+ },});await log({@@ -231,7 +223,7 @@ export async function confirmPayouts({// Send emails to all the partners involved in the payouts if the payout method is Direct Debit// This is because Direct Debit takes 4 business days to process, so we want to give partners a heads upif (- newInvoice &&+ invoice &&DIRECT_DEBIT_PAYMENT_METHOD_TYPES.includes(paymentMethod.type)) {if (!resend) {diff --git a/apps/web/app/(ee)/api/cron/payouts/confirm/route.ts b/apps/web/app/(ee)/api/cron/payouts/confirm/route.tsindex 3ad5000dfb..d3d1709b7f 100644--- a/apps/web/app/(ee)/api/cron/payouts/confirm/route.ts+++ b/apps/web/app/(ee)/api/cron/payouts/confirm/route.ts@@ -60,7 +60,7 @@ export async function POST(req: Request) {} catch (error) {await log({message: `Error confirming payouts for program: ${error.message}`,- type: "cron",+ type: "errors",});return handleAndReturnErrorResponse(error);