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 to Dots API

Kiran Kdde6eca93c6b
diff --git a/apps/web/lib/actions/add-bank-account.ts b/apps/web/lib/actions/add-bank-account.ts
index e072d1988a..1a1d571c59 100644
--- a/apps/web/lib/actions/add-bank-account.ts
+++ b/apps/web/lib/actions/add-bank-account.ts
@@ -6,6 +6,7 @@ import { addAppAchAccount } from "../dots/add-app-ach-account";
import { createDotsApp } from "../dots/create-dots-app";
import { addBankAccountSchema } from "../dots/schemas";
import { authActionClient } from "./safe-action";
+import { createIdempotencyKey } from "../dots/utils";
const schema = addBankAccountSchema.extend({ workspaceId: z.string() });
@@ -33,6 +34,8 @@ export const addBankAccountAction = authActionClient
dotsAppId = dotsApp.id;
}
+ await createIdempotencyKey();
+
// Add bank account to Dots app
const achAccount = await addAppAchAccount({
dotsAppId,
diff --git a/apps/web/lib/dots/create-org-transfer.ts b/apps/web/lib/dots/create-org-transfer.ts
index e9000a88d1..7f739d8cbd 100644
--- a/apps/web/lib/dots/create-org-transfer.ts
+++ b/apps/web/lib/dots/create-org-transfer.ts
@@ -1,4 +1,5 @@
import { dotsFetch } from "./fetch";
+import { createIdempotencyKey } from "./utils";
export const createOrgTransfer = async ({
amount,
@@ -8,11 +9,13 @@ export const createOrgTransfer = async ({
dotsAppId: string;
}) => {
console.log(`Creating an org transfer of ${amount} cents`);
+
return await dotsFetch("/organization-transfers", {
method: "POST",
body: {
amount,
api_app_id: dotsAppId,
+ idempotency_key: await createIdempotencyKey(),
},
});
};
diff --git a/apps/web/lib/dots/create-transfer.ts b/apps/web/lib/dots/create-transfer.ts
index 0b09447cb5..49c2584012 100644
--- a/apps/web/lib/dots/create-transfer.ts
+++ b/apps/web/lib/dots/create-transfer.ts
@@ -1,4 +1,5 @@
import { dotsFetch } from "./fetch";
+import { createIdempotencyKey } from "./utils";
export const createTransfer = async ({
amount,
@@ -10,12 +11,14 @@ export const createTransfer = async ({
dotsUserId: string;
}) => {
console.log(`Creating a transfer of ${amount} cents`);
+
return await dotsFetch("/transfers", {
method: "POST",
dotsAppId,
body: {
user_id: dotsUserId,
amount: -amount, // negative means transfer from Business to Partner
+ idempotency_key: await createIdempotencyKey(),
},
});
};
diff --git a/apps/web/lib/dots/create-withdrawal.ts b/apps/web/lib/dots/create-withdrawal.ts
index d9eafd8362..6ffeed455f 100644
--- a/apps/web/lib/dots/create-withdrawal.ts
+++ b/apps/web/lib/dots/create-withdrawal.ts
@@ -1,5 +1,6 @@
import { dotsFetch } from "./fetch";
import { DotsPayoutPlatform } from "./types";
+import { createIdempotencyKey } from "./utils";
export const createWithdrawal = async ({
dotsUserId,
@@ -20,6 +21,7 @@ export const createWithdrawal = async ({
amount,
platform,
payout_fee_party: payoutFeeParty,
+ idempotency_key: await createIdempotencyKey(),
},
});
};
diff --git a/apps/web/lib/dots/deposit-funds.ts b/apps/web/lib/dots/deposit-funds.ts
index 8d531d543d..1c511729a9 100644
--- a/apps/web/lib/dots/deposit-funds.ts
+++ b/apps/web/lib/dots/deposit-funds.ts
@@ -1,5 +1,6 @@
import z from "../zod";
import { dotsFetch } from "./fetch";
+import { createIdempotencyKey } from "./utils";
const statusSchema = z.enum([
"created",
@@ -34,7 +35,7 @@ export const depositFunds = async ({
method: "POST",
body: {
amount: Number(amount) * 100, // The amount to deposit in cents
- idempotency_key: crypto.randomUUID(),
+ idempotency_key: await createIdempotencyKey(),
},
});
diff --git a/apps/web/lib/dots/utils.ts b/apps/web/lib/dots/utils.ts
new file mode 100644
index 0000000000..c614d382b0
--- /dev/null
+++ b/apps/web/lib/dots/utils.ts
@@ -0,0 +1,17 @@
+import { redis } from "@/lib/upstash";
+
+// TODO: Move this to a shared library
+export const createIdempotencyKey = async () => {
+ const key = crypto.randomUUID();
+
+ const ok = await redis.set(`dots:idempotency:${key}`, 1, {
+ ex: 24 * 60 * 60, // In Dots API, idempotency keys have a 24-hour timeout.
+ nx: true,
+ });
+
+ if (!ok) {
+ throw new Error("Duplicate idempotency key. Please try again.");
+ }
+
+ return key;
+};