diff --git a/.changeset/shaggy-ravens-wonder.md b/.changeset/shaggy-ravens-wonder.md new file mode 100644 index 0000000000..1271398cf3 --- /dev/null +++ b/.changeset/shaggy-ravens-wonder.md @@ -0,0 +1,5 @@ +--- +"@medusajs/core-flows": patch +--- + +fix(core-flows): Allow adding shipping methods through order edits diff --git a/integration-tests/http/__tests__/order-edits/order-edits.spec.ts b/integration-tests/http/__tests__/order-edits/order-edits.spec.ts index 7cca314c71..df0effb259 100644 --- a/integration-tests/http/__tests__/order-edits/order-edits.spec.ts +++ b/integration-tests/http/__tests__/order-edits/order-edits.spec.ts @@ -505,5 +505,78 @@ medusaIntegrationTestRunner({ expect(result[0].confirmed_by).toEqual(expect.stringContaining("user_")) }) }) + + describe("Order Edit Shipping Methods", () => { + it("should add a shipping method through an order edit", async () => { + await api.post( + "/admin/order-edits", + { order_id: order.id, description: "Test" }, + adminHeaders + ) + + const orderId = order.id + + const shippingMethodResponse = await api.post( + `/admin/order-edits/${orderId}/shipping-method`, + { shipping_option_id: shippingOption.id, custom_amount: 5 }, + adminHeaders + ) + + expect( + shippingMethodResponse.data.order_preview.shipping_methods.length + ).toEqual(2) + expect( + shippingMethodResponse.data.order_preview.shipping_methods + ).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + amount: 10, + }), + expect.objectContaining({ + amount: 5, + }), + ]) + ) + + const requestResult = await api.post( + `/admin/order-edits/${orderId}/request`, + {}, + adminHeaders + ) + + expect(requestResult.data.order_preview.order_change.status).toEqual( + OrderChangeStatus.REQUESTED + ) + + await api.post( + `/admin/order-edits/${orderId}/confirm`, + {}, + adminHeaders + ) + + const orderResult = await api.get( + `/admin/orders/${orderId}`, + adminHeaders + ) + + expect(orderResult.data.order.shipping_methods.length).toEqual(2) + expect(orderResult.data.order.shipping_methods).toEqual( + expect.arrayContaining([ + expect.objectContaining({ amount: 10 }), + expect.objectContaining({ amount: 5 }), + ]) + ) + + const orderChangesResult = await api.get( + `/admin/orders/${orderId}/changes?change_type=edit`, + adminHeaders + ) + + expect(orderChangesResult.data.order_changes.length).toEqual(1) + expect(orderChangesResult.data.order_changes[0].status).toEqual( + OrderChangeStatus.CONFIRMED + ) + }) + }) }, }) diff --git a/packages/core/core-flows/src/order/workflows/order-edit/create-order-edit-shipping-method.ts b/packages/core/core-flows/src/order/workflows/order-edit/create-order-edit-shipping-method.ts index 96ff0b51be..a98c740b55 100644 --- a/packages/core/core-flows/src/order/workflows/order-edit/create-order-edit-shipping-method.ts +++ b/packages/core/core-flows/src/order/workflows/order-edit/create-order-edit-shipping-method.ts @@ -39,14 +39,14 @@ export type CreateOrderEditShippingMethodValidationStepInput = { /** * This step validates that a shipping method can be created for an order edit. * If the order is canceled or the order change is not active, the step will throw an error. - * + * * :::note - * + * * You can retrieve an order and order change details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query), * or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep). - * + * * ::: - * + * * @example * const data = createOrderEditShippingMethodValidationStep({ * order: { @@ -94,10 +94,10 @@ export const createOrderEditShippingMethodWorkflowId = /** * This workflow creates a shipping method for an order edit. It's used by the * [Add Shipping Method API Route](https://docs.medusajs.com/api/admin#order-edits_postordereditsidshippingmethod). - * + * * You can use this workflow within your customizations or your own custom workflows, allowing you to create a shipping method * for an order edit in your in your own custom flows. - * + * * @example * const { result } = await createOrderEditShippingMethodWorkflow(container) * .run({ @@ -106,14 +106,16 @@ export const createOrderEditShippingMethodWorkflowId = * shipping_option_id: "so_123", * } * }) - * + * * @summary - * + * * Create a shipping method for an order edit. */ export const createOrderEditShippingMethodWorkflow = createWorkflow( createOrderEditShippingMethodWorkflowId, - function (input: CreateOrderEditShippingMethodWorkflowInput): WorkflowResponse { + function ( + input: CreateOrderEditShippingMethodWorkflowInput + ): WorkflowResponse { const order: OrderDTO = useRemoteQueryStep({ entry_point: "orders", fields: ["id", "status", "currency_code", "canceled_at"], @@ -152,6 +154,7 @@ export const createOrderEditShippingMethodWorkflow = createWorkflow( const shippingMethodInput = transform( { + relatedEntity: { order_id: order.id }, shippingOptions, customPrice: input.custom_amount, orderChange,