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.

Update convert-currency.ts

Steven Tey178bdae346f8
diff --git a/apps/web/lib/analytics/convert-currency.ts b/apps/web/lib/analytics/convert-currency.ts
index 4d72b79849..1404c8a7e4 100644
--- a/apps/web/lib/analytics/convert-currency.ts
+++ b/apps/web/lib/analytics/convert-currency.ts
@@ -31,25 +31,26 @@ export const convertCurrency = async ({
const fxRate = await redis.hget("fxRates:usd", currencyCode); // e.g. for MYR it'll be around 4.4
- if (fxRate) {
- // convert amount to USD based on the current FX rate
- let convertedAmount = amount / Number(fxRate);
-
- // if the currency is a zero decimal currency, we need to multiply the converted amount by 100
- if (isZeroDecimalCurrency) {
- convertedAmount = convertedAmount * 100;
- }
-
+ // if the FX rate is not found, we return the original amount
+ if (!fxRate) {
return {
- currency: "usd",
- // round the final converted amount to 0 decimal places (USD in cents)
- amount: Math.round(convertedAmount),
+ currency,
+ amount,
};
}
+ // convert amount to USD based on the current FX rate
+ let convertedAmount = amount / Number(fxRate);
+
+ // if the currency is a zero decimal currency, we need to multiply the converted amount by 100
+ if (isZeroDecimalCurrency) {
+ convertedAmount = convertedAmount * 100;
+ }
+
return {
- currency,
- amount,
+ currency: "usd",
+ // round the final converted amount to 0 decimal places (USD in cents)
+ amount: Math.round(convertedAmount),
};
};
@@ -65,26 +66,27 @@ export const convertCurrencyWithFxRates = ({
const currencyCode = currency.toUpperCase();
const isZeroDecimalCurrency = ZERO_DECIMAL_CURRENCIES.includes(currencyCode);
- if (fxRates) {
- const fxRate = fxRates[currencyCode];
-
- // convert amount to USD based on the current FX rate
- let convertedAmount = amount / Number(fxRate);
-
- // if the currency is a zero decimal currency, we need to multiply the converted amount by 100
- if (isZeroDecimalCurrency) {
- convertedAmount = convertedAmount * 100;
- }
+ const fxRate = fxRates[currencyCode];
+ // if the FX rate is not found, we return the original amount
+ if (!fxRate) {
return {
- currency: "usd",
- // round the final converted amount to 0 decimal places (USD in cents)
- amount: Math.round(convertedAmount),
+ currency,
+ amount,
};
}
+ // convert amount to USD based on the current FX rate
+ let convertedAmount = amount / Number(fxRate);
+
+ // if the currency is a zero decimal currency, we need to multiply the converted amount by 100
+ if (isZeroDecimalCurrency) {
+ convertedAmount = convertedAmount * 100;
+ }
+
return {
- currency,
- amount,
+ currency: "usd",
+ // round the final converted amount to 0 decimal places (USD in cents)
+ amount: Math.round(convertedAmount),
};
};