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.

Multi-currency support in `/track/sale`

Steven Tey87c596e40abc
diff --git a/apps/web/app/api/cron/aggregate-clicks/route.ts b/apps/web/app/api/cron/aggregate-clicks/route.ts
index cafe6e04df..442d4829f1 100644
--- a/apps/web/app/api/cron/aggregate-clicks/route.ts
+++ b/apps/web/app/api/cron/aggregate-clicks/route.ts
@@ -9,8 +9,7 @@ import { NextResponse } from "next/server";
export const dynamic = "force-dynamic";
/**
- * TODO:
- * - Use a cron job (similar to how we do it for usage cron) to account for the future where we have a lot of links to process
+ * TODO: Use a cron job (similar to how we do it for usage cron) to account for the future where we have a lot of links to process
*/
// This route is used aggregate clicks events on daily basis for Program links and add to the Commission table
diff --git a/apps/web/app/api/cron/fx-rates/route.ts b/apps/web/app/api/cron/fx-rates/route.ts
new file mode 100644
index 0000000000..658d8805eb
--- /dev/null
+++ b/apps/web/app/api/cron/fx-rates/route.ts
@@ -0,0 +1,49 @@
+import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { verifyVercelSignature } from "@/lib/cron/verify-vercel";
+import { redis } from "@/lib/upstash";
+import { log } from "@dub/utils";
+import { NextResponse } from "next/server";
+
+export const dynamic = "force-dynamic";
+
+// Cron to update the Foreign Exchange Rates in Redis
+// Runs once every day at 00:00 (0 0 * * *)
+export async function GET(req: Request) {
+ try {
+ await verifyVercelSignature(req);
+
+ const res = await fetch("https://api.currencyapi.com/v3/latest", {
+ headers: {
+ apikey: process.env.CURRENCY_API_KEY || "",
+ },
+ });
+
+ const { data } = (await res.json()) as {
+ data: Record<string, { value: number }>;
+ };
+
+ if (!data) {
+ return NextResponse.json({
+ message: "Failed to fetch FX rates",
+ });
+ }
+
+ const transformedRates: Record<string, number> = {};
+
+ for (const [ticker, details] of Object.entries(data)) {
+ transformedRates[ticker] = details.value;
+ }
+
+ // // Store FX rates in Redis (with USD as the base currency)
+ await redis.hset("fxRates:usd", transformedRates);
+
+ return NextResponse.json(transformedRates);
+ } catch (error) {
+ await log({
+ message: `Error updating FX rates: ${error.message}`,
+ type: "cron",
+ });
+
+ return handleAndReturnErrorResponse(error);
+ }
+}
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index c17596f6c0..6d380bd6bc 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -23,7 +23,7 @@ import { determinePartnerReward } from "../determine-partner-reward-edge";
// POST /api/track/sale – Track a sale conversion event
export const POST = withWorkspace(
async ({ req, workspace }) => {
- const {
+ let {
externalId,
customerId, // deprecated
paymentProcessor,
@@ -95,6 +95,17 @@ export const POST = withWorkspace(
.omit({ timestamp: true })
.parse(leadEvent.data[0]);
+ // if currency is not USD, convert it to USD
+ 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 FX rate
+ // round it to 0 decimal places
+ amount = Math.round(amount / Number(fxRates));
+ }
+ }
+
const eventId = nanoid(16);
const saleData = {