The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add support for zero-decimal currencies
Steven Teya6fb257ab047
diff --git a/apps/web/lib/analytics/convert-currency.ts b/apps/web/lib/analytics/convert-currency.tsindex b5c3b99323..3520e49140 100644--- a/apps/web/lib/analytics/convert-currency.ts+++ b/apps/web/lib/analytics/convert-currency.ts@@ -1,5 +1,24 @@import { redis } from "../upstash/redis";+const ZERO_DECIMAL_CURRENCIES = [+ "BIF",+ "CLP",+ "DJF",+ "GNF",+ "JPY",+ "KMF",+ "KRW",+ "MGA",+ "PYG",+ "RWF",+ "UGX",+ "VND",+ "VUV",+ "XAF",+ "XOF",+ "XPF",+];+export const convertCurrency = async ({currency,amount,@@ -7,14 +26,19 @@ export const convertCurrency = async ({currency: string;amount: number;}) => {+ const isZeroDecimalCurrency = ZERO_DECIMAL_CURRENCIES.includes(+ currency.toUpperCase(),+ );+const fxRates = await redis.hget("fxRates:usd", currency.toUpperCase()); // e.g. for MYR it'll be around 4.4if (fxRates) {+ // convert amount to USD (in cents) based on the current FX rate and round it to 0 decimal places+ const convertedAmount = Math.round(amount / Number(fxRates));return {currency: "usd",- // convert amount to USD (in cents) based on the current FX rate- // round it to 0 decimal places- amount: Math.round(amount / Number(fxRates)),+ // if the currency is a zero decimal currency, we need to multiply the amount by 100 to convert it to cents+ amount: isZeroDecimalCurrency ? convertedAmount * 100 : convertedAmount,};}