From a28226af80a8880afbdb926a5001f0cb0d89fdc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Mon, 14 Jul 2025 15:35:44 +0200 Subject: [PATCH] fix(core-flows): updating tax lines when draft order shipping is removed (#12919) **What** - don't call `updateOrderTaxLinesWorkflow` when a shipping method is removed from a draft order (tax lines will be cascade deleted with the method) --- .changeset/chilled-masks-shout.md | 5 + .../draft-order/admin/draft-order.spec.ts | 185 +++++++++++++++++- .../remove-draft-order-shipping-method.ts | 7 - 3 files changed, 189 insertions(+), 8 deletions(-) create mode 100644 .changeset/chilled-masks-shout.md diff --git a/.changeset/chilled-masks-shout.md b/.changeset/chilled-masks-shout.md new file mode 100644 index 0000000000..4dfc052a53 --- /dev/null +++ b/.changeset/chilled-masks-shout.md @@ -0,0 +1,5 @@ +--- +"@medusajs/core-flows": patch +--- + +fix(core-flows): updating tax lines when draft order shipping is removed diff --git a/integration-tests/http/__tests__/draft-order/admin/draft-order.spec.ts b/integration-tests/http/__tests__/draft-order/admin/draft-order.spec.ts index 5bfc0552fa..c83a2c5ff4 100644 --- a/integration-tests/http/__tests__/draft-order/admin/draft-order.spec.ts +++ b/integration-tests/http/__tests__/draft-order/admin/draft-order.spec.ts @@ -16,6 +16,7 @@ medusaIntegrationTestRunner({ let stockLocation: HttpTypes.AdminStockLocation let testDraftOrder: HttpTypes.AdminDraftOrder let shippingOption: HttpTypes.AdminShippingOption + let shippingOptionHeavy: HttpTypes.AdminShippingOption beforeEach(async () => { const container = getContainer() @@ -55,6 +56,14 @@ medusaIntegrationTestRunner({ ) ).data.shipping_profile + const shippingProfileHeavy = ( + await api.post( + `/admin/shipping-profiles`, + { name: "test shipping profile heavy", type: "heavy" }, + adminHeaders + ) + ).data.shipping_profile + const fulfillmentSets = ( await api.post( `/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`, @@ -103,7 +112,28 @@ medusaIntegrationTestRunner({ description: "Test description", code: "test-code", }, - prices: [{ currency_code: "usd", amount: 1000 }], + prices: [{ currency_code: "usd", amount: 5 }], + rules: [], + }, + adminHeaders + ) + ).data.shipping_option + + shippingOptionHeavy = ( + await api.post( + `/admin/shipping-options`, + { + name: `Test shipping option ${fulfillmentSet.id}`, + service_zone_id: fulfillmentSet.service_zones[0].id, + shipping_profile_id: shippingProfileHeavy.id, + provider_id: "manual_test-provider", + price_type: "flat", + type: { + label: "Test type", + description: "Test description", + code: "test-code", + }, + prices: [{ currency_code: "usd", amount: 10 }], rules: [], }, adminHeaders @@ -117,6 +147,13 @@ medusaIntegrationTestRunner({ email: "test@test.com", region_id: region.id, sales_channel_id: salesChannel.id, + shipping_address: { + address_1: "123 Main St", + city: "Anytown", + country_code: "US", + postal_code: "12345", + first_name: "John", + }, }, adminHeaders ) @@ -610,6 +647,152 @@ medusaIntegrationTestRunner({ expect(order.shipping_methods.length).toBe(0) }) + + it("should ensure that the shipping method is removed from the order and tax lines are updated with multiple shipping methods", async () => { + /** + * Add Heavy SO + */ + + edit = ( + await api.post( + `/admin/draft-orders/${testDraftOrder.id}/edit`, + {}, + adminHeaders + ) + ).data.draft_order_preview + + await api.post( + `/admin/draft-orders/${testDraftOrder.id}/edit/shipping-methods`, + { + shipping_option_id: shippingOptionHeavy.id, + }, + adminHeaders + ) + + edit = ( + await api.post( + `/admin/draft-orders/${testDraftOrder.id}/edit/confirm`, + {}, + adminHeaders + ) + ).data.draft_order_preview + + /** + * Tax rate -> 2% + * + * One product -> 10$ + * Shipping method 1 -> 5$ + * Shipping method 2 -> 10$ + */ + + expect(edit).toEqual( + expect.objectContaining({ + total: 25.5, + subtotal: 25, + tax_total: 0.5, + + items: [ + expect.objectContaining({ + subtotal: 10, + total: 10.2, + tax_total: 0.2, + tax_lines: [ + expect.objectContaining({ + rate: 2, + }), + ], + }), + ], + shipping_methods: expect.arrayContaining([ + expect.objectContaining({ + shipping_option_id: shippingOption.id, + amount: 5, + subtotal: 5, + total: 5.1, + tax_total: 0.1, + }), + expect.objectContaining({ + shipping_option_id: shippingOptionHeavy.id, + amount: 10, + subtotal: 10, + total: 10.2, + tax_total: 0.2, + }), + ]), + }) + ) + + /** + * Remove Heavy shipping method + */ + + edit = ( + await api.post( + `/admin/draft-orders/${testDraftOrder.id}/edit`, + {}, + adminHeaders + ) + ).data.draft_order_preview + + const response = await api.delete( + `/admin/draft-orders/${ + testDraftOrder.id + }/edit/shipping-methods/method/${ + edit.shipping_methods.find( + (sm) => sm.shipping_option_id === shippingOptionHeavy.id + ).id + }`, + adminHeaders + ) + + expect(response.status).toBe(200) + expect(response.data.draft_order_preview.shipping_methods.length).toBe( + 1 + ) + + await api.post( + `/admin/draft-orders/${testDraftOrder.id}/edit/confirm`, + {}, + adminHeaders + ) + + const order = ( + await api.get( + `/admin/draft-orders/${testDraftOrder.id}?fields=+total,+subtotal,+tax_total,+items.subtotal,+items.total,+items.tax_total,+shipping_methods.amount,+shipping_methods.subtotal,+shipping_methods.total,+shipping_methods.tax_total`, + adminHeaders + ) + ).data.draft_order + + expect(order).toEqual( + expect.objectContaining({ + total: 15.3, + subtotal: 15, + tax_total: 0.3, + + items: [ + expect.objectContaining({ + subtotal: 10, + total: 10.2, + tax_total: 0.2, + tax_lines: [ + expect.objectContaining({ + rate: 2, + }), + ], + }), + ], + shipping_methods: expect.arrayContaining([ + expect.objectContaining({ + shipping_option_id: shippingOption.id, + amount: 5, + subtotal: 5, + total: 5.1, + tax_total: 0.1, + }), + ]), + }) + ) + }) }) }, }) diff --git a/packages/core/core-flows/src/draft-order/workflows/remove-draft-order-shipping-method.ts b/packages/core/core-flows/src/draft-order/workflows/remove-draft-order-shipping-method.ts index f0e848cb37..62585d0293 100644 --- a/packages/core/core-flows/src/draft-order/workflows/remove-draft-order-shipping-method.ts +++ b/packages/core/core-flows/src/draft-order/workflows/remove-draft-order-shipping-method.ts @@ -15,7 +15,6 @@ import { useRemoteQueryStep } from "../../common" import { createOrderChangeActionsWorkflow, previewOrderChangeStep, - updateOrderTaxLinesWorkflow, } from "../../order" import { validateDraftOrderChangeStep } from "../steps/validate-draft-order-change" import { draftOrderFieldsForRefreshSteps } from "../utils/fields" @@ -87,12 +86,6 @@ export const removeDraftOrderShippingMethodWorkflow = createWorkflow( validateDraftOrderChangeStep({ order, orderChange }) - updateOrderTaxLinesWorkflow.runAsStep({ - input: { - order_id: order.id, - }, - }) - const appliedPromoCodes: string[] = transform( order, (order) => order.promotions?.map((promotion) => promotion.code) ?? []