fix(core-flows): handle cacluated shipping options on draft orders gracefully (#13353)
* fix(core-flows): handle calculated shipping options on draft orders gracefully * fix: throw medusa error
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/core-flows": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(core-flows): handle cacluated shipping options on draft orders gracefully
|
||||||
+34
-1
@@ -1,16 +1,25 @@
|
|||||||
import {
|
import {
|
||||||
ChangeActionType,
|
ChangeActionType,
|
||||||
|
isDefined,
|
||||||
|
MedusaError,
|
||||||
OrderChangeStatus,
|
OrderChangeStatus,
|
||||||
PromotionActions,
|
PromotionActions,
|
||||||
|
ShippingOptionPriceType,
|
||||||
} from "@medusajs/framework/utils"
|
} from "@medusajs/framework/utils"
|
||||||
import {
|
import {
|
||||||
|
createStep,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
transform,
|
transform,
|
||||||
when,
|
when,
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
WorkflowResponse,
|
WorkflowResponse,
|
||||||
} from "@medusajs/framework/workflows-sdk"
|
} 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 { useRemoteQueryStep } from "../../common"
|
||||||
import {
|
import {
|
||||||
createOrderChangeActionsWorkflow,
|
createOrderChangeActionsWorkflow,
|
||||||
@@ -23,6 +32,27 @@ import { validateDraftOrderChangeStep } from "../steps/validate-draft-order-chan
|
|||||||
import { draftOrderFieldsForRefreshSteps } from "../utils/fields"
|
import { draftOrderFieldsForRefreshSteps } from "../utils/fields"
|
||||||
import { refreshDraftOrderAdjustmentsWorkflow } from "./refresh-draft-order-adjustments"
|
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 =
|
export const addDraftOrderShippingMethodsWorkflowId =
|
||||||
"add-draft-order-shipping-methods"
|
"add-draft-order-shipping-methods"
|
||||||
|
|
||||||
@@ -100,6 +130,7 @@ export const addDraftOrderShippingMethodsWorkflow = createWorkflow(
|
|||||||
fields: [
|
fields: [
|
||||||
"id",
|
"id",
|
||||||
"name",
|
"name",
|
||||||
|
"price_type",
|
||||||
"calculated_price.calculated_amount",
|
"calculated_price.calculated_amount",
|
||||||
"calculated_price.is_calculated_price_tax_inclusive",
|
"calculated_price.is_calculated_price_tax_inclusive",
|
||||||
],
|
],
|
||||||
@@ -111,6 +142,8 @@ export const addDraftOrderShippingMethodsWorkflow = createWorkflow(
|
|||||||
},
|
},
|
||||||
}).config({ name: "fetch-shipping-option" })
|
}).config({ name: "fetch-shipping-option" })
|
||||||
|
|
||||||
|
validateShippingOptionStep({ shippingOptions, input })
|
||||||
|
|
||||||
const shippingMethodInput = transform(
|
const shippingMethodInput = transform(
|
||||||
{
|
{
|
||||||
relatedEntity: { order_id: order.id },
|
relatedEntity: { order_id: order.id },
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export function prepareShippingMethod(relatedEntityField?: string) {
|
|||||||
: option.calculated_price.calculated_amount,
|
: option.calculated_price.calculated_amount,
|
||||||
is_custom_amount: isCustomPrice,
|
is_custom_amount: isCustomPrice,
|
||||||
is_tax_inclusive:
|
is_tax_inclusive:
|
||||||
!!option.calculated_price.is_calculated_price_tax_inclusive,
|
!!option.calculated_price?.is_calculated_price_tax_inclusive,
|
||||||
data: option.data ?? {},
|
data: option.data ?? {},
|
||||||
name: option.name,
|
name: option.name,
|
||||||
version: orderChange.version,
|
version: orderChange.version,
|
||||||
|
|||||||
Reference in New Issue
Block a user