Files
calendar/packages/app-store/_utils/payments/currencyConversions.ts
T
+2
Anwar SadathGitHubcoderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>Omar Lópezamritcoderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>Kartik SainiAnik Dhabal BabuDevanshu SharmaBenny JooPeer RichelsenVolnei Munhoz
55e6682832 feat: Custom Fields OR Add-Ons with a fee associated (#19997)
* feat: Addons to bookings

* Add tests

* Add tests

* Fix price field not shown immediately after enabling payment for an event

* Add support for all currencies in addons and fix type errors

* Address review comments

* Add tests to cover all input types

* Address cubic-dev-ai comments

* Remove openapi.json from git tracking

* Revert changes

* Revert unrelated linter/formatter changes

* Address cubic-dev-ai comments

* Addres cubic-dev-ai comment

* Update to use test id instead of hardcoded label

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Address coderabbits review comments

* Fix failing unit test

* some fixes...

* Fix tests

* Fix import

* Move handlePayment test file to appropriate location

* Fix test

---------

Co-authored-by: Omar López <zomars@me.com>
Co-authored-by: amrit <iamamrit27@gmail.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Kartik Saini <41051387+kart1ka@users.noreply.github.com>
Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
Co-authored-by: Devanshu Sharma <devanshusharma658@gmail.com>
Co-authored-by: Benny Joo <sldisek783@gmail.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: Volnei Munhoz <volnei.munhoz@gmail.com>
2025-09-16 21:33:23 +09:00

59 lines
1.7 KiB
TypeScript

const zeroDecimalCurrencies = [
"BIF",
"CLP",
"DJF",
"GNF",
"JPY",
"KMF",
"KRW",
"MGA",
"PYG",
"RWF",
"UGX",
"VND",
"VUV",
"XAF",
"XOF",
"XPF",
];
export const convertToSmallestCurrencyUnit = (amount: number, currency: string) => {
// Special cases
if (zeroDecimalCurrencies.includes(currency.toUpperCase())) {
return amount;
}
return Math.round(amount * 100);
};
export const convertFromSmallestToPresentableCurrencyUnit = (amount: number, currency: string) => {
// Special cases
if (zeroDecimalCurrencies.includes(currency.toUpperCase())) {
return amount;
}
return amount / 100;
};
export const getCurrencySymbol = (currencyCode: string): string => {
try {
const formatter = Intl.NumberFormat("en", { style: "currency", currency: currencyCode });
// formatToParts(1) breaks down the formatted number (1) into its constituent parts
// like currency symbol, decimal separator, etc. We use 1 as it's a simple number
// that will show the currency symbol clearly.
// For example, since we are formatting the number 1 with USD currency, formatToParts(1) would return an array like:
// [
// { type: "currency", value: "$" },
// { type: "integer", value: "1" },
// { type: "decimal", value: "." },
// { type: "fraction", value: "00" }
// ]
const parts = formatter.formatToParts(1);
const currencyPart = parts.find((part) => part.type === "currency");
return currencyPart?.value || "$";
} catch {
// Ideally we would not reach here, but if for some reason we reach here, we return
// $ as default currency
console.warn(`Failed to get currency symbol for ${currencyCode}, falling back to $`);
return "$";
}
};