chore: remove pricing conversions on admin (#7665)

This commit is contained in:
Frane Polić
2024-06-10 17:38:35 +02:00
committed by GitHub
parent f08f0d6cc9
commit 3b8160b564
7 changed files with 25 additions and 103 deletions
@@ -4,47 +4,6 @@ export const getDecimalDigits = (currency: string) => {
return currencies[currency.toUpperCase()]?.decimal_digits ?? 0
}
/**
* Converts an amount from the database format (cents) to the presentational format
*
* @param amount - The amount to format
* @param currency - The currency code to format the amount in
* @returns The formatted amount
*
* @example
* getPresentationalAmount(1000, "usd") // 10
* getPresentationalAmount(1000, "jpy") // 1000
*/
export const getPresentationalAmount = (amount: number, currency: string) => {
const decimalDigits = getDecimalDigits(currency)
if (decimalDigits === undefined) {
throw new Error("Currency has no decimal digits")
}
return amount / 10 ** decimalDigits
}
/**
* Converts an amount to the database format (cents)
* @param amount - The amount to convert to the database amount
* @param currency - The currency code to convert the amount to
* @returns The amount in the database format
*
* @example
* getDbAmount(10.5, "usd") // 1050
* getDbAmount(10, "jpy") // 10
*/
export const getDbAmount = (amount: number, currency: string) => {
const decimalDigits = getDecimalDigits(currency)
if (decimalDigits === undefined) {
throw new Error("Currency has no decimal digits")
}
return amount * 10 ** decimalDigits
}
/**
* Returns a formatted amount based on the currency code using the browser's locale
* @param amount - The amount to format
@@ -52,8 +11,8 @@ export const getDbAmount = (amount: number, currency: string) => {
* @returns - The formatted amount
*
* @example
* getFormattedAmount(1000, "usd") // '$10.00' if the browser's locale is en-US
* getFormattedAmount(1000, "usd") // '10,00 $' if the browser's locale is fr-FR
* getFormattedAmount(10, "usd") // '$10.00' if the browser's locale is en-US
* getFormattedAmount(10, "usd") // '10,00 $' if the browser's locale is fr-FR
*/
export const getLocaleAmount = (amount: number, currencyCode: string) => {
const formatter = new Intl.NumberFormat(undefined, {
@@ -62,7 +21,7 @@ export const getLocaleAmount = (amount: number, currencyCode: string) => {
currency: currencyCode,
})
return formatter.format(getPresentationalAmount(amount, currencyCode))
return formatter.format(amount)
}
export const getNativeSymbol = (currencyCode: string) => {
@@ -84,9 +43,8 @@ export const getNativeSymbol = (currencyCode: string) => {
export const getStylizedAmount = (amount: number, currencyCode: string) => {
const symbol = getNativeSymbol(currencyCode)
const decimalDigits = getDecimalDigits(currencyCode)
const presentationAmount = getPresentationalAmount(amount, currencyCode)
const total = presentationAmount.toLocaleString(undefined, {
const total = amount.toLocaleString(undefined, {
minimumFractionDigits: decimalDigits,
maximumFractionDigits: decimalDigits,
})