Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Idempotency keys #1731

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/web/lib/actions/partners/create-dots-transfer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use server";

import { createIdempotencyKey } from "@/lib/dots/idempotency";
import { prisma } from "@/lib/prisma";
import { z } from "zod";
import { createOrgTransfer } from "../../dots/create-org-transfer";
Expand Down Expand Up @@ -44,10 +45,12 @@ export const createDotsTransferAction = authActionClient
amount: payout.amount,
dotsAppId: workspace.dotsAppId,
dotsUserId: programEnrollment.dotsUserId,
idempotencyKey: await createIdempotencyKey(`transfer_${payoutId}`),
}),
createOrgTransfer({
amount: payout.fee,
dotsAppId: workspace.dotsAppId,
idempotencyKey: await createIdempotencyKey(`org_transfer_${payoutId}`),
}),
]);

Expand Down
3 changes: 3 additions & 0 deletions apps/web/lib/dots/create-org-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { dotsFetch } from "./fetch";
export const createOrgTransfer = async ({
amount,
dotsAppId,
idempotencyKey,
}: {
amount: number;
dotsAppId: string;
idempotencyKey: string;
}) => {
console.log(`Creating an org transfer of ${amount} cents`);

Expand All @@ -14,6 +16,7 @@ export const createOrgTransfer = async ({
body: {
amount,
api_app_id: dotsAppId,
idempotency_key: idempotencyKey,
},
});
};
3 changes: 3 additions & 0 deletions apps/web/lib/dots/create-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export const createTransfer = async ({
amount,
dotsAppId,
dotsUserId,
idempotencyKey,
}: {
amount: number;
dotsAppId: string;
dotsUserId: string;
idempotencyKey: string;
}) => {
console.log(`Creating a transfer of ${amount} cents`);

Expand All @@ -17,6 +19,7 @@ export const createTransfer = async ({
body: {
user_id: dotsUserId,
amount: -amount, // negative means transfer from Business to Partner
idempotency_key: idempotencyKey,
},
});
};
13 changes: 13 additions & 0 deletions apps/web/lib/dots/idempotency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { redis } from "@/lib/upstash";

export const createIdempotencyKey = async (id: string) => {
const idempotencyKey = crypto.randomUUID();

const oldValue = await redis.set(id, idempotencyKey, {
ex: 24 * 60 * 60, // In Dots API, idempotency keys have a 24-hour timeout.
nx: true,
get: true,
});

return oldValue || idempotencyKey;
};
Loading