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:
Sebastian Rindom
2024-09-29 11:52:16 +02:00
committed by GitHub
parent 17b2868a50
commit 0efbcd2344
5 changed files with 246 additions and 2 deletions
@@ -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 {
createWorkflow,
parallelize,
transform,
WorkflowData,
WorkflowResponse,
@@ -11,6 +12,7 @@ import {
} from "../steps"
import { setShippingOptionsPriceSetsStep } from "../steps/set-shipping-options-price-sets"
import { validateFulfillmentProvidersStep } from "../steps/validate-fulfillment-providers"
import { validateShippingOptionPricesStep } from "../steps/validate-shipping-option-prices"
export const createShippingOptionsWorkflowId =
"create-shipping-options-workflow"
@@ -24,7 +26,10 @@ export const createShippingOptionsWorkflow = createWorkflow(
FulfillmentWorkflow.CreateShippingOptionsWorkflowInput[]
>
): WorkflowResponse<FulfillmentWorkflow.CreateShippingOptionsWorkflowOutput> => {
validateFulfillmentProvidersStep(input)
parallelize(
validateFulfillmentProvidersStep(input),
validateShippingOptionPricesStep(input)
)
const data = transform(input, (data) => {
const shippingOptionsIndexToPrices = data.map((option, index) => {
@@ -1,6 +1,7 @@
import { FulfillmentWorkflow } from "@medusajs/framework/types"
import {
createWorkflow,
parallelize,
transform,
WorkflowData,
WorkflowResponse,
@@ -10,6 +11,7 @@ import {
upsertShippingOptionsStep,
} from "../steps"
import { validateFulfillmentProvidersStep } from "../steps/validate-fulfillment-providers"
import { validateShippingOptionPricesStep } from "../steps/validate-shipping-option-prices"
export const updateShippingOptionsWorkflowId =
"update-shipping-options-workflow"
@@ -23,7 +25,10 @@ export const updateShippingOptionsWorkflow = createWorkflow(
FulfillmentWorkflow.UpdateShippingOptionsWorkflowInput[]
>
): WorkflowResponse<FulfillmentWorkflow.UpdateShippingOptionsWorkflowOutput> => {
validateFulfillmentProvidersStep(input)
parallelize(
validateFulfillmentProvidersStep(input),
validateShippingOptionPricesStep(input)
)
const data = transform(input, (data) => {
const shippingOptionsIndexToPrices = data.map((option, index) => {