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:
5
.changeset/hot-impalas-retire.md
Normal file
5
.changeset/hot-impalas-retire.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/core-flows": patch
|
||||
---
|
||||
|
||||
fix(core-flows): handle cacluated shipping options on draft orders gracefully
|
||||
@@ -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 },
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user