Files
twenty/front/src/utils/convert-currency-amount.ts
Weiko bc579d64a6 Rename Money/Url to Currency/Link and remove snake_case from composite fields (#2536)
* Rename Money/Url to Currency/Link

* regenerate front types

* renaming money/url field types

* fix double text

* fix tests

* fix server tests

* fix generate-target-column-map

* fix currency convert

* fix: tests

---------

Co-authored-by: Jérémy Magrin <jeremy.magrin@gmail.com>
2023-11-17 10:31:17 +01:00

31 lines
958 B
TypeScript

export const convertCurrencyToCurrencyMicros = (
currencyAmount: number | undefined,
) => {
if (!currencyAmount) {
return undefined;
}
const currencyAmountAsNumber = +currencyAmount;
if (isNaN(currencyAmountAsNumber)) {
throw new Error(`Cannot convert ${currencyAmount} to micros`);
}
const currencyAmountAsMicros = currencyAmountAsNumber * 1000000;
if (currencyAmountAsMicros % 1 !== 0) {
throw new Error(`Cannot convert ${currencyAmount} to micros`);
}
return currencyAmountAsMicros;
};
export const convertCurrencyMicrosToCurrency = (
currencyAmountMicros: number | undefined,
) => {
if (!currencyAmountMicros) {
return undefined;
}
const currencyAmountMicrosAsNumber = +currencyAmountMicros;
if (isNaN(currencyAmountMicrosAsNumber)) {
throw new Error(`Cannot convert ${currencyAmountMicros} to currency`);
}
const currencyAmount = currencyAmountMicrosAsNumber / 1000000;
return currencyAmount;
};