feat(core-flows,types,utils,medusa): Translate tax lines (#14359)

* Include locale field for traslations on tax line workflows

* Translate tax lines in getItemTaxLinesStep with new util

* Update tax calculation context, so that we pass locale to third party tax providers if they want to return translated tax rates

* Apply translations to tax lines on product and variant tax middlewares

* Cart management translations tests

* Update tax lines when order locale gets updated

* Add changeset

* Get tranlsated tax lines step

* Fix wording

* Mutate ref directly

* Update order tax lines translations upon order locale change

* Claims translations tests

* Update tax lines upon draft order locale update

* Exchange tests for tax lines translations

* Order edits test for tax line translation

* Add tests for shipping methods tax line translations on various order flows

* Returns shipping method translations tests

* Execute update in parallel

* Use TranslationFeatureFlag.key

* Fix feature flag import

* Add @medusajs/medusa dependency for feature flag usage

* Revert "Add @medusajs/medusa dependency for feature flag usage"

This reverts commit e8897aed0a88f83c1034ac73e817e4222250a2c9.

* Use feature flag string directly

* Fix test

* Parallelize tax line translations application
This commit is contained in:
Nicolas Gorga
2026-01-13 15:12:42 -03:00
committed by GitHub
parent 28fae96cee
commit cec8b8e428
23 changed files with 2032 additions and 164 deletions

View File

@@ -21,13 +21,16 @@ medusaIntegrationTestRunner({
let shippingProfile: { id: string }
let stockLocation: { id: string }
let shippingOption: { id: string }
let taxRate: { id: string }
beforeAll(async () => {
appContainer = getContainer()
})
beforeEach(async () => {
await setupTaxStructure(appContainer.resolve(Modules.TAX))
const taxStructure = await setupTaxStructure(
appContainer.resolve(Modules.TAX)
)
await createAdminUser(dbConnection, adminHeaders, appContainer)
salesChannel = (
@@ -162,6 +165,14 @@ medusaIntegrationTestRunner({
)
).data.shipping_option
const taxRatesResponse = await api.get(
`/admin/tax-rates?tax_region_id=${taxStructure.us.children.cal.province.id}`,
adminHeaders
)
taxRate = taxRatesResponse.data.tax_rates.find(
(rate: { code: string }) => rate.code === "CADEFAULT"
)
await api.post(
"/admin/translations/batch",
{
@@ -224,6 +235,22 @@ medusaIntegrationTestRunner({
name: "Test-Versandoption",
},
},
{
reference_id: taxRate.id,
reference: "tax_rate",
locale_code: "fr-FR",
translations: {
name: "Taux par défaut CA",
},
},
{
reference_id: taxRate.id,
reference: "tax_rate",
locale_code: "de-DE",
translations: {
name: "CA Standardsteuersatz",
},
},
],
},
adminHeaders
@@ -244,6 +271,7 @@ medusaIntegrationTestRunner({
address_1: "123 Main St",
city: "Anytown",
country_code: "us",
province: "ca",
postal_code: "12345",
first_name: "John",
},
@@ -283,6 +311,12 @@ medusaIntegrationTestRunner({
variant_title: "Petit",
})
)
expect(updatedDraftOrder.items[0].tax_lines.length).toBeGreaterThan(0)
const taxLine = updatedDraftOrder.items[0].tax_lines.find(
(tl) => tl.code === "CADEFAULT"
)
expect(taxLine.description).toEqual("Taux par défaut CA")
})
it("should have original values when draft order has no locale", async () => {
@@ -297,6 +331,7 @@ medusaIntegrationTestRunner({
address_1: "123 Main St",
city: "Anytown",
country_code: "us",
province: "ca",
postal_code: "12345",
first_name: "John",
},
@@ -336,6 +371,12 @@ medusaIntegrationTestRunner({
variant_title: "Small",
})
)
expect(updatedDraftOrder.items[0].tax_lines.length).toBeGreaterThan(0)
const taxLine = updatedDraftOrder.items[0].tax_lines.find(
(tl) => tl.code === "CADEFAULT"
)
expect(taxLine.description).toEqual("CA Default Rate")
})
it("should translate multiple items added to draft order", async () => {
@@ -351,6 +392,7 @@ medusaIntegrationTestRunner({
address_1: "123 Main St",
city: "Anytown",
country_code: "us",
province: "ca",
postal_code: "12345",
first_name: "John",
},
@@ -407,6 +449,146 @@ medusaIntegrationTestRunner({
variant_title: "Mittel",
})
)
expect(smallItem.tax_lines.length).toBeGreaterThan(0)
const smallTaxLine = smallItem.tax_lines.find(
(tl) => tl.code === "CADEFAULT"
)
expect(smallTaxLine.description).toEqual("CA Standardsteuersatz")
expect(mediumItem.tax_lines.length).toBeGreaterThan(0)
const mediumTaxLine = mediumItem.tax_lines.find(
(tl) => tl.code === "CADEFAULT"
)
expect(mediumTaxLine.description).toEqual("CA Standardsteuersatz")
})
})
describe("POST /admin/draft-orders/:id/edit/shipping-methods (add shipping method to draft order)", () => {
it("should translate shipping method tax lines when adding to draft order with locale", async () => {
const draftOrder = (
await api.post(
"/admin/draft-orders",
{
email: "test@test.com",
region_id: region.id,
sales_channel_id: salesChannel.id,
locale: "fr-FR",
shipping_address: {
address_1: "123 Main St",
city: "Anytown",
country_code: "us",
province: "ca",
postal_code: "12345",
first_name: "John",
},
},
adminHeaders
)
).data.draft_order
await api.post(
`/admin/draft-orders/${draftOrder.id}/edit`,
{},
adminHeaders
)
await api.post(
`/admin/draft-orders/${draftOrder.id}/edit/items`,
{
items: [{ variant_id: product.variants[0].id, quantity: 1 }],
},
adminHeaders
)
await api.post(
`/admin/draft-orders/${draftOrder.id}/edit/shipping-methods`,
{
shipping_option_id: shippingOption.id,
},
adminHeaders
)
await api.post(
`/admin/draft-orders/${draftOrder.id}/edit/confirm`,
{},
adminHeaders
)
const updatedDraftOrder = (
await api.get(`/admin/draft-orders/${draftOrder.id}`, adminHeaders)
).data.draft_order
expect(updatedDraftOrder.shipping_methods.length).toBeGreaterThan(0)
const shippingMethod = updatedDraftOrder.shipping_methods[0]
const taxLine = shippingMethod.tax_lines.find(
(tl) => tl.code === "CADEFAULT"
)
expect(taxLine.description).toEqual("Taux par défaut CA")
})
it("should use original tax line description when draft order has no locale", async () => {
const draftOrder = (
await api.post(
"/admin/draft-orders",
{
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",
province: "ca",
postal_code: "12345",
first_name: "John",
},
},
adminHeaders
)
).data.draft_order
await api.post(
`/admin/draft-orders/${draftOrder.id}/edit`,
{},
adminHeaders
)
await api.post(
`/admin/draft-orders/${draftOrder.id}/edit/items`,
{
items: [{ variant_id: product.variants[0].id, quantity: 1 }],
},
adminHeaders
)
await api.post(
`/admin/draft-orders/${draftOrder.id}/edit/shipping-methods`,
{
shipping_option_id: shippingOption.id,
},
adminHeaders
)
await api.post(
`/admin/draft-orders/${draftOrder.id}/edit/confirm`,
{},
adminHeaders
)
const updatedDraftOrder = (
await api.get(`/admin/draft-orders/${draftOrder.id}`, adminHeaders)
).data.draft_order
expect(updatedDraftOrder.shipping_methods.length).toBeGreaterThan(0)
const shippingMethod = updatedDraftOrder.shipping_methods[0]
const taxLine = shippingMethod.tax_lines.find(
(tl) => tl.code === "CADEFAULT"
)
expect(taxLine).toBeDefined()
expect(taxLine.description).toEqual("CA Default Rate")
})
})
@@ -424,6 +606,7 @@ medusaIntegrationTestRunner({
address_1: "123 Main St",
city: "Anytown",
country_code: "us",
province: "ca",
postal_code: "12345",
first_name: "John",
},
@@ -449,6 +632,14 @@ medusaIntegrationTestRunner({
adminHeaders
)
await api.post(
`/admin/draft-orders/${draftOrder.id}/edit/shipping-methods`,
{
shipping_option_id: shippingOption.id,
},
adminHeaders
)
await api.post(
`/admin/draft-orders/${draftOrder.id}/edit/confirm`,
{},
@@ -464,6 +655,12 @@ medusaIntegrationTestRunner({
)
expect(frenchSmallItem.variant_title).toEqual("Petit")
expect(frenchSmallItem.tax_lines.length).toBeGreaterThan(0)
const frenchTaxLine = frenchSmallItem.tax_lines.find(
(tl) => tl.code === "CADEFAULT"
)
expect(frenchTaxLine.description).toEqual("Taux par défaut CA")
await api.post(
`/admin/draft-orders/${draftOrder.id}`,
{ locale: "de-DE" },
@@ -494,6 +691,31 @@ medusaIntegrationTestRunner({
variant_title: "Mittel",
})
)
expect(germanSmallItem.tax_lines.length).toBeGreaterThan(0)
const germanSmallTaxLine = germanSmallItem.tax_lines.find(
(tl) => tl.code === "CADEFAULT"
)
expect(germanSmallTaxLine.description).toEqual(
"CA Standardsteuersatz"
)
expect(germanMediumItem.tax_lines.length).toBeGreaterThan(0)
const germanMediumTaxLine = germanMediumItem.tax_lines.find(
(tl) => tl.code === "CADEFAULT"
)
expect(germanMediumTaxLine.description).toEqual(
"CA Standardsteuersatz"
)
expect(updatedDraftOrder.shipping_methods.length).toBeGreaterThan(0)
const shippingMethod = updatedDraftOrder.shipping_methods[0]
const shippingTaxLine = shippingMethod.tax_lines.find(
(tl) => tl.code === "CADEFAULT"
)
expect(shippingTaxLine).toBeDefined()
expect(shippingTaxLine.description).toEqual("CA Standardsteuersatz")
})
})