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.

add idempotency key for createTransfer

Steven Teyb7a5fcc1af5d
diff --git a/apps/web/lib/actions/partners/create-partner-payout.ts b/apps/web/lib/actions/partners/create-partner-payout.ts
index f6c4286b02..8f7c4a069b 100644
--- a/apps/web/lib/actions/partners/create-partner-payout.ts
+++ b/apps/web/lib/actions/partners/create-partner-payout.ts
@@ -6,6 +6,7 @@ import { formatDate } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { sendEmail } from "emails";
import PartnerPayoutSent from "emails/partner-payout-sent";
+import { v5 as uuidv5 } from "uuid";
import { z } from "zod";
import { createOrgTransfer } from "../../dots/create-org-transfer";
import { createTransfer } from "../../dots/create-transfer";
@@ -47,17 +48,19 @@ export const createPartnerPayoutAction = authActionClient
throw new Error("Partner is not properly enrolled in this program");
}
- const [transfer, orgTransfer] = await Promise.all([
- createTransfer({
- amount: payout.amount,
- dotsAppId: workspace.dotsAppId,
- dotsUserId: programEnrollment.dotsUserId,
- }),
- createOrgTransfer({
- amount: payout.fee,
- dotsAppId: workspace.dotsAppId,
- }),
- ]);
+ const transfer = await createTransfer({
+ amount: payout.amount,
+ dotsAppId: workspace.dotsAppId,
+ dotsUserId: programEnrollment.dotsUserId,
+ idempotencyKey: uuidv5(payout.id, uuidv5.URL), // pass a unique idempotency key for each payout to avoid duplicate payouts
+ });
+
+ // we're splitting this out of the Promise.all() to avoid a race condition
+ // e.g. if the transfer fails, we shouldn't proceed with creating the org transfer
+ const orgTransfer = await createOrgTransfer({
+ amount: payout.fee,
+ dotsAppId: workspace.dotsAppId,
+ });
await Promise.all([
prisma.payout.update({
diff --git a/apps/web/lib/dots/create-transfer.ts b/apps/web/lib/dots/create-transfer.ts
index 0711b12365..48fa6d69a6 100644
--- a/apps/web/lib/dots/create-transfer.ts
+++ b/apps/web/lib/dots/create-transfer.ts
@@ -4,12 +4,16 @@ export const createTransfer = async ({
amount,
dotsAppId,
dotsUserId,
+ idempotencyKey,
}: {
amount: number;
dotsAppId: string;
dotsUserId: string;
+ idempotencyKey: string;
}) => {
- console.log(`Creating a transfer of ${amount} cents`);
+ console.log(
+ `Creating a transfer of ${amount} cents, with idempotency key ${idempotencyKey}`,
+ );
return await dotsFetch("/transfers", {
method: "POST",
@@ -17,6 +21,7 @@ export const createTransfer = async ({
body: {
user_id: dotsUserId,
amount: -amount, // negative means transfer from Business to Partner
+ idempotency_key: idempotencyKey,
},
});
};
diff --git a/apps/web/package.json b/apps/web/package.json
index 567168ba1b..0816f4f790 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -111,6 +111,7 @@
"unique-names-generator": "^4.7.1",
"unsplash-js": "^7.0.18",
"use-debounce": "^10.0.4",
+ "uuid": "^11.0.3",
"vaul": "^0.6.8",
"zod": "^3.22.4",
"zod-error": "^1.5.0",
diff --git a/apps/web/ui/partners/payout-details-sheet.tsx b/apps/web/ui/partners/payout-details-sheet.tsx
index 7f55c222f2..35d818b781 100644
--- a/apps/web/ui/partners/payout-details-sheet.tsx
+++ b/apps/web/ui/partners/payout-details-sheet.tsx
@@ -159,6 +159,8 @@ function PayoutDetailsSheetContent({
error: error ? "Failed to load sales" : undefined,
} as any);
+ const { queryParams } = useRouterStuff();
+
const { executeAsync, isExecuting } = useAction(createPartnerPayoutAction, {
onSuccess: async () => {
await mutate(
@@ -170,6 +172,7 @@ function PayoutDetailsSheetContent({
);
toast.success("Successfully confirmed payout!");
setIsOpen(false);
+ queryParams({ del: "payoutId" });
},
onError({ error }) {
toast.error(error.serverError);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 625e3d398d..f6597eea5a 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -333,6 +333,9 @@ importers:
use-debounce:
specifier: ^10.0.4
version: 10.0.4([email protected])
+ uuid:
+ specifier: ^11.0.3
+ version: 11.0.3
vaul:
specifier: ^0.6.8
@@ -21856,6 +21859,11 @@ packages:
engines: {node: '>= 0.4.0'}
dev: false
+ resolution: {integrity: sha512-d0z310fCWv5dJwnX1Y/MncBAqGMKEzlBb1AOf7z9K8ALnd0utBX/msg/fA0+sbyN1ihbMsLhrBlnl1ak7Wa0rg==}
+ hasBin: true
+ dev: false
+
resolution: {integrity: sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==}
hasBin: true