feat(core-flows, js-sdk, medusa): draft order shipping removal (#12124)

**What**
- allow removal of a shipping method

---

CLOSES CMRC-1013
This commit is contained in:
Frane Polić
2025-04-16 06:10:24 +00:00
committed by GitHub
parent a12b5f7456
commit 01542f6973
10 changed files with 382 additions and 11 deletions
@@ -149,7 +149,7 @@ export const addDraftOrderShippingMethodsWorkflow = createWorkflow(
order,
shippingOptions,
createdMethods,
customPrice: input.custom_amount as any, // Need to cast this to any otherwise the type becomes to complex.
customPrice: input.custom_amount as any, // Need to cast this to any otherwise the type becomes too complex.
orderChange,
},
({
@@ -6,7 +6,7 @@ import {
} from "@medusajs/framework/workflows-sdk"
import { OrderDTO, OrderWorkflow } from "@medusajs/types"
import { useRemoteQueryStep } from "../../common"
import { createOrderChangeStep } from "../../order"
import { createOrderChangeStep, previewOrderChangeStep } from "../../order"
import { validateDraftOrderStep } from "../steps"
export const beginDraftOrderEditWorkflowId = "begin-draft-order-edit"
@@ -34,6 +34,8 @@ export const beginDraftOrderEditWorkflow = createWorkflow(
}
})
return new WorkflowResponse(createOrderChangeStep(orderChangeInput))
createOrderChangeStep(orderChangeInput)
return new WorkflowResponse(previewOrderChangeStep(input.order_id))
}
)
@@ -54,7 +54,7 @@ export const cancelDraftOrderEditWorkflow = createWorkflow(
({ orderChange }) => {
return (orderChange.actions ?? [])
.filter((a) => a.action === ChangeActionType.SHIPPING_ADD)
.map(({ id }) => id)
.map(({ reference_id }) => reference_id)
}
)
@@ -14,3 +14,4 @@ export * from "./update-draft-order-action-item"
export * from "./update-draft-order-action-shipping-method"
export * from "./update-draft-order-item"
export * from "./update-draft-order-shipping-method"
export * from "./remove-draft-order-shipping-method"
@@ -0,0 +1,130 @@
import {
ChangeActionType,
OrderChangeStatus,
PromotionActions,
} from "@medusajs/framework/utils"
import {
createWorkflow,
transform,
when,
WorkflowData,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { OrderChangeDTO, OrderDTO } from "@medusajs/types"
import { useRemoteQueryStep } from "../../common"
import {
createOrderChangeActionsWorkflow,
previewOrderChangeStep,
updateOrderTaxLinesWorkflow,
} from "../../order"
import { validateDraftOrderChangeStep } from "../steps/validate-draft-order-change"
import { draftOrderFieldsForRefreshSteps } from "../utils/fields"
import { refreshDraftOrderAdjustmentsWorkflow } from "./refresh-draft-order-adjustments"
export const removeDraftOrderShippingMethodWorkflowId =
"remove-draft-order-shipping-method"
interface RemoveDraftOrderShippingMethodWorkflowInput {
/**
* The ID of the draft order to add the shipping methods to.
*/
order_id: string
/**
* The ID of the shipping method to remove.
*/
shipping_method_id: string
}
export const removeDraftOrderShippingMethodWorkflow = createWorkflow(
removeDraftOrderShippingMethodWorkflowId,
function (input: WorkflowData<RemoveDraftOrderShippingMethodWorkflowInput>) {
const order: OrderDTO = useRemoteQueryStep({
entry_point: "orders",
fields: draftOrderFieldsForRefreshSteps,
variables: { id: input.order_id },
list: false,
throw_if_key_not_found: true,
}).config({ name: "order-query" })
const orderChange: OrderChangeDTO = useRemoteQueryStep({
entry_point: "order_change",
fields: ["id", "status", "version"],
variables: {
filters: {
order_id: input.order_id,
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
},
},
list: false,
}).config({ name: "order-change-query" })
validateDraftOrderChangeStep({ order, orderChange })
updateOrderTaxLinesWorkflow.runAsStep({
input: {
order_id: order.id,
},
})
const appliedPromoCodes = transform(order, (order) => {
const promotionLink = (order as any).promotion_link
if (!promotionLink) {
return []
}
if (Array.isArray(promotionLink)) {
return promotionLink.map((promo) => promo.promotion.code)
}
return [promotionLink.promotion.code]
})
// If any the order has any promo codes, then we need to refresh the adjustments.
when(
appliedPromoCodes,
(appliedPromoCodes) => appliedPromoCodes.length > 0
).then(() => {
const refetchedOrder = useRemoteQueryStep({
entry_point: "orders",
fields: draftOrderFieldsForRefreshSteps,
variables: { id: input.order_id },
list: false,
throw_if_key_not_found: true,
}).config({ name: "refetched-order-query" })
refreshDraftOrderAdjustmentsWorkflow.runAsStep({
input: {
order: refetchedOrder,
promo_codes: appliedPromoCodes,
action: PromotionActions.REPLACE,
},
})
})
const orderChangeActionInput = transform(
{
input,
order,
orderChange,
},
({ order, orderChange, input }) => {
return [
{
action: ChangeActionType.SHIPPING_REMOVE,
reference: "order_shipping_method",
order_change_id: orderChange.id,
reference_id: input.shipping_method_id,
order_id: order.id,
},
]
}
)
createOrderChangeActionsWorkflow.runAsStep({
input: orderChangeActionInput,
})
return new WorkflowResponse(previewOrderChangeStep(order.id))
}
)
@@ -14,12 +14,12 @@ export const createOrderChangeActionsWorkflowId = "create-order-change-actions"
/**
* This workflow creates order change actions. It's used by other order-related workflows,
* such as {@link requestItemReturnWorkflow} to create an order change action based on changes made to the order.
*
* You can use this workflow within your customizations or your own custom workflows, allowing you to wrap custom logic around
*
* You can use this workflow within your customizations or your own custom workflows, allowing you to wrap custom logic around
* creating an order change action.
*
*
* @summary
*
*
* Create an order change action.
*/
export const createOrderChangeActionsWorkflow = createWorkflow(