From a93f96fb07875ad22f4e302b078c992842d1321a Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 15 Nov 2024 16:45:13 +0100 Subject: [PATCH 1/3] fix: add address and customer metadata to tax calc context --- .../cart/store/cart.workflows.spec.ts | 39 +++++++++++++++++++ .../src/cart/workflows/update-tax-lines.ts | 2 + .../src/tax/steps/get-item-tax-lines.ts | 2 + packages/core/types/src/tax/common.ts | 10 +++++ 4 files changed, 53 insertions(+) diff --git a/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts b/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts index 246294fe5ef38..7ae11a700952f 100644 --- a/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts +++ b/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts @@ -11,6 +11,7 @@ import { updateLineItemInCartWorkflow, updateLineItemsStepId, updatePaymentCollectionStepId, + updateTaxLinesWorkflow, } from "@medusajs/core-flows" import { ICartModuleService, @@ -2159,6 +2160,44 @@ medusaIntegrationTestRunner({ ]) }) }) + + describe("updateTaxLinesWorkflow", () => { + it.only("should create a payment collection and link it to cart", 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, + }) + }) + }) }) }, }) diff --git a/packages/core/core-flows/src/cart/workflows/update-tax-lines.ts b/packages/core/core-flows/src/cart/workflows/update-tax-lines.ts index dd02e0c1f314b..4fb049bf2830d 100644 --- a/packages/core/core-flows/src/cart/workflows/update-tax-lines.ts +++ b/packages/core/core-flows/src/cart/workflows/update-tax-lines.ts @@ -47,6 +47,7 @@ const cartFields = [ "shipping_methods.amount", "customer.id", "customer.email", + "customer.metadata", "customer.groups.id", "shipping_address.id", "shipping_address.address_1", @@ -56,6 +57,7 @@ const cartFields = [ "shipping_address.country_code", "shipping_address.region_code", "shipping_address.province", + "shipping_address.metadata", ] export type UpdateTaxLinesWorkflowInput = { diff --git a/packages/core/core-flows/src/tax/steps/get-item-tax-lines.ts b/packages/core/core-flows/src/tax/steps/get-item-tax-lines.ts index b500bd7ae2043..e9576bf681816 100644 --- a/packages/core/core-flows/src/tax/steps/get-item-tax-lines.ts +++ b/packages/core/core-flows/src/tax/steps/get-item-tax-lines.ts @@ -53,6 +53,7 @@ function normalizeTaxModuleContext( id: orderOrCart.customer.id, email: orderOrCart.customer.email, customer_groups: orderOrCart.customer.groups?.map((g) => g.id) || [], + metadata: orderOrCart.customer.metadata, } return { @@ -63,6 +64,7 @@ function normalizeTaxModuleContext( address_2: address.address_2, city: address.city, postal_code: address.postal_code, + metadata: address.metadata, }, customer, is_return: isReturn ?? false, diff --git a/packages/core/types/src/tax/common.ts b/packages/core/types/src/tax/common.ts index 219013babe0f5..a3fa60b287bcf 100644 --- a/packages/core/types/src/tax/common.ts +++ b/packages/core/types/src/tax/common.ts @@ -448,6 +448,11 @@ export interface TaxCalculationContext { * The postal code. */ postal_code?: string + + /** + * Address metadata. + */ + metadata?: Record | null } /** @@ -468,6 +473,11 @@ export interface TaxCalculationContext { * The groups that the customer belongs to. */ customer_groups: string[] + + /** + * Customer metadata. + */ + metadata?: Record | null } /** From cb40147ce4d57effe4ff213f468a7f07cee724a5 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Fri, 15 Nov 2024 16:50:07 +0100 Subject: [PATCH 2/3] fix: rm only --- .../modules/__tests__/cart/store/cart.workflows.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts b/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts index 7ae11a700952f..105836f9e6a7a 100644 --- a/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts +++ b/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts @@ -2162,7 +2162,7 @@ medusaIntegrationTestRunner({ }) describe("updateTaxLinesWorkflow", () => { - it.only("should create a payment collection and link it to cart", async () => { + it("should create a payment collection and link it to cart", async () => { const cart = await cartModuleService.createCarts({ currency_code: "dkk", region_id: defaultRegion.id, From bd84b75831e61731438f29048eaf2a8971e984c8 Mon Sep 17 00:00:00 2001 From: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Tue, 19 Nov 2024 12:23:58 +0100 Subject: [PATCH 3/3] Update integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts --- .../modules/__tests__/cart/store/cart.workflows.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts b/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts index 105836f9e6a7a..21cd151bf5aff 100644 --- a/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts +++ b/integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts @@ -2162,7 +2162,7 @@ medusaIntegrationTestRunner({ }) describe("updateTaxLinesWorkflow", () => { - it("should create a payment collection and link it to cart", async () => { + it("should include shipping address metadata in tax calculation context", async () => { const cart = await cartModuleService.createCarts({ currency_code: "dkk", region_id: defaultRegion.id,