Skip to content

Commit a28226a

Browse files
authored
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)
1 parent e547c65 commit a28226a

File tree

3 files changed

+189
-8
lines changed

3 files changed

+189
-8
lines changed

.changeset/chilled-masks-shout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/core-flows": patch
3+
---
4+
5+
fix(core-flows): updating tax lines when draft order shipping is removed

integration-tests/http/__tests__/draft-order/admin/draft-order.spec.ts

Lines changed: 184 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ medusaIntegrationTestRunner({
1616
let stockLocation: HttpTypes.AdminStockLocation
1717
let testDraftOrder: HttpTypes.AdminDraftOrder
1818
let shippingOption: HttpTypes.AdminShippingOption
19+
let shippingOptionHeavy: HttpTypes.AdminShippingOption
1920

2021
beforeEach(async () => {
2122
const container = getContainer()
@@ -55,6 +56,14 @@ medusaIntegrationTestRunner({
5556
)
5657
).data.shipping_profile
5758

59+
const shippingProfileHeavy = (
60+
await api.post(
61+
`/admin/shipping-profiles`,
62+
{ name: "test shipping profile heavy", type: "heavy" },
63+
adminHeaders
64+
)
65+
).data.shipping_profile
66+
5867
const fulfillmentSets = (
5968
await api.post(
6069
`/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
@@ -103,7 +112,28 @@ medusaIntegrationTestRunner({
103112
description: "Test description",
104113
code: "test-code",
105114
},
106-
prices: [{ currency_code: "usd", amount: 1000 }],
115+
prices: [{ currency_code: "usd", amount: 5 }],
116+
rules: [],
117+
},
118+
adminHeaders
119+
)
120+
).data.shipping_option
121+
122+
shippingOptionHeavy = (
123+
await api.post(
124+
`/admin/shipping-options`,
125+
{
126+
name: `Test shipping option ${fulfillmentSet.id}`,
127+
service_zone_id: fulfillmentSet.service_zones[0].id,
128+
shipping_profile_id: shippingProfileHeavy.id,
129+
provider_id: "manual_test-provider",
130+
price_type: "flat",
131+
type: {
132+
label: "Test type",
133+
description: "Test description",
134+
code: "test-code",
135+
},
136+
prices: [{ currency_code: "usd", amount: 10 }],
107137
rules: [],
108138
},
109139
adminHeaders
@@ -117,6 +147,13 @@ medusaIntegrationTestRunner({
117147
118148
region_id: region.id,
119149
sales_channel_id: salesChannel.id,
150+
shipping_address: {
151+
address_1: "123 Main St",
152+
city: "Anytown",
153+
country_code: "US",
154+
postal_code: "12345",
155+
first_name: "John",
156+
},
120157
},
121158
adminHeaders
122159
)
@@ -610,6 +647,152 @@ medusaIntegrationTestRunner({
610647

611648
expect(order.shipping_methods.length).toBe(0)
612649
})
650+
651+
it("should ensure that the shipping method is removed from the order and tax lines are updated with multiple shipping methods", async () => {
652+
/**
653+
* Add Heavy SO
654+
*/
655+
656+
edit = (
657+
await api.post(
658+
`/admin/draft-orders/${testDraftOrder.id}/edit`,
659+
{},
660+
adminHeaders
661+
)
662+
).data.draft_order_preview
663+
664+
await api.post(
665+
`/admin/draft-orders/${testDraftOrder.id}/edit/shipping-methods`,
666+
{
667+
shipping_option_id: shippingOptionHeavy.id,
668+
},
669+
adminHeaders
670+
)
671+
672+
edit = (
673+
await api.post(
674+
`/admin/draft-orders/${testDraftOrder.id}/edit/confirm`,
675+
{},
676+
adminHeaders
677+
)
678+
).data.draft_order_preview
679+
680+
/**
681+
* Tax rate -> 2%
682+
*
683+
* One product -> 10$
684+
* Shipping method 1 -> 5$
685+
* Shipping method 2 -> 10$
686+
*/
687+
688+
expect(edit).toEqual(
689+
expect.objectContaining({
690+
total: 25.5,
691+
subtotal: 25,
692+
tax_total: 0.5,
693+
694+
items: [
695+
expect.objectContaining({
696+
subtotal: 10,
697+
total: 10.2,
698+
tax_total: 0.2,
699+
tax_lines: [
700+
expect.objectContaining({
701+
rate: 2,
702+
}),
703+
],
704+
}),
705+
],
706+
shipping_methods: expect.arrayContaining([
707+
expect.objectContaining({
708+
shipping_option_id: shippingOption.id,
709+
amount: 5,
710+
subtotal: 5,
711+
total: 5.1,
712+
tax_total: 0.1,
713+
}),
714+
expect.objectContaining({
715+
shipping_option_id: shippingOptionHeavy.id,
716+
amount: 10,
717+
subtotal: 10,
718+
total: 10.2,
719+
tax_total: 0.2,
720+
}),
721+
]),
722+
})
723+
)
724+
725+
/**
726+
* Remove Heavy shipping method
727+
*/
728+
729+
edit = (
730+
await api.post(
731+
`/admin/draft-orders/${testDraftOrder.id}/edit`,
732+
{},
733+
adminHeaders
734+
)
735+
).data.draft_order_preview
736+
737+
const response = await api.delete(
738+
`/admin/draft-orders/${
739+
testDraftOrder.id
740+
}/edit/shipping-methods/method/${
741+
edit.shipping_methods.find(
742+
(sm) => sm.shipping_option_id === shippingOptionHeavy.id
743+
).id
744+
}`,
745+
adminHeaders
746+
)
747+
748+
expect(response.status).toBe(200)
749+
expect(response.data.draft_order_preview.shipping_methods.length).toBe(
750+
1
751+
)
752+
753+
await api.post(
754+
`/admin/draft-orders/${testDraftOrder.id}/edit/confirm`,
755+
{},
756+
adminHeaders
757+
)
758+
759+
const order = (
760+
await api.get(
761+
`/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`,
762+
adminHeaders
763+
)
764+
).data.draft_order
765+
766+
expect(order).toEqual(
767+
expect.objectContaining({
768+
total: 15.3,
769+
subtotal: 15,
770+
tax_total: 0.3,
771+
772+
items: [
773+
expect.objectContaining({
774+
subtotal: 10,
775+
total: 10.2,
776+
tax_total: 0.2,
777+
tax_lines: [
778+
expect.objectContaining({
779+
rate: 2,
780+
}),
781+
],
782+
}),
783+
],
784+
shipping_methods: expect.arrayContaining([
785+
expect.objectContaining({
786+
shipping_option_id: shippingOption.id,
787+
amount: 5,
788+
subtotal: 5,
789+
total: 5.1,
790+
tax_total: 0.1,
791+
}),
792+
]),
793+
})
794+
)
795+
})
613796
})
614797
},
615798
})

packages/core/core-flows/src/draft-order/workflows/remove-draft-order-shipping-method.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { useRemoteQueryStep } from "../../common"
1515
import {
1616
createOrderChangeActionsWorkflow,
1717
previewOrderChangeStep,
18-
updateOrderTaxLinesWorkflow,
1918
} from "../../order"
2019
import { validateDraftOrderChangeStep } from "../steps/validate-draft-order-change"
2120
import { draftOrderFieldsForRefreshSteps } from "../utils/fields"
@@ -87,12 +86,6 @@ export const removeDraftOrderShippingMethodWorkflow = createWorkflow(
8786

8887
validateDraftOrderChangeStep({ order, orderChange })
8988

90-
updateOrderTaxLinesWorkflow.runAsStep({
91-
input: {
92-
order_id: order.id,
93-
},
94-
})
95-
9689
const appliedPromoCodes: string[] = transform(
9790
order,
9891
(order) => order.promotions?.map((promotion) => promotion.code) ?? []

0 commit comments

Comments
 (0)