fix(promotion, types): non discountable items check (#12644)
* fix(promotions): check if item is discountable * fix: return earl yonly if non discountable * fix: update test * chore: add integration test
This commit is contained in:
6
.changeset/proud-shirts-leave.md
Normal file
6
.changeset/proud-shirts-leave.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@medusajs/promotion": patch
|
||||
"@medusajs/types": patch
|
||||
---
|
||||
|
||||
fix(promotion, types): non discountable items check
|
||||
@@ -2621,6 +2621,96 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
})
|
||||
|
||||
it("should only apply promotion on discountable items", async () => {
|
||||
const notDiscountableProduct = (
|
||||
await api.post(
|
||||
"/admin/products",
|
||||
{
|
||||
title: "Medusa T-Shirt not discountable",
|
||||
handle: "t-shirt-not-discountable",
|
||||
discountable: false,
|
||||
options: [
|
||||
{
|
||||
title: "Size",
|
||||
values: ["S"],
|
||||
},
|
||||
],
|
||||
variants: [
|
||||
{
|
||||
title: "S",
|
||||
sku: "s-shirt",
|
||||
options: {
|
||||
Size: "S",
|
||||
},
|
||||
manage_inventory: false,
|
||||
prices: [
|
||||
{
|
||||
amount: 1000,
|
||||
currency_code: "usd",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product
|
||||
|
||||
const cartData = {
|
||||
currency_code: "usd",
|
||||
sales_channel_id: salesChannel.id,
|
||||
region_id: region.id,
|
||||
shipping_address: shippingAddressData,
|
||||
items: [
|
||||
{ variant_id: product.variants[0].id, quantity: 1 },
|
||||
{
|
||||
variant_id: notDiscountableProduct.variants[0].id,
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
promo_codes: [promotion.code],
|
||||
}
|
||||
|
||||
const cart = (
|
||||
await api.post(
|
||||
`/store/carts?fields=+items.is_discountable,+items.total,+items.discount_total`,
|
||||
cartData,
|
||||
storeHeaders
|
||||
)
|
||||
).data.cart
|
||||
|
||||
expect(cart).toEqual(
|
||||
expect.objectContaining({
|
||||
discount_subtotal: 100,
|
||||
items: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
variant_id: product.variants[0].id,
|
||||
is_discountable: true,
|
||||
unit_price: 1500,
|
||||
total: 1395,
|
||||
discount_total: 100,
|
||||
adjustments: [
|
||||
expect.objectContaining({
|
||||
promotion_id: promotion.id,
|
||||
amount: 100,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
variant_id: notDiscountableProduct.variants[0].id,
|
||||
is_discountable: false,
|
||||
total: 1000,
|
||||
unit_price: 1000,
|
||||
discount_total: 0,
|
||||
adjustments: [],
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should remove promotion adjustments when promotion is deleted", async () => {
|
||||
let cartBeforeRemovingPromotion = (
|
||||
await api.get(`/store/carts/${cart.id}`, storeHeaders)
|
||||
|
||||
@@ -162,6 +162,71 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
})
|
||||
|
||||
it("should add line item adjustments only for discountable items", async () => {
|
||||
const createdPromotion =
|
||||
await promotionModuleService.createPromotions({
|
||||
code: "PROMOTION_TEST",
|
||||
type: PromotionType.STANDARD,
|
||||
status: PromotionStatus.ACTIVE,
|
||||
application_method: {
|
||||
type: "fixed",
|
||||
target_type: "items",
|
||||
allocation: "across",
|
||||
value: 1000,
|
||||
apply_to_quantity: 1,
|
||||
currency_code: "usd",
|
||||
},
|
||||
})
|
||||
|
||||
const cart = await cartModuleService.createCarts({
|
||||
currency_code: "usd",
|
||||
items: [
|
||||
{
|
||||
id: "item-1",
|
||||
unit_price: 2000,
|
||||
quantity: 1,
|
||||
title: "Test item",
|
||||
product_id: "prod_mat",
|
||||
} as any,
|
||||
{
|
||||
id: "item-2",
|
||||
unit_price: 1000,
|
||||
quantity: 1,
|
||||
title: "Test item",
|
||||
product_id: "prod_tshirt",
|
||||
is_discountable: false,
|
||||
} as any,
|
||||
],
|
||||
})
|
||||
|
||||
const created = await api.post(
|
||||
`/store/carts/${cart.id}/promotions`,
|
||||
{ promo_codes: [createdPromotion.code] },
|
||||
storeHeaders
|
||||
)
|
||||
|
||||
expect(created.status).toEqual(200)
|
||||
expect(created.data.cart).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
items: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: "item-1",
|
||||
adjustments: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
code: createdPromotion.code,
|
||||
amount: 1000,
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
adjustments: [],
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should add shipping method adjustments to a cart based on promotions", async () => {
|
||||
const [appliedPromotion] =
|
||||
await promotionModuleService.createPromotions([
|
||||
|
||||
@@ -180,6 +180,11 @@ export interface ComputeActionItemLine extends Record<string, unknown> {
|
||||
*/
|
||||
subtotal: BigNumberInput
|
||||
|
||||
/**
|
||||
* Whether the line item is discountable.
|
||||
*/
|
||||
is_discountable: boolean
|
||||
|
||||
/**
|
||||
* The adjustments applied before on the line item.
|
||||
*/
|
||||
|
||||
@@ -201,7 +201,15 @@ function getValidItemsForPromotion(
|
||||
}
|
||||
|
||||
return items.filter((item) => {
|
||||
if (!item || !("subtotal" in item) || MathBN.lte(item.subtotal, 0)) {
|
||||
if (!item) {
|
||||
return false
|
||||
}
|
||||
|
||||
if ("is_discountable" in item && !item.is_discountable) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (!("subtotal" in item) || MathBN.lte(item.subtotal, 0)) {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user