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:
Sebastian Rindom
2024-09-30 11:51:55 +00:00
committed by GitHub
parent 7484cef0fa
commit 852df3f764
8 changed files with 207 additions and 55 deletions
@@ -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,
@@ -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 = {
@@ -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
}