fix: add address and customer metadata to tax calc context (#10122)

Fixes CMRC-713

**What**
- Ensure that customer and shipping address metadata is passed in the tax calculation context when updating tax lines on a cart.

**Why**
- Gives more flexibility in criteria to evaluate tax rates. 
- https://github.com/medusajs/medusa/discussions/10121
- https://github.com/medusajs/medusa/discussions/10114

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Sebastian Rindom
2024-11-19 11:42:31 +00:00
committed by GitHub
co-authored by Oli Juhl
parent 3b1463048a
commit 41dc05d0c9
4 changed files with 53 additions and 0 deletions
@@ -11,6 +11,7 @@ import {
updateLineItemInCartWorkflow, updateLineItemInCartWorkflow,
updateLineItemsStepId, updateLineItemsStepId,
updatePaymentCollectionStepId, updatePaymentCollectionStepId,
updateTaxLinesWorkflow,
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { import {
ICartModuleService, ICartModuleService,
@@ -2159,6 +2160,44 @@ medusaIntegrationTestRunner({
]) ])
}) })
}) })
describe("updateTaxLinesWorkflow", () => {
it("should include shipping address metadata in tax calculation context", async () => {
const cart = await cartModuleService.createCarts({
currency_code: "dkk",
region_id: defaultRegion.id,
shipping_address: {
metadata: {
testing_tax: true,
},
},
items: [
{
quantity: 1,
unit_price: 5000,
title: "Test item",
},
],
})
const { transaction } = await updateTaxLinesWorkflow(
appContainer
).run({
input: {
cart_id: cart.id,
},
throwOnError: false,
})
expect(
// @ts-ignore
transaction.context.invoke["use-remote-query"].output.output
.shipping_address.metadata
).toEqual({
testing_tax: true,
})
})
})
}) })
}, },
}) })
@@ -47,6 +47,7 @@ const cartFields = [
"shipping_methods.amount", "shipping_methods.amount",
"customer.id", "customer.id",
"customer.email", "customer.email",
"customer.metadata",
"customer.groups.id", "customer.groups.id",
"shipping_address.id", "shipping_address.id",
"shipping_address.address_1", "shipping_address.address_1",
@@ -56,6 +57,7 @@ const cartFields = [
"shipping_address.country_code", "shipping_address.country_code",
"shipping_address.region_code", "shipping_address.region_code",
"shipping_address.province", "shipping_address.province",
"shipping_address.metadata",
] ]
export type UpdateTaxLinesWorkflowInput = { export type UpdateTaxLinesWorkflowInput = {
@@ -53,6 +53,7 @@ function normalizeTaxModuleContext(
id: orderOrCart.customer.id, id: orderOrCart.customer.id,
email: orderOrCart.customer.email, email: orderOrCart.customer.email,
customer_groups: orderOrCart.customer.groups?.map((g) => g.id) || [], customer_groups: orderOrCart.customer.groups?.map((g) => g.id) || [],
metadata: orderOrCart.customer.metadata,
} }
return { return {
@@ -63,6 +64,7 @@ function normalizeTaxModuleContext(
address_2: address.address_2, address_2: address.address_2,
city: address.city, city: address.city,
postal_code: address.postal_code, postal_code: address.postal_code,
metadata: address.metadata,
}, },
customer, customer,
is_return: isReturn ?? false, is_return: isReturn ?? false,
+10
View File
@@ -448,6 +448,11 @@ export interface TaxCalculationContext {
* The postal code. * The postal code.
*/ */
postal_code?: string postal_code?: string
/**
* Address metadata.
*/
metadata?: Record<string, unknown> | null
} }
/** /**
@@ -468,6 +473,11 @@ export interface TaxCalculationContext {
* The groups that the customer belongs to. * The groups that the customer belongs to.
*/ */
customer_groups: string[] customer_groups: string[]
/**
* Customer metadata.
*/
metadata?: Record<string, unknown> | null
} }
/** /**