diff --git a/.changeset/hot-impalas-retire.md b/.changeset/hot-impalas-retire.md new file mode 100644 index 0000000000..547d142c32 --- /dev/null +++ b/.changeset/hot-impalas-retire.md @@ -0,0 +1,5 @@ +--- +"@medusajs/core-flows": patch +--- + +fix(core-flows): handle cacluated shipping options on draft orders gracefully diff --git a/packages/core/core-flows/src/draft-order/workflows/add-draft-order-shipping-methods.ts b/packages/core/core-flows/src/draft-order/workflows/add-draft-order-shipping-methods.ts index 0e0377a800..dbc8f6fe51 100644 --- a/packages/core/core-flows/src/draft-order/workflows/add-draft-order-shipping-methods.ts +++ b/packages/core/core-flows/src/draft-order/workflows/add-draft-order-shipping-methods.ts @@ -1,16 +1,25 @@ import { ChangeActionType, + isDefined, + MedusaError, OrderChangeStatus, PromotionActions, + ShippingOptionPriceType, } from "@medusajs/framework/utils" import { + createStep, createWorkflow, transform, when, WorkflowData, WorkflowResponse, } from "@medusajs/framework/workflows-sdk" -import { BigNumberInput, OrderChangeDTO, OrderDTO } from "@medusajs/types" +import { + BigNumberInput, + OrderChangeDTO, + OrderDTO, + ShippingOptionDTO, +} from "@medusajs/types" import { useRemoteQueryStep } from "../../common" import { createOrderChangeActionsWorkflow, @@ -23,6 +32,27 @@ import { validateDraftOrderChangeStep } from "../steps/validate-draft-order-chan import { draftOrderFieldsForRefreshSteps } from "../utils/fields" import { refreshDraftOrderAdjustmentsWorkflow } from "./refresh-draft-order-adjustments" +const validateShippingOptionStep = createStep( + "validate-shipping-option", + async (data: { + shippingOptions: ShippingOptionDTO[] + input: AddDraftOrderShippingMethodsWorkflowInput + }) => { + const shippingOption = data.shippingOptions[0] + const customAmount = data.input.custom_amount + + if ( + shippingOption.price_type === ShippingOptionPriceType.CALCULATED && + !isDefined(customAmount) + ) { + throw new MedusaError( + MedusaError.Types.INVALID_DATA, + "Calculated shipping options are not currently supported on draft orders without a custom amount." + ) + } + } +) + export const addDraftOrderShippingMethodsWorkflowId = "add-draft-order-shipping-methods" @@ -100,6 +130,7 @@ export const addDraftOrderShippingMethodsWorkflow = createWorkflow( fields: [ "id", "name", + "price_type", "calculated_price.calculated_amount", "calculated_price.is_calculated_price_tax_inclusive", ], @@ -111,6 +142,8 @@ export const addDraftOrderShippingMethodsWorkflow = createWorkflow( }, }).config({ name: "fetch-shipping-option" }) + validateShippingOptionStep({ shippingOptions, input }) + const shippingMethodInput = transform( { relatedEntity: { order_id: order.id }, diff --git a/packages/core/core-flows/src/order/utils/prepare-shipping-method.ts b/packages/core/core-flows/src/order/utils/prepare-shipping-method.ts index fc62280f8d..275c459488 100644 --- a/packages/core/core-flows/src/order/utils/prepare-shipping-method.ts +++ b/packages/core/core-flows/src/order/utils/prepare-shipping-method.ts @@ -14,7 +14,7 @@ export function prepareShippingMethod(relatedEntityField?: string) { : option.calculated_price.calculated_amount, is_custom_amount: isCustomPrice, is_tax_inclusive: - !!option.calculated_price.is_calculated_price_tax_inclusive, + !!option.calculated_price?.is_calculated_price_tax_inclusive, data: option.data ?? {}, name: option.name, version: orderChange.version,