fix(medusa,dashboard): don't send price updates for deleted regions/currencies (#9361)
**What** - Fixes an issue where the admin dashboard would send region prices for deleted regions. - Also, includes the implementation for `GET /admin/shipping-options/:id` and its corresponding SDK function. **Why** - When a region price for a deleted region was sent to the backend it would result in the insert hitting a not null constraint on the currency_code for prices. To avoid this the dashboard should not send region prices for deleted regions. **Additional context** - Prices for deleted regions should ideally not be returned when fetching shipping option prices. However, we don't yet have a mechanism for cleaning up region prices after a region is deleted. Fixes CC-540
This commit is contained in:
@@ -18,24 +18,24 @@ export const shippingOptionsQueryKeys = queryKeysFactory(
|
||||
SHIPPING_OPTIONS_QUERY_KEY
|
||||
)
|
||||
|
||||
// TODO: Endpoint is not implemented yet
|
||||
// export const useShippingOption = (
|
||||
// id: string,
|
||||
// options?: UseQueryOptions<
|
||||
// HttpTypes.AdminShippingOptionResponse,
|
||||
// Error,
|
||||
// HttpTypes.AdminShippingOptionResponse,
|
||||
// QueryKey
|
||||
// >
|
||||
// ) => {
|
||||
// const { data, ...rest } = useQuery({
|
||||
// queryFn: () => sdk.admin.shippingOption.retrieve(id),
|
||||
// queryKey: shippingOptionsQueryKeys.retrieve(id),
|
||||
// ...options,
|
||||
// })
|
||||
export const useShippingOption = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
options?: UseQueryOptions<
|
||||
HttpTypes.AdminShippingOptionResponse,
|
||||
Error,
|
||||
HttpTypes.AdminShippingOptionResponse,
|
||||
QueryKey
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => sdk.admin.shippingOption.retrieve(id, query),
|
||||
queryKey: shippingOptionsQueryKeys.detail(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
// return { ...data, ...rest }
|
||||
// }
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useShippingOptions = (
|
||||
query?: HttpTypes.AdminShippingOptionListParams,
|
||||
|
||||
+13
@@ -129,6 +129,11 @@ export function EditShippingOptionsPricingForm({
|
||||
return undefined
|
||||
}
|
||||
|
||||
const currencyExists = currencies.some(currencyCode => currencyCode.toLowerCase() == code.toLowerCase())
|
||||
if (!currencyExists) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const amount = castNumber(value)
|
||||
|
||||
const priceRecord: PriceRecord = {
|
||||
@@ -155,6 +160,14 @@ export function EditShippingOptionsPricingForm({
|
||||
return undefined
|
||||
}
|
||||
|
||||
// Check if the region_id exists in the regions array to avoid
|
||||
// sending updates of region prices where the region has been
|
||||
// deleted
|
||||
const regionExists = regions?.some(region => region.id === region_id)
|
||||
if (!regionExists) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const amount = castNumber(value)
|
||||
|
||||
const priceRecord: PriceRecord = {
|
||||
|
||||
+10
-11
@@ -1,25 +1,24 @@
|
||||
import { json, useParams } from "react-router-dom"
|
||||
|
||||
import { RouteFocusModal } from "../../../components/modals"
|
||||
import { useShippingOptions } from "../../../hooks/api/shipping-options"
|
||||
import { useShippingOption } from "../../../hooks/api/shipping-options"
|
||||
import { EditShippingOptionsPricingForm } from "./components/create-shipping-options-form"
|
||||
|
||||
export function LocationServiceZoneShippingOptionPricing() {
|
||||
const { so_id, location_id } = useParams()
|
||||
|
||||
const { shipping_options, isPending, isFetching, isError, error } =
|
||||
useShippingOptions({
|
||||
// TODO: change this when GET option by id endpoint is implemented
|
||||
id: [so_id!],
|
||||
if (!so_id) {
|
||||
throw json({
|
||||
message: "Shipping Option ID paramater is missing",
|
||||
status: 404,
|
||||
})
|
||||
}
|
||||
|
||||
const { shipping_option: shippingOption, isError, error } =
|
||||
useShippingOption(so_id, {
|
||||
fields: "*prices,*prices.price_rules",
|
||||
})
|
||||
|
||||
const shippingOption = shipping_options?.find((so) => so.id === so_id)
|
||||
|
||||
if (!isPending && !isFetching && !shippingOption) {
|
||||
throw json(`Shipping option with id: ${so_id} not found`, { status: 404 })
|
||||
}
|
||||
|
||||
if (isError) {
|
||||
throw error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user