Skip to content

Commit f4ae615

Browse files
authored
fix(tax): Use correct rule reference for shipping (#13195)
* fix(tax): Use correct rule reference * fix tests
1 parent 356b286 commit f4ae615

File tree

3 files changed

+122
-4
lines changed

3 files changed

+122
-4
lines changed

integration-tests/http/__tests__/cart/store/cart.spec.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ medusaIntegrationTestRunner({
4545
cart,
4646
customer,
4747
promotion,
48-
shippingProfile
48+
shippingProfile,
49+
taxSeedData
4950

5051
beforeAll(async () => {
5152
appContainer = getContainer()
@@ -78,7 +79,7 @@ medusaIntegrationTestRunner({
7879
)
7980
).data.shipping_profile
8081

81-
await setupTaxStructure(appContainer.resolve(Modules.TAX))
82+
taxSeedData = await setupTaxStructure(appContainer.resolve(Modules.TAX))
8283

8384
region = (
8485
await api.post(
@@ -4173,6 +4174,61 @@ medusaIntegrationTestRunner({
41734174
)
41744175
})
41754176

4177+
it("should add shipping method with tax rate override to cart", async () => {
4178+
let taxRegion = (
4179+
await api.get(`/admin/tax-regions?country_code=us`, adminHeaders)
4180+
).data.tax_regions[0]
4181+
4182+
// Create tax rate override for shipping option
4183+
await api.post(
4184+
`/admin/tax-rates`,
4185+
{
4186+
name: "Shipping Option Override",
4187+
tax_region_id: taxRegion.id,
4188+
rate: 25,
4189+
code: "T25",
4190+
is_combinable: false,
4191+
rules: [
4192+
{
4193+
reference: "shipping_option",
4194+
reference_id: shippingOption.id,
4195+
},
4196+
],
4197+
is_default: false,
4198+
},
4199+
adminHeaders
4200+
)
4201+
4202+
let response = await api.post(
4203+
`/store/carts/${cart.id}/shipping-methods`,
4204+
{ option_id: shippingOption.id },
4205+
storeHeaders
4206+
)
4207+
4208+
expect(response.status).toEqual(200)
4209+
expect(response.data.cart).toEqual(
4210+
expect.objectContaining({
4211+
id: cart.id,
4212+
shipping_methods: expect.arrayContaining([
4213+
expect.objectContaining({
4214+
shipping_option_id: shippingOption.id,
4215+
amount: 1000,
4216+
is_tax_inclusive: true,
4217+
tax_lines: expect.arrayContaining([
4218+
expect.objectContaining({
4219+
id: expect.any(String),
4220+
description: "Shipping Option Override",
4221+
code: "T25",
4222+
rate: 25,
4223+
provider_id: "system",
4224+
}),
4225+
]),
4226+
}),
4227+
]),
4228+
})
4229+
)
4230+
})
4231+
41764232
it("should throw when prices are not setup for shipping option", async () => {
41774233
cart = (
41784234
await api.post(

packages/modules/tax/integration-tests/__tests__/index.spec.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ITaxModuleService } from "@medusajs/framework/types"
22
import { Module, Modules, toMikroORMEntity } from "@medusajs/framework/utils"
3-
import { TaxModuleService } from "@services"
43
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
4+
import { TaxModuleService } from "@services"
55
import { setupTaxStructure } from "../utils/setup-tax-structure"
66

77
jest.setTimeout(30000)
@@ -717,6 +717,68 @@ moduleIntegrationTestRunner<ITaxModuleService>({
717717
])
718718
})
719719

720+
it("prioritizes specific shipping rate over default rate", async () => {
721+
const region = await service.createTaxRegions({
722+
country_code: "US",
723+
provider_id: "tp_system",
724+
default_tax_rate: {
725+
code: "TEST",
726+
name: "Test Rate",
727+
rate: 5,
728+
},
729+
})
730+
731+
const rate = await service.createTaxRates({
732+
tax_region_id: region.id,
733+
name: "Shipping Rate",
734+
code: "SHIPPING_TEST",
735+
rate: 2,
736+
})
737+
738+
const item = {
739+
id: "shipping_test",
740+
shipping_option_id: "so_1234",
741+
quantity: 1,
742+
}
743+
744+
const calculationContext = {
745+
address: {
746+
country_code: "US",
747+
},
748+
}
749+
750+
let taxLines = await service.getTaxLines([item], calculationContext)
751+
752+
expect(taxLines).toEqual([
753+
expect.objectContaining({
754+
rate_id: expect.any(String),
755+
rate: 5,
756+
code: "TEST",
757+
name: "Test Rate",
758+
}),
759+
])
760+
761+
await service.updateTaxRates(rate.id, {
762+
rules: [
763+
{
764+
reference: "shipping_option",
765+
reference_id: "so_1234",
766+
},
767+
],
768+
})
769+
770+
taxLines = await service.getTaxLines([item], calculationContext)
771+
772+
expect(taxLines).toEqual([
773+
expect.objectContaining({
774+
rate_id: expect.any(String),
775+
rate: 2,
776+
code: "SHIPPING_TEST",
777+
name: "Shipping Rate",
778+
}),
779+
])
780+
})
781+
720782
it("should delete tax rate", async () => {
721783
const region = await service.createTaxRegions({
722784
country_code: "US",

packages/modules/tax/src/services/tax-module-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ export default class TaxModuleService
654654
const matchingRules = rate.rules.filter((rule) => {
655655
if (isShipping) {
656656
return (
657-
rule.reference === "shipping" &&
657+
rule.reference === "shipping_option" &&
658658
rule.reference_id === item.shipping_option_id
659659
)
660660
}

0 commit comments

Comments
 (0)