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.

support currency

Steven Teybe34a368637e
diff --git a/apps/web/app/api/partners/sales/route.ts b/apps/web/app/api/partners/sales/route.ts
index 5b48a5e58f..831f3f5468 100644
--- a/apps/web/app/api/partners/sales/route.ts
+++ b/apps/web/app/api/partners/sales/route.ts
@@ -4,6 +4,7 @@ import { calculateSaleEarnings } from "@/lib/api/sales/calculate-sale-earnings";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth/workspace";
import { determinePartnerReward } from "@/lib/partners/determine-partner-reward";
+import { redis } from "@/lib/upstash";
import { updatePartnerSaleSchema } from "@/lib/zod/schemas/partners";
import { ProgramSaleSchema } from "@/lib/zod/schemas/program-sales";
import { prisma } from "@dub/prisma";
@@ -12,7 +13,7 @@ import { NextResponse } from "next/server";
// PATCH /api/partners/sales - update a sale
export const PATCH = withWorkspace(
async ({ req, workspace }) => {
- const { programId, invoiceId, amount, modifyAmount } =
+ let { programId, invoiceId, amount, modifyAmount, currency } =
updatePartnerSaleSchema.parse(await parseRequestBody(req));
const program = await getProgramOrThrow({
@@ -47,6 +48,22 @@ export const PATCH = withWorkspace(
}
const { partner, amount: originalAmount } = sale;
+
+ // if currency is not USD, convert it to USD based on the current FX rate
+ // TODO: allow custom "defaultCurrency" on workspace table in the future
+ if (currency !== "usd") {
+ const fxRates = await redis.hget("fxRates:usd", currency.toUpperCase()); // e.g. for MYR it'll be around 4.4
+ if (fxRates) {
+ currency = "usd";
+ // convert amount to USD (in cents) based on the current FX rate
+ // round it to 0 decimal places
+ amount = Math.round(originalAmount / Number(fxRates));
+ modifyAmount = modifyAmount
+ ? Math.round(modifyAmount / Number(fxRates))
+ : undefined;
+ }
+ }
+
const finalAmount = modifyAmount
? originalAmount + modifyAmount
: amount ?? originalAmount;
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index e5f5890c8a..5bb83c887d 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -303,6 +303,13 @@ export const updatePartnerSaleSchema = z
"Modify the current sale amount: use positive values to increase the amount, negative values to decrease it.",
)
.optional(),
+ currency: z
+ .string()
+ .default("usd")
+ .transform((val) => val.toLowerCase())
+ .describe(
+ "The currency of the sale amount to update. Accepts ISO 4217 currency codes.",
+ ),
})
.refine(
(data) => data.amount !== undefined || data.modifyAmount !== undefined,