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:
@@ -8,7 +8,31 @@ import {
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
import { refetchShippingOption } from "../helpers"
|
||||
import { AdminUpdateShippingOptionType } from "../validators"
|
||||
import {
|
||||
AdminGetShippingOptionParamsType,
|
||||
AdminUpdateShippingOptionType,
|
||||
} from "../validators"
|
||||
import { MedusaError } from "@medusajs/framework/utils"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<AdminGetShippingOptionParamsType>,
|
||||
res: MedusaResponse<HttpTypes.AdminShippingOptionResponse>
|
||||
) => {
|
||||
const shippingOption = await refetchShippingOption(
|
||||
req.params.id,
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
if (!shippingOption) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Shipping Option with id: ${req.params.id} not found`
|
||||
)
|
||||
}
|
||||
|
||||
res.json({ shipping_option: shippingOption })
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminUpdateShippingOptionType>,
|
||||
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
promiseAll,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/framework/utils"
|
||||
|
||||
export const refetchShippingOption = async (
|
||||
@@ -14,17 +13,14 @@ export const refetchShippingOption = async (
|
||||
scope: MedusaContainer,
|
||||
fields: string[]
|
||||
) => {
|
||||
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "shipping_option",
|
||||
variables: {
|
||||
filters: { id: shippingOptionId },
|
||||
},
|
||||
const query = scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
const { data } = await query.graph({
|
||||
entity: "shipping_option",
|
||||
filters: { id: shippingOptionId },
|
||||
fields: fields,
|
||||
})
|
||||
|
||||
const shippingOptions = await remoteQuery(queryObject)
|
||||
return shippingOptions[0]
|
||||
return data[0]
|
||||
}
|
||||
|
||||
export const refetchBatchRules = async (
|
||||
@@ -32,32 +28,28 @@ export const refetchBatchRules = async (
|
||||
scope: MedusaContainer,
|
||||
fields: string[]
|
||||
) => {
|
||||
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
const query = scope.resolve(ContainerRegistrationKeys.QUERY)
|
||||
let created = Promise.resolve<ShippingOptionRuleDTO[]>([])
|
||||
let updated = Promise.resolve<ShippingOptionRuleDTO[]>([])
|
||||
|
||||
if (batchResult.created.length) {
|
||||
const createdQuery = remoteQueryObjectFromString({
|
||||
entryPoint: "shipping_option_rule",
|
||||
variables: {
|
||||
created = query
|
||||
.graph({
|
||||
entity: "shipping_option_rule",
|
||||
filters: { id: batchResult.created.map((p) => p.id) },
|
||||
},
|
||||
fields: fields,
|
||||
})
|
||||
|
||||
created = remoteQuery(createdQuery)
|
||||
fields: fields,
|
||||
})
|
||||
.then(({ data }) => data)
|
||||
}
|
||||
|
||||
if (batchResult.updated.length) {
|
||||
const updatedQuery = remoteQueryObjectFromString({
|
||||
entryPoint: "shipping_option_rule",
|
||||
variables: {
|
||||
updated = query
|
||||
.graph({
|
||||
entity: "shipping_option_rule",
|
||||
filters: { id: batchResult.updated.map((p) => p.id) },
|
||||
},
|
||||
fields: fields,
|
||||
})
|
||||
|
||||
updated = remoteQuery(updatedQuery)
|
||||
fields: fields,
|
||||
})
|
||||
.then(({ data }) => data)
|
||||
}
|
||||
|
||||
const [createdRes, updatedRes] = await promiseAll([created, updated])
|
||||
|
||||
@@ -35,6 +35,16 @@ export const adminShippingOptionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
}),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/shipping-options/:id",
|
||||
middlewares: [
|
||||
validateAndTransformQuery(
|
||||
AdminGetShippingOptionParams,
|
||||
retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/shipping-options",
|
||||
|
||||
Reference in New Issue
Block a user