From 3b8160b56432f04cecb19d07f700c133f5532186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Mon, 10 Jun 2024 17:38:35 +0200 Subject: [PATCH] chore: remove pricing conversions on admin (#7665) --- .../dashboard/src/lib/money-amount-helpers.ts | 50 ++----------------- .../create-shipping-options-form.tsx | 8 ++- .../edit-shipping-options-pricing-form.tsx | 17 ++----- .../pricing-create-form.tsx | 17 ++----- .../pricing-products-prices-form.tsx | 18 ++----- .../routes/products/product-create/utils.ts | 5 +- .../products/product-prices/pricing-edit.tsx | 13 ++--- 7 files changed, 25 insertions(+), 103 deletions(-) diff --git a/packages/admin-next/dashboard/src/lib/money-amount-helpers.ts b/packages/admin-next/dashboard/src/lib/money-amount-helpers.ts index af0233c30d..62ef29b211 100644 --- a/packages/admin-next/dashboard/src/lib/money-amount-helpers.ts +++ b/packages/admin-next/dashboard/src/lib/money-amount-helpers.ts @@ -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, }) diff --git a/packages/admin-next/dashboard/src/routes/locations/shipping-options-create/components/create-shipping-options-form/create-shipping-options-form.tsx b/packages/admin-next/dashboard/src/routes/locations/shipping-options-create/components/create-shipping-options-form/create-shipping-options-form.tsx index 430ddb6252..fb60a5bb24 100644 --- a/packages/admin-next/dashboard/src/routes/locations/shipping-options-create/components/create-shipping-options-form/create-shipping-options-form.tsx +++ b/packages/admin-next/dashboard/src/routes/locations/shipping-options-create/components/create-shipping-options-form/create-shipping-options-form.tsx @@ -28,8 +28,8 @@ import { useRegions } from "../../../../../hooks/api/regions" import { useCreateShippingOptions } from "../../../../../hooks/api/shipping-options" import { useShippingProfiles } from "../../../../../hooks/api/shipping-profiles" import { formatProvider } from "../../../../../lib/format-provider" -import { getDbAmount } from "../../../../../lib/money-amount-helpers" import { CreateShippingOptionsPricesForm } from "./create-shipping-options-prices-form" +import { castNumber } from "../../../../../lib/cast-number" enum Tab { DETAILS = "details", @@ -103,8 +103,7 @@ export function CreateShippingOptionsForm({ const handleSubmit = form.handleSubmit(async (data) => { const currencyPrices = Object.entries(data.currency_prices) .map(([code, value]) => { - const amount = - value === "" ? undefined : getDbAmount(Number(value), code) + const amount = value === "" ? undefined : castNumber(value) return { currency_code: code, @@ -119,8 +118,7 @@ export function CreateShippingOptionsForm({ .map(([region_id, value]) => { const code = regionsMap.get(region_id) - const amount = - value === "" ? undefined : getDbAmount(Number(value), code) + const amount = value === "" ? undefined : castNumber(value) return { region_id, diff --git a/packages/admin-next/dashboard/src/routes/locations/shipping-options-edit-pricing/components/create-shipping-options-form/edit-shipping-options-pricing-form.tsx b/packages/admin-next/dashboard/src/routes/locations/shipping-options-edit-pricing/components/create-shipping-options-form/edit-shipping-options-pricing-form.tsx index 5a6acf5905..5ea401d40b 100644 --- a/packages/admin-next/dashboard/src/routes/locations/shipping-options-edit-pricing/components/create-shipping-options-form/edit-shipping-options-pricing-form.tsx +++ b/packages/admin-next/dashboard/src/routes/locations/shipping-options-edit-pricing/components/create-shipping-options-form/edit-shipping-options-pricing-form.tsx @@ -25,11 +25,8 @@ import { useCurrencies } from "../../../../../hooks/api/currencies" import { useRegions } from "../../../../../hooks/api/regions" import { useUpdateShippingOptions } from "../../../../../hooks/api/shipping-options" import { useStore } from "../../../../../hooks/api/store" -import { - getDbAmount, - getPresentationalAmount, -} from "../../../../../lib/money-amount-helpers" import { ExtendedProductDTO } from "../../../../../types/api-responses" +import { castNumber } from "../../../../../lib/cast-number.ts" const getInitialCurrencyPrices = (prices: PriceDTO[]) => { const ret: Record = {} @@ -38,10 +35,7 @@ const getInitialCurrencyPrices = (prices: PriceDTO[]) => { // this is a region price return } - ret[p.currency_code!] = getPresentationalAmount( - p.amount as number, - p.currency_code! - ) + ret[p.currency_code!] = castNumber(p.amount) }) return ret } @@ -51,10 +45,7 @@ const getInitialRegionPrices = (prices: PriceDTO[]) => { prices.forEach((p) => { if (p.price_rules!.length) { const regionId = p.price_rules![0].value - ret[regionId] = getPresentationalAmount( - p.amount as number, - p.currency_code! - ) + ret[regionId] = castNumber(p.amount) } }) @@ -145,7 +136,7 @@ export function EditShippingOptionsPricingForm({ return undefined } - const amount = getDbAmount(Number(value), code) + const amount = castNumber(value) const priceRecord = { currency_code: code, diff --git a/packages/admin-next/dashboard/src/routes/pricing/pricing-create/components/pricing-create-form/pricing-create-form.tsx b/packages/admin-next/dashboard/src/routes/pricing/pricing-create/components/pricing-create-form/pricing-create-form.tsx index bef7902da2..f4b580f29b 100644 --- a/packages/admin-next/dashboard/src/routes/pricing/pricing-create/components/pricing-create-form/pricing-create-form.tsx +++ b/packages/admin-next/dashboard/src/routes/pricing/pricing-create/components/pricing-create-form/pricing-create-form.tsx @@ -12,7 +12,6 @@ import { } from "../../../../../components/route-modal" import { useCreatePriceList } from "../../../../../hooks/api/price-lists" import { castNumber } from "../../../../../lib/cast-number" -import { getDbAmount } from "../../../../../lib/money-amount-helpers" import { PricingDetailsForm } from "./pricing-details-form" import { PricingPricesForm } from "./pricing-prices-form" import { PricingProductsForm } from "./pricing-products-form" @@ -89,10 +88,7 @@ export const PricingCreateForm = () => { } prices.push({ - amount: getDbAmount( - castNumber(currencyPrice.amount), - currencyCode - ), + amount: castNumber(currencyPrice.amount), currency_code: currencyCode, // @ts-expect-error type is wrong variant_id: variantId, @@ -127,13 +123,10 @@ export const PricingCreateForm = () => { ) => { form.clearErrors(fields) - const values = fields.reduce( - (acc, key) => { - acc[key] = form.getValues(key) - return acc - }, - {} as Record - ) + const values = fields.reduce((acc, key) => { + acc[key] = form.getValues(key) + return acc + }, {} as Record) const validationResult = schema.safeParse(values) diff --git a/packages/admin-next/dashboard/src/routes/pricing/pricing-products-prices/components/pricing-products-prices-form/pricing-products-prices-form.tsx b/packages/admin-next/dashboard/src/routes/pricing/pricing-products-prices/components/pricing-products-prices-form/pricing-products-prices-form.tsx index e61e219ffc..6c6b6af292 100644 --- a/packages/admin-next/dashboard/src/routes/pricing/pricing-products-prices/components/pricing-products-prices-form/pricing-products-prices-form.tsx +++ b/packages/admin-next/dashboard/src/routes/pricing/pricing-products-prices/components/pricing-products-prices-form/pricing-products-prices-form.tsx @@ -23,10 +23,6 @@ import { } from "../../../../../hooks/api/price-lists" import { useStore } from "../../../../../hooks/api/store" import { castNumber } from "../../../../../lib/cast-number" -import { - getDbAmount, - getPresentationalAmount, -} from "../../../../../lib/money-amount-helpers" import { ExtendedProductDTO } from "../../../../../types/api-responses" import { usePriceListGridColumns } from "../../../common/hooks/use-price-list-grid-columns" import { @@ -268,13 +264,8 @@ function initDefaultValues( variants[variant.id] = { currency_prices: currencyPrices.reduce( (prices, { currency_code, amount, id }) => { - const presentationAmount = getPresentationalAmount( - amount, - currency_code - ).toString() - prices[currency_code] = { - amount: presentationAmount, + amount: amount.toString(), id, } return prices @@ -350,15 +341,14 @@ function sortPrices( // If the price has not changed, we don't need to update it if ( originalPrice && - originalPrice.amount === - getDbAmount(castNumber(currencyPrice.amount), currencyCode) + originalPrice.amount === castNumber(currencyPrice.amount) ) { continue } pricesToUpdate.push({ id: currencyPrice.id, - amount: getDbAmount(castNumber(currencyPrice.amount), currencyCode), + amount: castNumber(currencyPrice.amount), currency_code: currencyCode, // @ts-expect-error type is wrong variant_id: variantId, @@ -374,7 +364,7 @@ function sortPrices( if (!currencyPrice.id && currencyPrice.amount) { pricesToCreate.push({ - amount: getDbAmount(castNumber(currencyPrice.amount), currencyCode), + amount: castNumber(currencyPrice.amount), currency_code: currencyCode, // @ts-expect-error type is wrong variant_id: variantId, diff --git a/packages/admin-next/dashboard/src/routes/products/product-create/utils.ts b/packages/admin-next/dashboard/src/routes/products/product-create/utils.ts index eb0ee003fb..d4d5e1b9bf 100644 --- a/packages/admin-next/dashboard/src/routes/products/product-create/utils.ts +++ b/packages/admin-next/dashboard/src/routes/products/product-create/utils.ts @@ -1,7 +1,6 @@ import { CreateProductDTO } from "@medusajs/types" import { ProductCreateSchemaType } from "./types" -import { getDbAmount } from "../../../lib/money-amount-helpers.ts" -import { castNumber } from "../../../lib/cast-number.ts" +import { castNumber } from "../../../lib/cast-number" export const normalizeProductFormValues = ( values: ProductCreateSchemaType & { status: CreateProductDTO["status"] } @@ -64,7 +63,7 @@ export const normalizeVariants = ( } else { return { currency_code: key, - amount: getDbAmount(castNumber(value), key), + amount: castNumber(value), } } }) diff --git a/packages/admin-next/dashboard/src/routes/products/product-prices/pricing-edit.tsx b/packages/admin-next/dashboard/src/routes/products/product-prices/pricing-edit.tsx index 8c55e7f109..89ed499569 100644 --- a/packages/admin-next/dashboard/src/routes/products/product-prices/pricing-edit.tsx +++ b/packages/admin-next/dashboard/src/routes/products/product-prices/pricing-edit.tsx @@ -7,11 +7,7 @@ import { RouteFocusModal, useRouteModal } from "../../../components/route-modal" import { useUpdateProductVariantsBatch } from "../../../hooks/api/products" import { ExtendedProductDTO } from "../../../types/api-responses" import { VariantPricingForm } from "../common/variant-pricing-form" -import { - getDbAmount, - getPresentationalAmount, -} from "../../../lib/money-amount-helpers.ts" -import { castNumber } from "../../../lib/cast-number.ts" +import { castNumber } from "../../../lib/cast-number" export const UpdateVariantPricesSchema = zod.object({ variants: zod.array( @@ -35,10 +31,7 @@ export const PricingEdit = ({ product }: { product: ExtendedProductDTO }) => { variants: product.variants.map((variant: any) => ({ title: variant.title, prices: variant.prices.reduce((acc: any, price: any) => { - acc[price.currency_code] = getPresentationalAmount( - price.amount, - price.currency_code - ) + acc[price.currency_code] = price.amount return acc }, {}), })) as any, @@ -57,7 +50,7 @@ export const PricingEdit = ({ product }: { product: ExtendedProductDTO }) => { prices: Object.entries(variant.prices || {}).map( ([key, value]: any) => ({ currency_code: key, - amount: getDbAmount(castNumber(value), key), + amount: castNumber(value), }) ), })),