chore: remove pricing conversions on admin (#7665)
This commit is contained in:
@@ -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,
|
||||
})
|
||||
|
||||
+3
-5
@@ -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,
|
||||
|
||||
+4
-13
@@ -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<string, number> = {}
|
||||
@@ -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,
|
||||
|
||||
+5
-12
@@ -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<string, unknown>
|
||||
)
|
||||
const values = fields.reduce((acc, key) => {
|
||||
acc[key] = form.getValues(key)
|
||||
return acc
|
||||
}, {} as Record<string, unknown>)
|
||||
|
||||
const validationResult = schema.safeParse(values)
|
||||
|
||||
|
||||
+4
-14
@@ -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,
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -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),
|
||||
})
|
||||
),
|
||||
})),
|
||||
|
||||
Reference in New Issue
Block a user