fix(promotion): check currency when computing actions for promotions (#13084)

* fix(promotion): check currency when computing actions for promotions

* chore: fix tests

* chore: fix more specs
This commit is contained in:
Riqwan Thamir
2025-07-31 11:41:07 +02:00
committed by GitHub
parent 54a74b0215
commit 75320e744f
12 changed files with 152 additions and 69 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/promotion": patch
---
fix(promotion): check currency when computing actions for promotions

View File

@@ -17,7 +17,7 @@ export const campaignData = {
budget: {
type: CampaignBudgetType.SPEND,
limit: 1000,
currency_code: "USD",
currency_code: "usd",
},
}
@@ -31,7 +31,7 @@ export const campaignsData = [
budget: {
type: CampaignBudgetType.SPEND,
limit: 1000,
currency_code: "USD",
currency_code: "usd",
},
},
{
@@ -56,7 +56,7 @@ const promotionData = {
target_type: "items",
type: "fixed",
allocation: "each",
currency_code: "USD",
currency_code: "usd",
value: 100,
max_quantity: 100,
target_rules: [
@@ -122,7 +122,7 @@ medusaIntegrationTestRunner({
value: 100,
max_quantity: 100,
target_rules: [],
currency_code: "USD",
currency_code: "usd",
},
rules: [],
}

View File

@@ -2071,6 +2071,56 @@ medusaIntegrationTestRunner({
)
})
it("should not add a promotion that belongs to a different currency than the cart", async () => {
const newPromotion = (
await api.post(
`/admin/promotions`,
{
code: "PROMOTION_TEST",
type: PromotionType.STANDARD,
status: PromotionStatus.ACTIVE,
application_method: {
type: "fixed",
target_type: "items",
allocation: "across",
// Set for EUR currency, different from USD Currency of the cart
currency_code: "eur",
value: 1000,
apply_to_quantity: 1,
},
},
adminHeaders
)
).data.promotion
await api.post(
`/store/carts/${cart.id}/line-items`,
{
variant_id: product.variants[0].id,
quantity: 1,
},
storeHeaders
)
let updated = await api.post(
`/store/carts/${cart.id}`,
{ promo_codes: [newPromotion.code] },
storeHeaders
)
expect(updated.status).toEqual(200)
expect(updated.data.cart).toEqual(
expect.objectContaining({
id: cart.id,
items: [
expect.objectContaining({
adjustments: [],
}),
],
})
)
})
it("should not generate tax lines if automatic taxes is false", async () => {
let updated = await api.post(
`/store/carts/${cart.id}`,

View File

@@ -31,7 +31,7 @@ const standardPromotionPayload = {
target_type: "items",
type: "fixed",
allocation: "each",
currency_code: "USD",
currency_code: "usd",
value: 100,
max_quantity: 100,
target_rules: [
@@ -87,7 +87,7 @@ medusaIntegrationTestRunner({
target_type: "items",
value: 100,
target_rules: [promotionRule],
currency_code: "USD",
currency_code: "usd",
},
rules: [promotionRule],
},
@@ -180,7 +180,7 @@ medusaIntegrationTestRunner({
type: "fixed",
target_type: "order",
value: 100,
currency_code: "USD",
currency_code: "usd",
},
},
adminHeaders
@@ -322,7 +322,7 @@ medusaIntegrationTestRunner({
allocation: "each",
value: 100,
max_quantity: 100,
currency_code: "USD",
currency_code: "usd",
target_rules: [
{
attribute: "test.test",
@@ -364,7 +364,7 @@ medusaIntegrationTestRunner({
allocation: "each",
value: 100,
max_quantity: 100,
currency_code: "USD",
currency_code: "usd",
buy_rules: [
{
attribute: "test.test",
@@ -415,7 +415,7 @@ medusaIntegrationTestRunner({
max_quantity: 100,
apply_to_quantity: 1,
buy_rules_min_quantity: 1,
currency_code: "USD",
currency_code: "usd",
target_rules: [
{
attribute: "test.test",
@@ -557,7 +557,7 @@ medusaIntegrationTestRunner({
target_type: "items",
type: "fixed",
allocation: "each",
currency_code: "USD",
currency_code: "usd",
value: 100,
max_quantity: 100,
},
@@ -683,7 +683,7 @@ medusaIntegrationTestRunner({
target_type: "items",
type: "fixed",
allocation: "across",
currency_code: "DKK",
currency_code: "dkk",
value: 100,
},
},
@@ -727,11 +727,11 @@ medusaIntegrationTestRunner({
).data.cart
/**
* Orignal total -> 1300 DKK (tax incl.)
* Orignal total -> 1300 dkk (tax incl.)
* Tax rate -> 25%
* Promotion -> FIXED 100 DKK (tax incl.)
* Promotion -> FIXED 100 dkk (tax incl.)
*
* We want total to be 1300 DKK - 100 DKK = 1200 DKK
* We want total to be 1300 dkk - 100 dkk = 1200 dkk
*/
expect(cart).toEqual(
expect.objectContaining({
@@ -812,11 +812,11 @@ medusaIntegrationTestRunner({
).data.order
/**
* Orignal total -> 1300 DKK (tax incl.)
* Orignal total -> 1300 dkk (tax incl.)
* Tax rate -> 25%
* Promotion -> FIXED 100 DKK (tax incl.)
* Promotion -> FIXED 100 dkk (tax incl.)
*
* We want total to be 1300 DKK - 100 DKK = 1200 DKK
* We want total to be 1300 dkk - 100 dkk = 1200 dkk
*/
expect(order).toEqual(
expect.objectContaining({
@@ -972,7 +972,7 @@ medusaIntegrationTestRunner({
target_type: "items",
type: "fixed",
allocation: "each",
currency_code: "DKK",
currency_code: "dkk",
value: 100,
max_quantity: 2,
},
@@ -1022,11 +1022,11 @@ medusaIntegrationTestRunner({
).data.cart
/**
* Orignal total -> 1500 DKK (tax incl.)
* Promotion -> FIXED 100 DKK per item (tax incl.)
* Orignal total -> 1500 dkk (tax incl.)
* Promotion -> FIXED 100 dkk per item (tax incl.)
* Tax rate -> 25%
*
* We want total to be 1500 DKK - 100 DKK - 100 DKK = 1300 DKK
* We want total to be 1500 dkk - 100 dkk - 100 dkk = 1300 dkk
*/
expect(cart).toEqual(
expect.objectContaining({
@@ -1036,7 +1036,7 @@ medusaIntegrationTestRunner({
subtotal: 1200, // taxable base (item subtotal - discount subtotal) = 1200 - 200 = 1000
tax_total: 260,
discount_total: 200, // 2 * 100 DKK fixed tax inclusive
discount_total: 200, // 2 * 100 dkk fixed tax inclusive
discount_subtotal: 160,
discount_tax_total: 40,
@@ -1127,11 +1127,11 @@ medusaIntegrationTestRunner({
).data.order
/**
* Orignal total -> 1500 DKK (tax incl.)
* Promotion -> FIXED 100 DKK per item (tax incl.)
* Orignal total -> 1500 dkk (tax incl.)
* Promotion -> FIXED 100 dkk per item (tax incl.)
* Tax rate -> 25%
*
* We want total to be 1500 DKK - 100 DKK - 100 DKK = 1300 DKK
* We want total to be 1500 dkk - 100 dkk - 100 dkk = 1300 dkk
*/
expect(order).toEqual(
expect.objectContaining({
@@ -1141,7 +1141,7 @@ medusaIntegrationTestRunner({
subtotal: 1200, // taxable base (item subtotal - discount subtotal) = 1200 - 200 = 1000
tax_total: 260,
discount_total: 200, // 2 * 100 DKK fixed tax inclusive
discount_total: 200, // 2 * 100 dkk fixed tax inclusive
discount_subtotal: 160,
discount_tax_total: 40,
@@ -1263,7 +1263,7 @@ medusaIntegrationTestRunner({
target_type: "items",
type: "fixed",
allocation: "across",
currency_code: "DKK",
currency_code: "dkk",
value: 100,
},
},
@@ -1307,9 +1307,9 @@ medusaIntegrationTestRunner({
).data.cart
/**
* Orignal total -> 1300 DKK (tax incl.)
* Orignal total -> 1300 dkk (tax incl.)
* Tax rate -> 25%
* Promotion -> FIXED 100 DKK (tax exclusive !)
* Promotion -> FIXED 100 dkk (tax exclusive !)
*/
expect(cart).toEqual(
expect.objectContaining({
@@ -1388,9 +1388,9 @@ medusaIntegrationTestRunner({
).data.order
/**
* Orignal total -> 1300 DKK (tax incl.)
* Orignal total -> 1300 dkk (tax incl.)
* Tax rate -> 25%
* Promotion -> FIXED 100 DKK (tax exclusive !)
* Promotion -> FIXED 100 dkk (tax exclusive !)
*/
expect(order).toEqual(
expect.objectContaining({
@@ -1500,7 +1500,7 @@ medusaIntegrationTestRunner({
target_type: "items",
type: "fixed",
allocation: "across",
currency_code: "DKK",
currency_code: "dkk",
value: 100,
},
},
@@ -1544,9 +1544,9 @@ medusaIntegrationTestRunner({
).data.cart
/**
* Orignal total -> 1300 DKK (tax excl.)
* Orignal total -> 1300 dkk (tax excl.)
* Tax rate -> 25%
* Promotion -> FIXED 100 DKK (tax exclusive !)
* Promotion -> FIXED 100 dkk (tax exclusive !)
*/
expect(cart).toEqual(
expect.objectContaining({
@@ -1625,9 +1625,9 @@ medusaIntegrationTestRunner({
).data.order
/**
* Orignal total -> 1300 DKK (tax excl.)
* Orignal total -> 1300 dkk (tax excl.)
* Tax rate -> 25%
* Promotion -> FIXED 100 DKK (tax exclusive !)
* Promotion -> FIXED 100 dkk (tax exclusive !)
*/
expect(order).toEqual(
expect.objectContaining({
@@ -2053,7 +2053,7 @@ medusaIntegrationTestRunner({
buy_rules_min_quantity: 1,
buy_rules: [promotionRule],
target_rules: [promotionRule],
currency_code: "USD",
currency_code: "usd",
},
rules: [promotionRule],
},
@@ -2216,7 +2216,7 @@ medusaIntegrationTestRunner({
type: PromotionType.BUYGET,
application_method: {
type: "fixed",
currency_code: "USD",
currency_code: "usd",
target_type: "items",
allocation: "across",
value: 100,

View File

@@ -240,11 +240,6 @@ medusaIntegrationTestRunner({
operator: "in",
values: ["cus_test"],
},
{
attribute: "currency_code",
operator: "in",
values: ["eur"],
},
],
application_method: {
type: "fixed",
@@ -275,11 +270,6 @@ medusaIntegrationTestRunner({
operator: "in",
values: ["cus_test"],
},
{
attribute: "currency_code",
operator: "in",
values: ["eur"],
},
],
application_method: {
type: "fixed",
@@ -300,7 +290,7 @@ medusaIntegrationTestRunner({
])
const cart = await cartModuleService.createCarts({
currency_code: "eur",
currency_code: "usd",
customer_id: "cus_test",
items: [
{

View File

@@ -191,11 +191,6 @@ medusaIntegrationTestRunner({
operator: "in",
values: ["cus_test"],
},
{
attribute: "currency_code",
operator: "in",
values: ["eur"],
},
],
application_method: {
type: "fixed",
@@ -225,11 +220,6 @@ medusaIntegrationTestRunner({
operator: "in",
values: ["cus_test"],
},
{
attribute: "currency_code",
operator: "in",
values: ["eur"],
},
],
application_method: {
type: "fixed",
@@ -249,7 +239,7 @@ medusaIntegrationTestRunner({
})
const cart = await cartModuleService.createCarts({
currency_code: "eur",
currency_code: "usd",
customer_id: "cus_test",
items: [
{

View File

@@ -11,7 +11,7 @@ export const defaultCampaignsData = [
budget: {
type: CampaignBudgetType.SPEND,
limit: 1000,
currency_code: "USD",
currency_code: "usd",
used: 0,
},
},
@@ -25,7 +25,7 @@ export const defaultCampaignsData = [
budget: {
type: CampaignBudgetType.USAGE,
limit: 1000,
currency_code: "USD",
currency_code: "usd",
used: 0,
},
},

View File

@@ -7,7 +7,7 @@ export const defaultPromotionsData: CreatePromotionDTO[] = [
code: "PROMOTION_1",
type: PromotionType.STANDARD,
application_method: {
currency_code: "USD",
currency_code: "usd",
target_type: "items",
type: "fixed",
allocation: "across",
@@ -19,7 +19,7 @@ export const defaultPromotionsData: CreatePromotionDTO[] = [
code: "PROMOTION_2",
type: PromotionType.STANDARD,
application_method: {
currency_code: "USD",
currency_code: "usd",
target_type: "items",
type: "fixed",
allocation: "across",

View File

@@ -59,7 +59,7 @@ export async function createDefaultPromotion(
campaign_id: "campaign-id-1",
...promotion,
application_method: {
currency_code: "USD",
currency_code: "usd",
target_type: "items",
type: "fixed",
allocation: "across",

View File

@@ -2694,6 +2694,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions(["PROMOTION_TEST"], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -2743,6 +2744,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions(["PROMOTION_TEST"], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -2816,6 +2818,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions([], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -2891,6 +2894,7 @@ moduleIntegrationTestRunner({
const result = await service.computeActions(
[],
{
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -2980,6 +2984,7 @@ moduleIntegrationTestRunner({
const result = await service.computeActions(
["PROMOTION_TEST", "PROMOTION_TEST_2"],
{
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -3092,6 +3097,7 @@ moduleIntegrationTestRunner({
const result = await service.computeActions(
["PROMOTION_TEST", "PROMOTION_TEST_2"],
{
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -3177,6 +3183,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions(["PROMOTION_TEST"], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -3230,6 +3237,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions(["PROMOTION_TEST"], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -3280,6 +3288,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions(["PROMOTION_TEST"], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -3352,6 +3361,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions([], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -3449,6 +3459,7 @@ moduleIntegrationTestRunner({
const result = await service.computeActions(
["PROMOTION_TEST", "PROMOTION_TEST_2"],
{
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -3559,6 +3570,7 @@ moduleIntegrationTestRunner({
const result = await service.computeActions(
["PROMOTION_TEST", "PROMOTION_TEST_2"],
{
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -3631,6 +3643,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions(["PROMOTION_TEST"], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -3683,6 +3696,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions(["PROMOTION_TEST"], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -3731,6 +3745,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions(["PROMOTION_TEST"], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -3803,6 +3818,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions([], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -3900,6 +3916,7 @@ moduleIntegrationTestRunner({
const result = await service.computeActions(
["PROMOTION_TEST", "PROMOTION_TEST_2"],
{
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -4010,6 +4027,7 @@ moduleIntegrationTestRunner({
const result = await service.computeActions(
["PROMOTION_TEST", "PROMOTION_TEST_2"],
{
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -4088,6 +4106,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions(["PROMOTION_TEST"], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -4140,6 +4159,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions(["PROMOTION_TEST"], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -4183,6 +4203,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions(["PROMOTION_TEST"], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -4252,6 +4273,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions([], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -4260,6 +4282,7 @@ moduleIntegrationTestRunner({
items: [
{
id: "item_cotton_tshirt",
is_discountable: true,
quantity: 1,
subtotal: 100,
product_category: {
@@ -4271,6 +4294,7 @@ moduleIntegrationTestRunner({
},
{
id: "item_cotton_sweater",
is_discountable: true,
quantity: 2,
subtotal: 300,
product_category: {
@@ -4351,6 +4375,7 @@ moduleIntegrationTestRunner({
id: "item_cotton_tshirt",
quantity: 1,
subtotal: 50,
is_discountable: true,
product_category: {
id: "catg_cotton",
},
@@ -4362,6 +4387,7 @@ moduleIntegrationTestRunner({
id: "item_cotton_sweater",
quantity: 1,
subtotal: 150,
is_discountable: true,
product_category: {
id: "catg_cotton",
},
@@ -4461,6 +4487,7 @@ moduleIntegrationTestRunner({
product: {
id: "prod_tshirt",
},
is_discountable: true,
},
{
id: "item_cotton_sweater",
@@ -4472,6 +4499,7 @@ moduleIntegrationTestRunner({
product: {
id: "prod_sweater",
},
is_discountable: true,
},
],
}
@@ -4527,6 +4555,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions(["PROMOTION_TEST"], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -4537,6 +4566,7 @@ moduleIntegrationTestRunner({
id: "item_cotton_tshirt",
quantity: 1,
subtotal: 100,
is_discountable: true,
product_category: {
id: "catg_cotton",
},
@@ -4554,6 +4584,7 @@ moduleIntegrationTestRunner({
id: "item_cotton_sweater",
quantity: 5,
subtotal: 750,
is_discountable: true,
product_category: {
id: "catg_cotton",
},
@@ -4617,6 +4648,7 @@ moduleIntegrationTestRunner({
})
const result = await service.computeActions(["PROMOTION_TEST"], {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -4678,6 +4710,7 @@ moduleIntegrationTestRunner({
describe("when promotion of type buyget", () => {
it("should compute adjustment when target and buy rules match", async () => {
const context = {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -4771,6 +4804,7 @@ moduleIntegrationTestRunner({
it("should return empty array when conditions for minimum qty aren't met", async () => {
const context = {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -4857,6 +4891,7 @@ moduleIntegrationTestRunner({
it("should compute actions for multiple items when conditions for target qty exceed one item", async () => {
const context = {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -4957,6 +4992,7 @@ moduleIntegrationTestRunner({
it("should return empty array when target rules arent met with context", async () => {
const context = {
currency_code: "usd",
customer: {
customer_group: {
id: "VIP",
@@ -5332,24 +5368,28 @@ moduleIntegrationTestRunner({
quantity: 1,
subtotal: 500,
product: { id: product1 },
is_discountable: true,
},
{
id: "item_cotton_tshirt1",
quantity: 1,
subtotal: 500,
product: { id: product1 },
is_discountable: true,
},
{
id: "item_cotton_tshirt2",
quantity: 1,
subtotal: 1000,
product: { id: product1 },
is_discountable: true,
},
{
id: "item_cotton_tshirt3",
quantity: 1,
subtotal: 1000,
product: { id: product1 },
is_discountable: true,
},
],
}
@@ -5384,6 +5424,7 @@ moduleIntegrationTestRunner({
quantity: 3,
subtotal: 1000,
product: { id: product1 },
is_discountable: true,
},
],
}
@@ -5405,18 +5446,21 @@ moduleIntegrationTestRunner({
quantity: 1,
subtotal: 1000,
product: { id: product1 },
is_discountable: true,
},
{
id: "item_cotton_tshirt1",
quantity: 1,
subtotal: 1000,
product: { id: product1 },
is_discountable: true,
},
{
id: "item_cotton_tshirt2",
quantity: 1,
subtotal: 1000,
product: { id: product1 },
is_discountable: true,
},
],
}

View File

@@ -154,7 +154,7 @@ moduleIntegrationTestRunner({
ends_at: endsAt,
budget: {
type: CampaignBudgetType.SPEND,
currency_code: "USD",
currency_code: "usd",
used: 100,
limit: 100,
},
@@ -179,7 +179,7 @@ moduleIntegrationTestRunner({
ends_at: endsAt,
budget: expect.objectContaining({
type: CampaignBudgetType.SPEND,
currency_code: "USD",
currency_code: "usd",
used: 100,
limit: 100,
}),

View File

@@ -520,13 +520,17 @@ export default class PromotionModuleService
continue
}
const isCurrencyCodeValid =
!isDefined(applicationMethod.currency_code) ||
applicationContext.currency_code === applicationMethod.currency_code
const isPromotionApplicable = areRulesValidForContext(
promotionRules,
applicationContext,
ApplicationMethodTargetType.ORDER
)
if (!isPromotionApplicable) {
if (!isPromotionApplicable || !isCurrencyCodeValid) {
continue
}