fix: validate regions exist for shipping option price update (#9364)
**What** - Adds a step to `updateShippingOptionsWorkflow` and `createShippingOptionsWorkflow` that validates if the region prices being updated have corresponding regions configured. **Why** - Previously, if you tried to send a region price update for a region that had been deleted the backend would throw an error when attempting to insert the region price. The error comes from a not-null constraint in the db, but it is better to validate that the regions we are trying to create prices for exist. Fixes CC-542
This commit is contained in:
+55
@@ -204,6 +204,61 @@ medusaIntegrationTestRunner({
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should fail with invalid region price", async () => {
|
||||||
|
const regionService = container.resolve(
|
||||||
|
Modules.REGION
|
||||||
|
) as IRegionModuleService
|
||||||
|
|
||||||
|
const [region] = await regionService.createRegions([
|
||||||
|
{
|
||||||
|
name: "Test region",
|
||||||
|
currency_code: "eur",
|
||||||
|
countries: ["fr"],
|
||||||
|
},
|
||||||
|
])
|
||||||
|
await regionService.softDeleteRegions([region.id])
|
||||||
|
|
||||||
|
const shippingOptionData: FulfillmentWorkflow.CreateShippingOptionsWorkflowInput =
|
||||||
|
{
|
||||||
|
name: "Test shipping option",
|
||||||
|
price_type: "flat",
|
||||||
|
service_zone_id: serviceZone.id,
|
||||||
|
shipping_profile_id: shippingProfile.id,
|
||||||
|
provider_id,
|
||||||
|
type: {
|
||||||
|
code: "manual-type",
|
||||||
|
label: "Manual Type",
|
||||||
|
description: "Manual Type Description",
|
||||||
|
},
|
||||||
|
prices: [
|
||||||
|
{
|
||||||
|
currency_code: "usd",
|
||||||
|
amount: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
region_id: region.id,
|
||||||
|
amount: 100,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
attribute: "total",
|
||||||
|
operator: RuleOperator.EQ,
|
||||||
|
value: "100",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
const { errors } = await createShippingOptionsWorkflow(container).run({
|
||||||
|
input: [shippingOptionData],
|
||||||
|
throwOnError: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(errors[0].error.message).toEqual(
|
||||||
|
`Cannot create prices for non-existent regions. Region with ids [${region.id}] were not found.`
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
it("should revert the shipping options and prices", async () => {
|
it("should revert the shipping options and prices", async () => {
|
||||||
const regionService = container.resolve(
|
const regionService = container.resolve(
|
||||||
Modules.REGION
|
Modules.REGION
|
||||||
|
|||||||
+127
@@ -257,6 +257,133 @@ medusaIntegrationTestRunner({
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should fail on non-existent region update shipping options", async () => {
|
||||||
|
const regionService = container.resolve(
|
||||||
|
Modules.REGION
|
||||||
|
) as IRegionModuleService
|
||||||
|
|
||||||
|
const [region] = await regionService.createRegions([
|
||||||
|
{
|
||||||
|
name: "Test region",
|
||||||
|
currency_code: "eur",
|
||||||
|
countries: ["fr"],
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
const shippingOptionData: FulfillmentWorkflow.CreateShippingOptionsWorkflowInput =
|
||||||
|
{
|
||||||
|
name: "Test shipping option",
|
||||||
|
price_type: "flat",
|
||||||
|
service_zone_id: serviceZone.id,
|
||||||
|
shipping_profile_id: shippingProfile.id,
|
||||||
|
provider_id,
|
||||||
|
type: {
|
||||||
|
code: "manual-type",
|
||||||
|
label: "Manual Type",
|
||||||
|
description: "Manual Type Description",
|
||||||
|
},
|
||||||
|
prices: [
|
||||||
|
{
|
||||||
|
currency_code: "usd",
|
||||||
|
amount: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
region_id: region.id,
|
||||||
|
amount: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
currency_code: "dkk",
|
||||||
|
amount: 1000,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
attribute: "total",
|
||||||
|
operator: RuleOperator.EQ,
|
||||||
|
value: "100",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
const { result } = await createShippingOptionsWorkflow(container).run({
|
||||||
|
input: [shippingOptionData],
|
||||||
|
})
|
||||||
|
|
||||||
|
const remoteQuery = container.resolve(
|
||||||
|
ContainerRegistrationKeys.REMOTE_QUERY
|
||||||
|
)
|
||||||
|
|
||||||
|
let remoteQueryObject = remoteQueryObjectFromString({
|
||||||
|
entryPoint: "shipping_option",
|
||||||
|
variables: {
|
||||||
|
id: result[0].id,
|
||||||
|
},
|
||||||
|
fields: [
|
||||||
|
"id",
|
||||||
|
"name",
|
||||||
|
"price_type",
|
||||||
|
"service_zone_id",
|
||||||
|
"shipping_profile_id",
|
||||||
|
"provider_id",
|
||||||
|
"data",
|
||||||
|
"metadata",
|
||||||
|
"type.*",
|
||||||
|
"created_at",
|
||||||
|
"updated_at",
|
||||||
|
"deleted_at",
|
||||||
|
"shipping_option_type_id",
|
||||||
|
"prices.*",
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
const [createdShippingOption] = await remoteQuery(remoteQueryObject)
|
||||||
|
|
||||||
|
const usdPrice = createdShippingOption.prices.find((price) => {
|
||||||
|
return price.currency_code === "usd"
|
||||||
|
})
|
||||||
|
|
||||||
|
const dkkPrice = createdShippingOption.prices.find((price) => {
|
||||||
|
return price.currency_code === "dkk"
|
||||||
|
})
|
||||||
|
|
||||||
|
const updateData: UpdateShippingOptionsWorkflowInput = {
|
||||||
|
id: createdShippingOption.id,
|
||||||
|
name: "Test shipping option",
|
||||||
|
price_type: "flat",
|
||||||
|
type: {
|
||||||
|
code: "manual-type",
|
||||||
|
label: "Manual Type",
|
||||||
|
description: "Manual Type Description",
|
||||||
|
},
|
||||||
|
prices: [
|
||||||
|
// We keep the usd price as is
|
||||||
|
// update the dkk price to 100
|
||||||
|
// delete the third price eur
|
||||||
|
// create a new eur one instead
|
||||||
|
usdPrice,
|
||||||
|
{
|
||||||
|
...dkkPrice,
|
||||||
|
amount: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
region_id: region.id,
|
||||||
|
amount: 1000,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
await regionService.softDeleteRegions([region.id])
|
||||||
|
|
||||||
|
const { errors } = await updateShippingOptionsWorkflow(container).run({
|
||||||
|
input: [updateData],
|
||||||
|
throwOnError: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(errors[0].error.message).toEqual(
|
||||||
|
`Cannot create prices for non-existent regions. Region with ids [${region.id}] were not found.`
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
it("should revert the shipping options", async () => {
|
it("should revert the shipping options", async () => {
|
||||||
const regionService = container.resolve(
|
const regionService = container.resolve(
|
||||||
Modules.REGION
|
Modules.REGION
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import { FulfillmentWorkflow } from "@medusajs/framework/types"
|
||||||
|
import { MedusaError, Modules } from "@medusajs/framework/utils"
|
||||||
|
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
|
||||||
|
|
||||||
|
export const validateShippingOptionPricesStepId =
|
||||||
|
"validate-shipping-option-prices"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate that regions exist for the shipping option prices.
|
||||||
|
*/
|
||||||
|
export const validateShippingOptionPricesStep = createStep(
|
||||||
|
validateShippingOptionPricesStepId,
|
||||||
|
async (
|
||||||
|
options: {
|
||||||
|
prices?: FulfillmentWorkflow.UpdateShippingOptionsWorkflowInput["prices"]
|
||||||
|
}[],
|
||||||
|
{ container }
|
||||||
|
) => {
|
||||||
|
const allPrices = options.flatMap((option) => option.prices ?? [])
|
||||||
|
|
||||||
|
const regionIdSet = new Set<string>()
|
||||||
|
|
||||||
|
allPrices.forEach((price) => {
|
||||||
|
if ("region_id" in price && price.region_id) {
|
||||||
|
regionIdSet.add(price.region_id)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (regionIdSet.size === 0) {
|
||||||
|
return new StepResponse(void 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
const regionService = container.resolve(Modules.REGION)
|
||||||
|
const regionList = await regionService.listRegions({
|
||||||
|
id: Array.from(regionIdSet),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (regionList.length !== regionIdSet.size) {
|
||||||
|
const missingRegions = Array.from(regionIdSet).filter(
|
||||||
|
(id) => !regionList.some((region) => region.id === id)
|
||||||
|
)
|
||||||
|
throw new MedusaError(
|
||||||
|
MedusaError.Types.INVALID_DATA,
|
||||||
|
`Cannot create prices for non-existent regions. Region with ids [${missingRegions.join(
|
||||||
|
", "
|
||||||
|
)}] were not found.`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return new StepResponse(void 0)
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { FulfillmentWorkflow } from "@medusajs/framework/types"
|
import { FulfillmentWorkflow } from "@medusajs/framework/types"
|
||||||
import {
|
import {
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
|
parallelize,
|
||||||
transform,
|
transform,
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
WorkflowResponse,
|
WorkflowResponse,
|
||||||
@@ -11,6 +12,7 @@ import {
|
|||||||
} from "../steps"
|
} from "../steps"
|
||||||
import { setShippingOptionsPriceSetsStep } from "../steps/set-shipping-options-price-sets"
|
import { setShippingOptionsPriceSetsStep } from "../steps/set-shipping-options-price-sets"
|
||||||
import { validateFulfillmentProvidersStep } from "../steps/validate-fulfillment-providers"
|
import { validateFulfillmentProvidersStep } from "../steps/validate-fulfillment-providers"
|
||||||
|
import { validateShippingOptionPricesStep } from "../steps/validate-shipping-option-prices"
|
||||||
|
|
||||||
export const createShippingOptionsWorkflowId =
|
export const createShippingOptionsWorkflowId =
|
||||||
"create-shipping-options-workflow"
|
"create-shipping-options-workflow"
|
||||||
@@ -24,7 +26,10 @@ export const createShippingOptionsWorkflow = createWorkflow(
|
|||||||
FulfillmentWorkflow.CreateShippingOptionsWorkflowInput[]
|
FulfillmentWorkflow.CreateShippingOptionsWorkflowInput[]
|
||||||
>
|
>
|
||||||
): WorkflowResponse<FulfillmentWorkflow.CreateShippingOptionsWorkflowOutput> => {
|
): WorkflowResponse<FulfillmentWorkflow.CreateShippingOptionsWorkflowOutput> => {
|
||||||
validateFulfillmentProvidersStep(input)
|
parallelize(
|
||||||
|
validateFulfillmentProvidersStep(input),
|
||||||
|
validateShippingOptionPricesStep(input)
|
||||||
|
)
|
||||||
|
|
||||||
const data = transform(input, (data) => {
|
const data = transform(input, (data) => {
|
||||||
const shippingOptionsIndexToPrices = data.map((option, index) => {
|
const shippingOptionsIndexToPrices = data.map((option, index) => {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { FulfillmentWorkflow } from "@medusajs/framework/types"
|
import { FulfillmentWorkflow } from "@medusajs/framework/types"
|
||||||
import {
|
import {
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
|
parallelize,
|
||||||
transform,
|
transform,
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
WorkflowResponse,
|
WorkflowResponse,
|
||||||
@@ -10,6 +11,7 @@ import {
|
|||||||
upsertShippingOptionsStep,
|
upsertShippingOptionsStep,
|
||||||
} from "../steps"
|
} from "../steps"
|
||||||
import { validateFulfillmentProvidersStep } from "../steps/validate-fulfillment-providers"
|
import { validateFulfillmentProvidersStep } from "../steps/validate-fulfillment-providers"
|
||||||
|
import { validateShippingOptionPricesStep } from "../steps/validate-shipping-option-prices"
|
||||||
|
|
||||||
export const updateShippingOptionsWorkflowId =
|
export const updateShippingOptionsWorkflowId =
|
||||||
"update-shipping-options-workflow"
|
"update-shipping-options-workflow"
|
||||||
@@ -23,7 +25,10 @@ export const updateShippingOptionsWorkflow = createWorkflow(
|
|||||||
FulfillmentWorkflow.UpdateShippingOptionsWorkflowInput[]
|
FulfillmentWorkflow.UpdateShippingOptionsWorkflowInput[]
|
||||||
>
|
>
|
||||||
): WorkflowResponse<FulfillmentWorkflow.UpdateShippingOptionsWorkflowOutput> => {
|
): WorkflowResponse<FulfillmentWorkflow.UpdateShippingOptionsWorkflowOutput> => {
|
||||||
validateFulfillmentProvidersStep(input)
|
parallelize(
|
||||||
|
validateFulfillmentProvidersStep(input),
|
||||||
|
validateShippingOptionPricesStep(input)
|
||||||
|
)
|
||||||
|
|
||||||
const data = transform(input, (data) => {
|
const data = transform(input, (data) => {
|
||||||
const shippingOptionsIndexToPrices = data.map((option, index) => {
|
const shippingOptionsIndexToPrices = data.map((option, index) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user