Fix(medusa): make shipping option requirements tax inclusive if the shipping option is (#2544)

**What**
- validate shipping option requirements including tax if the shipping option is tax inclusive

**Testing**
- " given a cart with total above min-threshold and subtotal below min-threshold shipping option with tax inclusive pricing is available and can be applied"
- "given a cart with total above max-threshold and subtotal below max-threshold shipping option with tax inclusive pricing is not available"

Fixes CORE-752
This commit is contained in:
Philip Korsholm authored and GitHub committed 2022-11-04 14:04:07 +00:00
1 parent a68af8a474
commit 434b11af46
4 files changed
+179 -8

No files matched your search

@@ -3,9 +3,18 @@ const { Region, ShippingProfile, ShippingOption } = require("@medusajs/medusa")
const setupServer = require("../../../helpers/setup-server") const setupServer = require("../../../helpers/setup-server")
const { useApi } = require("../../../helpers/use-api") const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db") const { useDb } = require("../../../helpers/use-db")
const cartSeeder = require("../../helpers/cart-seeder") const cartSeeder = require("../../helpers/cart-seeder")
const swapSeeder = require("../../helpers/swap-seeder") const swapSeeder = require("../../helpers/swap-seeder")
const {
simpleRegionFactory,
simpleShippingOptionFactory,
simpleCartFactory,
simpleProductFactory,
} = require("../../factories")
const {
default: startServerWithEnvironment,
} = require("../../../helpers/start-server-with-environment")
jest.setTimeout(30000) jest.setTimeout(30000)
@@ -13,10 +22,15 @@ describe("/store/shipping-options", () => {
let medusaProcess let medusaProcess
let dbConnection let dbConnection
describe("tax exclusive", () => {
beforeAll(async () => { beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..")) const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd }) const [process, conn] = await startServerWithEnvironment({
medusaProcess = await setupServer({ cwd }) cwd,
env: { MEDUSA_FF_TAX_INCLUSIVE_PRICING: true },
})
medusaProcess = process
dbConnection = conn
}) })
afterAll(async () => { afterAll(async () => {
@@ -94,7 +108,9 @@ describe("/store/shipping-options", () => {
it("retrieves all shipping options", async () => { it("retrieves all shipping options", async () => {
const api = useApi() const api = useApi()
const response = await api.get("/store/shipping-options").catch((err) => { const response = await api
.get("/store/shipping-options")
.catch((err) => {
return err.response return err.response
}) })
@@ -181,4 +197,151 @@ describe("/store/shipping-options", () => {
) )
}) })
}) })
})
describe("Tax inclusive GET /store/shipping-options/:cart_id", () => {
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
const [process, conn] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_TAX_INCLUSIVE_PRICING: true },
})
medusaProcess = process
dbConnection = conn
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
medusaProcess.kill()
})
beforeEach(async () => {
await simpleRegionFactory(dbConnection, {
id: "region2",
name: "Test Region 2",
currency_code: "usd",
tax_rate: 10,
includes_tax: true,
})
await simpleShippingOptionFactory(dbConnection, {
region_id: "region2",
includes_tax: true,
price: 100,
requirements: [{ type: "min_subtotal", amount: 100 }],
})
await simpleShippingOptionFactory(dbConnection, {
region_id: "region2",
includes_tax: true,
price: 150,
requirements: [{ type: "max_subtotal", amount: 150 }],
})
await simpleProductFactory(dbConnection, {
variants: [
{ id: "variant-1", prices: [{ currency: "usd", amount: 95 }] },
],
})
await simpleProductFactory(dbConnection, {
variants: [
{ id: "variant-2", prices: [{ currency: "usd", amount: 145 }] },
],
})
const cart = await simpleCartFactory(dbConnection, {
id: "test-cart",
region: "region2",
})
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("given a cart with total above min-threshold and subtotal below min-threshold shipping option with tax inclusive pricing is available and can be applied", async () => {
const api = useApi()
// create line item
const { data } = await api.post(`/store/carts/test-cart/line-items`, {
variant_id: "variant-1",
quantity: 1,
})
expect(data.cart.subtotal).toEqual(95)
expect(data.cart.total).toEqual(105)
const res = await api.get(`/store/shipping-options/test-cart`)
expect(res.data.shipping_options.length).toEqual(2)
expect(res.data.shipping_options).toEqual(
expect.arrayContaining([
expect.objectContaining({
requirements: [
expect.objectContaining({
type: "min_subtotal",
amount: 100,
}),
],
}),
])
)
expect(res.data.shipping_options).toEqual(
expect.arrayContaining([
expect.objectContaining({
requirements: [
expect.objectContaining({
type: "max_subtotal",
amount: 150,
}),
],
}),
])
)
const shippingOption = res.data.shipping_options.find(
(so) => !!so.requirements.find((r) => r.type === "min_subtotal")
)
const addShippingMethodRes = await api.post(
`/store/carts/test-cart/shipping-methods`,
{
option_id: shippingOption.id,
}
)
expect(addShippingMethodRes.status).toEqual(200)
expect(addShippingMethodRes.data.cart.shipping_methods.length).toEqual(1)
expect(
addShippingMethodRes.data.cart.shipping_methods[0].shipping_option_id
).toEqual(shippingOption.id)
})
it("given a cart with total above max-threshold and subtotal below max-threshold shipping option with tax inclusive pricing is not available", async () => {
const api = useApi()
// create line item
const { data } = await api.post(`/store/carts/test-cart/line-items`, {
variant_id: "variant-2",
quantity: 1,
})
expect(data.cart.subtotal).toEqual(145)
expect(data.cart.total).toEqual(160)
const res = await api.get(`/store/shipping-options/test-cart`)
expect(res.data.shipping_options.length).toEqual(1)
expect(res.data.shipping_options).toEqual(
expect.arrayContaining([
expect.objectContaining({
requirements: [
expect.objectContaining({
type: "min_subtotal",
amount: 100,
}),
],
}),
])
)
})
})
}) })
@@ -1,6 +1,7 @@
import { import {
ShippingOption, ShippingOption,
ShippingOptionPriceType, ShippingOptionPriceType,
ShippingOptionRequirement,
ShippingProfile, ShippingProfile,
ShippingProfileType, ShippingProfileType,
} from "@medusajs/medusa" } from "@medusajs/medusa"
@@ -17,6 +18,12 @@ export type ShippingOptionFactoryData = {
price_type?: ShippingOptionPriceType price_type?: ShippingOptionPriceType
includes_tax?: boolean includes_tax?: boolean
data?: object data?: object
requirements: ShippingOptionRequirementData[]
}
type ShippingOptionRequirementData = {
type: 'min_subtotal' | 'max_subtotal'
amount: number
} }
export const simpleShippingOptionFactory = async ( export const simpleShippingOptionFactory = async (
@@ -46,6 +53,7 @@ export const simpleShippingOptionFactory = async (
profile_id: data.is_giftcard ? gcProfile.id : defaultProfile.id, profile_id: data.is_giftcard ? gcProfile.id : defaultProfile.id,
price_type: data.price_type ?? ShippingOptionPriceType.FLAT_RATE, price_type: data.price_type ?? ShippingOptionPriceType.FLAT_RATE,
data: data.data ?? {}, data: data.data ?? {},
requirements: (data.requirements || []) as ShippingOptionRequirement[],
amount: typeof data.price !== "undefined" ? data.price : 500, amount: typeof data.price !== "undefined" ? data.price : 500,
} }
+1 -1
View File
@@ -1755,7 +1755,7 @@ class CartService extends TransactionBaseService {
return await this.atomicPhase_( return await this.atomicPhase_(
async (transactionManager: EntityManager) => { async (transactionManager: EntityManager) => {
const cart = await this.retrieve(cartId, { const cart = await this.retrieve(cartId, {
select: ["subtotal"], select: ["subtotal", "total"],
relations: [ relations: [
"shipping_methods", "shipping_methods",
"discounts", "discounts",
@@ -370,15 +370,15 @@ class ShippingOptionService extends TransactionBaseService {
) )
} }
const subtotal = cart.subtotal as number const amount = (option.includes_tax ? cart.total : cart.subtotal) as number
const requirementResults: boolean[] = option.requirements.map( const requirementResults: boolean[] = option.requirements.map(
(requirement) => { (requirement) => {
switch (requirement.type) { switch (requirement.type) {
case "max_subtotal": case "max_subtotal":
return requirement.amount > subtotal return requirement.amount > amount
case "min_subtotal": case "min_subtotal":
return requirement.amount <= subtotal return requirement.amount <= amount
default: default:
return true return true
} }