Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Product type tax overrides #9951

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion integration-tests/modules/__tests__/cart/store/carts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RemoteLink } from "@medusajs/modules-sdk"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
IApiKeyModuleService,
ICartModuleService,
Expand All @@ -21,7 +22,6 @@ import {
PromotionType,
RuleOperator,
} from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
createAdminUser,
generatePublishableKey,
Expand Down Expand Up @@ -1178,6 +1178,89 @@ medusaIntegrationTestRunner({
)
})

it("should update a carts tax lines with a reduced product type rate", async () => {
await setupTaxStructure(taxModule)

const region = await regionModule.createRegions({
name: "US",
currency_code: "usd",
automatic_taxes: false,
countries: ["ca"],
})

const cart = await cartModule.createCarts({
currency_code: "usd",
region_id: region.id,
shipping_address: {
address_1: "test address 1",
address_2: "test address 2",
city: "CA",
country_code: "CA",
province: "QC",
postal_code: "94016",
},
items: [
{
id: "item-1",
unit_price: 2000,
quantity: 1,
title: "Test item",
product_id: "prod_tshirt_reduced_tax",
product_type_id: "product_type_id_3",
} as any,
{
id: "item-2",
unit_price: 1000,
quantity: 1,
title: "Test item two",
product_id: "prod_tshirt",
} as any,
],
})

let updated = await api.post(
`/store/carts/${cart.id}/taxes`,
{},
storeHeaders
)

expect(updated.status).toEqual(200)

expect(updated.data.cart).toEqual(
expect.objectContaining({
id: cart.id,
items: expect.arrayContaining([
expect.objectContaining({
id: "item-2",
tax_lines: expect.arrayContaining([
// Regional rate for Quebec, Canada
expect.objectContaining({
description: "QC Default Rate",
code: "QCDEFAULT",
rate: 2,
provider_id: "system",
}),
]),
adjustments: [],
}),
expect.objectContaining({
id: "item-1",
tax_lines: expect.arrayContaining([
// Reduced rate for product types in Quebec, Canada
expect.objectContaining({
description: "QC Reduced Rate for Product Type",
code: "QCREDUCE_TYPE",
rate: 1,
provider_id: "system",
}),
]),
adjustments: [],
}),
]),
})
)
})

it("should throw error when shipping is not present", async () => {
await setupTaxStructure(taxModule)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
ICartModuleService,
IFulfillmentModuleService,
Expand All @@ -10,7 +11,6 @@ import {
ITaxModuleService,
} from "@medusajs/types"
import { ContainerRegistrationKeys, Modules } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
adminHeaders,
createAdminUser,
Expand Down Expand Up @@ -244,6 +244,7 @@ medusaIntegrationTestRunner({
product_description: null,
product_subtitle: null,
product_type: null,
product_type_id: null,
product_collection: null,
product_handle: "test-product",
variant_sku: null,
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/modules/__tests__/order/order.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { IOrderModuleService } from "@medusajs/types"
import { Modules } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
adminHeaders,
createAdminUser,
Expand Down Expand Up @@ -157,6 +157,7 @@ medusaIntegrationTestRunner({
product_description: null,
product_subtitle: null,
product_type: null,
product_type_id: null,
product_collection: null,
product_handle: null,
variant_sku: null,
Expand Down
142 changes: 0 additions & 142 deletions packages/core/core-flows/src/cart/steps/get-item-tax-lines.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/core-flows/src/cart/steps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export * from "./find-one-or-any-region"
export * from "./find-or-create-customer"
export * from "./find-sales-channel"
export * from "./get-actions-to-compute-from-promotions"
export * from "./get-item-tax-lines"
export * from "./get-line-item-actions"
export * from "./get-promotion-codes-to-apply"
export * from "./get-variant-price-sets"
Expand All @@ -28,3 +27,4 @@ export * from "./update-line-items"
export * from "./validate-cart-payments"
export * from "./validate-cart-shipping-options"
export * from "./validate-variant-prices"

Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export function prepareLineItemData(data: Input) {
variant.product.description ?? item?.product_description,
product_subtitle: variant.product.subtitle ?? item?.product_subtitle,
product_type: variant.product.type?.value ?? item?.product_type ?? null,
product_type_id: variant.product.type?.id ?? item?.product_type_id ?? null,
product_collection:
variant.product.collection?.title ?? item?.product_collection ?? null,
product_handle: variant.product.handle ?? item?.product_handle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
transform,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import { getItemTaxLinesStep, setTaxLinesForItemsStep } from "../steps"
import { getItemTaxLinesStep } from "../../tax/steps/get-item-tax-lines"
import { setTaxLinesForItemsStep } from "../steps"

const cartFields = [
"id",
Expand All @@ -23,6 +24,7 @@ const cartFields = [
"items.product_description",
"items.product_subtitle",
"items.product_type",
"items.product_type_id",
"items.product_collection",
"items.product_handle",
"items.variant_sku",
Expand Down Expand Up @@ -81,7 +83,7 @@ export const updateTaxLinesWorkflow = createWorkflow(

const taxLineItems = getItemTaxLinesStep(
transform({ input, cart }, (data) => ({
cart: data.cart,
orderOrCart: data.cart,
items: data.input.items || data.cart.items,
shipping_methods:
data.input.shipping_methods || data.cart.shipping_methods,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core-flows/src/order/steps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export * from "./exchange/cancel-exchange"
export * from "./exchange/create-exchange"
export * from "./exchange/create-exchange-items-from-actions"
export * from "./exchange/delete-exchanges"
export * from "./get-item-tax-lines"
export * from "./preview-order-change"
export * from "./register-fulfillment"
export * from "./register-shipment"
Expand All @@ -35,3 +34,4 @@ export * from "./update-order-change-actions"
export * from "./update-order-changes"
export * from "./update-order-exchanges"
export * from "./update-shipping-methods"

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import {
when,
} from "@medusajs/framework/workflows-sdk"
import { useRemoteQueryStep } from "../../common"
import {
getOrderItemTaxLinesStep,
setOrderTaxLinesForItemsStep,
} from "../steps"
import { getItemTaxLinesStep } from "../../tax/steps/get-item-tax-lines"
import { setOrderTaxLinesForItemsStep } from "../steps"

const completeOrderFields = [
"id",
Expand Down Expand Up @@ -174,7 +172,7 @@ export const updateOrderTaxLinesWorkflow = createWorkflow(
}).config({ name: "query-order-shipping-methods" })
})

const taxLineItems = getOrderItemTaxLinesStep(
const taxLineItems = getItemTaxLinesStep(
transform(
{ input, order, items, shippingMethods, isFullOrder },
(data) => {
Expand All @@ -187,7 +185,7 @@ export const updateOrderTaxLinesWorkflow = createWorkflow(
: data.items ?? []

return {
order: data.order,
orderOrCart: data.order,
items: lineItems,
shipping_methods: shippingMethods,
force_tax_calculation: data.input.force_tax_calculation,
Expand Down
Loading
Loading