Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Refactor `retryFailedPaypalPayoutsAction` (#3152)

Kiran K8deb13ef74ad
diff --git a/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts b/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts
index 41a76122ca..f04f15827d 100644
--- a/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts
+++ b/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts
@@ -34,8 +34,8 @@ export const retryFailedPaypalPayoutsAction = authPartnerActionClient
throw new Error("Connect your PayPal account to enable payouts.");
}
- const { success } = await ratelimit(5, "24 h").limit(
- `retry-failed-paypal-payouts:${partner.id}`,
+ const { success } = await ratelimit(1, "12 h").limit(
+ `retry-failed-paypal-payouts:${payoutId}`,
);
if (!success) {
@@ -44,58 +44,105 @@ export const retryFailedPaypalPayoutsAction = authPartnerActionClient
);
}
- const payout = await prisma.payout.findUniqueOrThrow({
- where: {
- id: payoutId,
- },
- select: {
- id: true,
- invoiceId: true,
- partnerId: true,
- status: true,
- amount: true,
- paypalTransferId: true,
- program: {
- select: {
- name: true,
+ // Use a transaction to atomically check and update the payout status
+ // This prevents race conditions where multiple retry requests happen concurrently
+ const updatedPayout = await prisma.$transaction(async (tx) => {
+ const payout = await tx.payout.findUnique({
+ where: {
+ id: payoutId,
+ },
+ select: {
+ id: true,
+ invoiceId: true,
+ partnerId: true,
+ status: true,
+ amount: true,
+ paypalTransferId: true,
+ program: {
+ select: {
+ name: true,
+ },
},
},
- },
- });
+ });
- if (payout.partnerId !== partner.id) {
- throw new Error("You are not authorized to retry this payout.");
- }
+ if (!payout) {
+ throw new Error("Payout not found.");
+ }
- if (payout.status !== "failed") {
- throw new Error(
- "This payout cannot be retried (you can only retry failed payouts).",
- );
- }
+ if (payout.partnerId !== partner.id) {
+ throw new Error("You are not authorized to retry this payout.");
+ }
- if (!payout.invoiceId) {
- throw new Error("This payout has no invoice ID.");
- }
+ if (payout.status !== "failed") {
+ throw new Error(
+ `This payout cannot be retried. Current status: ${payout.status}. Only failed payouts can be retried.`,
+ );
+ }
- if (!payout.paypalTransferId) {
- throw new Error("This payout has no existing PayPal transfer ID.");
- }
+ if (!payout.paypalTransferId) {
+ throw new Error("This payout has no existing PayPal transfer ID.");
+ }
+
+ if (!payout.invoiceId) {
+ throw new Error("This payout has no invoice ID.");
+ }
+
+ const updateResult = await tx.payout.updateMany({
+ where: {
+ id: payout.id,
+ status: "failed", // Only update if still in "failed" status
+ },
+ data: {
+ status: "processing",
+ },
+ });
+
+ // If no rows were updated, another request already processed this payout
+ if (updateResult.count === 0) {
+ throw new Error(
+ "This payout is already being processed or has been sent. Please wait for it to complete.",
+ );
+ }
+
+ return payout;
+ });
try {
await createPayPalBatchPayout({
- invoiceId: `${payout.invoiceId}-${nanoid(7)}`,
+ invoiceId: `${updatedPayout.invoiceId}-${nanoid(7)}`,
payouts: [
{
- id: payout.id,
- amount: payout.amount,
- program: payout.program,
+ id: updatedPayout.id,
+ amount: updatedPayout.amount,
+ program: updatedPayout.program,
partner: {
paypalEmail: partner.paypalEmail,
},
},
],
});
+
+ // Update status to "sent" after successful PayPal batch creation
+ await prisma.payout.update({
+ where: {
+ id: updatedPayout.id,
+ },
+ data: {
+ status: "sent",
+ },
+ });
} catch (error) {
+ // If PayPal batch creation fails, revert status back to "failed" so payout can be retried
+ await prisma.payout.update({
+ where: {
+ id: updatedPayout.id,
+ },
+ data: {
+ status: "failed",
+ },
+ });
+
throw new Error(
"Failed to retry payout. Please try again or contact support.",
);