Skip to content

Commit 75320e7

Browse files
authored
fix(promotion): check currency when computing actions for promotions (medusajs#13084)
* fix(promotion): check currency when computing actions for promotions * chore: fix tests * chore: fix more specs
1 parent 54a74b0 commit 75320e7

File tree

12 files changed

+152
-69
lines changed

12 files changed

+152
-69
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/promotion": patch
3+
---
4+
5+
fix(promotion): check currency when computing actions for promotions

integration-tests/http/__tests__/campaigns/admin/campaigns.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const campaignData = {
1717
budget: {
1818
type: CampaignBudgetType.SPEND,
1919
limit: 1000,
20-
currency_code: "USD",
20+
currency_code: "usd",
2121
},
2222
}
2323

@@ -31,7 +31,7 @@ export const campaignsData = [
3131
budget: {
3232
type: CampaignBudgetType.SPEND,
3333
limit: 1000,
34-
currency_code: "USD",
34+
currency_code: "usd",
3535
},
3636
},
3737
{
@@ -56,7 +56,7 @@ const promotionData = {
5656
target_type: "items",
5757
type: "fixed",
5858
allocation: "each",
59-
currency_code: "USD",
59+
currency_code: "usd",
6060
value: 100,
6161
max_quantity: 100,
6262
target_rules: [
@@ -122,7 +122,7 @@ medusaIntegrationTestRunner({
122122
value: 100,
123123
max_quantity: 100,
124124
target_rules: [],
125-
currency_code: "USD",
125+
currency_code: "usd",
126126
},
127127
rules: [],
128128
}

integration-tests/http/__tests__/cart/store/cart.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,56 @@ medusaIntegrationTestRunner({
20712071
)
20722072
})
20732073

2074+
it("should not add a promotion that belongs to a different currency than the cart", async () => {
2075+
const newPromotion = (
2076+
await api.post(
2077+
`/admin/promotions`,
2078+
{
2079+
code: "PROMOTION_TEST",
2080+
type: PromotionType.STANDARD,
2081+
status: PromotionStatus.ACTIVE,
2082+
application_method: {
2083+
type: "fixed",
2084+
target_type: "items",
2085+
allocation: "across",
2086+
// Set for EUR currency, different from USD Currency of the cart
2087+
currency_code: "eur",
2088+
value: 1000,
2089+
apply_to_quantity: 1,
2090+
},
2091+
},
2092+
adminHeaders
2093+
)
2094+
).data.promotion
2095+
2096+
await api.post(
2097+
`/store/carts/${cart.id}/line-items`,
2098+
{
2099+
variant_id: product.variants[0].id,
2100+
quantity: 1,
2101+
},
2102+
storeHeaders
2103+
)
2104+
2105+
let updated = await api.post(
2106+
`/store/carts/${cart.id}`,
2107+
{ promo_codes: [newPromotion.code] },
2108+
storeHeaders
2109+
)
2110+
2111+
expect(updated.status).toEqual(200)
2112+
expect(updated.data.cart).toEqual(
2113+
expect.objectContaining({
2114+
id: cart.id,
2115+
items: [
2116+
expect.objectContaining({
2117+
adjustments: [],
2118+
}),
2119+
],
2120+
})
2121+
)
2122+
})
2123+
20742124
it("should not generate tax lines if automatic taxes is false", async () => {
20752125
let updated = await api.post(
20762126
`/store/carts/${cart.id}`,

integration-tests/http/__tests__/promotions/admin/promotions.spec.ts

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const standardPromotionPayload = {
3131
target_type: "items",
3232
type: "fixed",
3333
allocation: "each",
34-
currency_code: "USD",
34+
currency_code: "usd",
3535
value: 100,
3636
max_quantity: 100,
3737
target_rules: [
@@ -87,7 +87,7 @@ medusaIntegrationTestRunner({
8787
target_type: "items",
8888
value: 100,
8989
target_rules: [promotionRule],
90-
currency_code: "USD",
90+
currency_code: "usd",
9191
},
9292
rules: [promotionRule],
9393
},
@@ -180,7 +180,7 @@ medusaIntegrationTestRunner({
180180
type: "fixed",
181181
target_type: "order",
182182
value: 100,
183-
currency_code: "USD",
183+
currency_code: "usd",
184184
},
185185
},
186186
adminHeaders
@@ -322,7 +322,7 @@ medusaIntegrationTestRunner({
322322
allocation: "each",
323323
value: 100,
324324
max_quantity: 100,
325-
currency_code: "USD",
325+
currency_code: "usd",
326326
target_rules: [
327327
{
328328
attribute: "test.test",
@@ -364,7 +364,7 @@ medusaIntegrationTestRunner({
364364
allocation: "each",
365365
value: 100,
366366
max_quantity: 100,
367-
currency_code: "USD",
367+
currency_code: "usd",
368368
buy_rules: [
369369
{
370370
attribute: "test.test",
@@ -415,7 +415,7 @@ medusaIntegrationTestRunner({
415415
max_quantity: 100,
416416
apply_to_quantity: 1,
417417
buy_rules_min_quantity: 1,
418-
currency_code: "USD",
418+
currency_code: "usd",
419419
target_rules: [
420420
{
421421
attribute: "test.test",
@@ -557,7 +557,7 @@ medusaIntegrationTestRunner({
557557
target_type: "items",
558558
type: "fixed",
559559
allocation: "each",
560-
currency_code: "USD",
560+
currency_code: "usd",
561561
value: 100,
562562
max_quantity: 100,
563563
},
@@ -683,7 +683,7 @@ medusaIntegrationTestRunner({
683683
target_type: "items",
684684
type: "fixed",
685685
allocation: "across",
686-
currency_code: "DKK",
686+
currency_code: "dkk",
687687
value: 100,
688688
},
689689
},
@@ -727,11 +727,11 @@ medusaIntegrationTestRunner({
727727
).data.cart
728728

729729
/**
730-
* Orignal total -> 1300 DKK (tax incl.)
730+
* Orignal total -> 1300 dkk (tax incl.)
731731
* Tax rate -> 25%
732-
* Promotion -> FIXED 100 DKK (tax incl.)
732+
* Promotion -> FIXED 100 dkk (tax incl.)
733733
*
734-
* We want total to be 1300 DKK - 100 DKK = 1200 DKK
734+
* We want total to be 1300 dkk - 100 dkk = 1200 dkk
735735
*/
736736
expect(cart).toEqual(
737737
expect.objectContaining({
@@ -812,11 +812,11 @@ medusaIntegrationTestRunner({
812812
).data.order
813813

814814
/**
815-
* Orignal total -> 1300 DKK (tax incl.)
815+
* Orignal total -> 1300 dkk (tax incl.)
816816
* Tax rate -> 25%
817-
* Promotion -> FIXED 100 DKK (tax incl.)
817+
* Promotion -> FIXED 100 dkk (tax incl.)
818818
*
819-
* We want total to be 1300 DKK - 100 DKK = 1200 DKK
819+
* We want total to be 1300 dkk - 100 dkk = 1200 dkk
820820
*/
821821
expect(order).toEqual(
822822
expect.objectContaining({
@@ -972,7 +972,7 @@ medusaIntegrationTestRunner({
972972
target_type: "items",
973973
type: "fixed",
974974
allocation: "each",
975-
currency_code: "DKK",
975+
currency_code: "dkk",
976976
value: 100,
977977
max_quantity: 2,
978978
},
@@ -1022,11 +1022,11 @@ medusaIntegrationTestRunner({
10221022
).data.cart
10231023

10241024
/**
1025-
* Orignal total -> 1500 DKK (tax incl.)
1026-
* Promotion -> FIXED 100 DKK per item (tax incl.)
1025+
* Orignal total -> 1500 dkk (tax incl.)
1026+
* Promotion -> FIXED 100 dkk per item (tax incl.)
10271027
* Tax rate -> 25%
10281028
*
1029-
* We want total to be 1500 DKK - 100 DKK - 100 DKK = 1300 DKK
1029+
* We want total to be 1500 dkk - 100 dkk - 100 dkk = 1300 dkk
10301030
*/
10311031
expect(cart).toEqual(
10321032
expect.objectContaining({
@@ -1036,7 +1036,7 @@ medusaIntegrationTestRunner({
10361036
subtotal: 1200, // taxable base (item subtotal - discount subtotal) = 1200 - 200 = 1000
10371037
tax_total: 260,
10381038

1039-
discount_total: 200, // 2 * 100 DKK fixed tax inclusive
1039+
discount_total: 200, // 2 * 100 dkk fixed tax inclusive
10401040
discount_subtotal: 160,
10411041
discount_tax_total: 40,
10421042

@@ -1127,11 +1127,11 @@ medusaIntegrationTestRunner({
11271127
).data.order
11281128

11291129
/**
1130-
* Orignal total -> 1500 DKK (tax incl.)
1131-
* Promotion -> FIXED 100 DKK per item (tax incl.)
1130+
* Orignal total -> 1500 dkk (tax incl.)
1131+
* Promotion -> FIXED 100 dkk per item (tax incl.)
11321132
* Tax rate -> 25%
11331133
*
1134-
* We want total to be 1500 DKK - 100 DKK - 100 DKK = 1300 DKK
1134+
* We want total to be 1500 dkk - 100 dkk - 100 dkk = 1300 dkk
11351135
*/
11361136
expect(order).toEqual(
11371137
expect.objectContaining({
@@ -1141,7 +1141,7 @@ medusaIntegrationTestRunner({
11411141
subtotal: 1200, // taxable base (item subtotal - discount subtotal) = 1200 - 200 = 1000
11421142
tax_total: 260,
11431143

1144-
discount_total: 200, // 2 * 100 DKK fixed tax inclusive
1144+
discount_total: 200, // 2 * 100 dkk fixed tax inclusive
11451145
discount_subtotal: 160,
11461146
discount_tax_total: 40,
11471147

@@ -1263,7 +1263,7 @@ medusaIntegrationTestRunner({
12631263
target_type: "items",
12641264
type: "fixed",
12651265
allocation: "across",
1266-
currency_code: "DKK",
1266+
currency_code: "dkk",
12671267
value: 100,
12681268
},
12691269
},
@@ -1307,9 +1307,9 @@ medusaIntegrationTestRunner({
13071307
).data.cart
13081308

13091309
/**
1310-
* Orignal total -> 1300 DKK (tax incl.)
1310+
* Orignal total -> 1300 dkk (tax incl.)
13111311
* Tax rate -> 25%
1312-
* Promotion -> FIXED 100 DKK (tax exclusive !)
1312+
* Promotion -> FIXED 100 dkk (tax exclusive !)
13131313
*/
13141314
expect(cart).toEqual(
13151315
expect.objectContaining({
@@ -1388,9 +1388,9 @@ medusaIntegrationTestRunner({
13881388
).data.order
13891389

13901390
/**
1391-
* Orignal total -> 1300 DKK (tax incl.)
1391+
* Orignal total -> 1300 dkk (tax incl.)
13921392
* Tax rate -> 25%
1393-
* Promotion -> FIXED 100 DKK (tax exclusive !)
1393+
* Promotion -> FIXED 100 dkk (tax exclusive !)
13941394
*/
13951395
expect(order).toEqual(
13961396
expect.objectContaining({
@@ -1500,7 +1500,7 @@ medusaIntegrationTestRunner({
15001500
target_type: "items",
15011501
type: "fixed",
15021502
allocation: "across",
1503-
currency_code: "DKK",
1503+
currency_code: "dkk",
15041504
value: 100,
15051505
},
15061506
},
@@ -1544,9 +1544,9 @@ medusaIntegrationTestRunner({
15441544
).data.cart
15451545

15461546
/**
1547-
* Orignal total -> 1300 DKK (tax excl.)
1547+
* Orignal total -> 1300 dkk (tax excl.)
15481548
* Tax rate -> 25%
1549-
* Promotion -> FIXED 100 DKK (tax exclusive !)
1549+
* Promotion -> FIXED 100 dkk (tax exclusive !)
15501550
*/
15511551
expect(cart).toEqual(
15521552
expect.objectContaining({
@@ -1625,9 +1625,9 @@ medusaIntegrationTestRunner({
16251625
).data.order
16261626

16271627
/**
1628-
* Orignal total -> 1300 DKK (tax excl.)
1628+
* Orignal total -> 1300 dkk (tax excl.)
16291629
* Tax rate -> 25%
1630-
* Promotion -> FIXED 100 DKK (tax exclusive !)
1630+
* Promotion -> FIXED 100 dkk (tax exclusive !)
16311631
*/
16321632
expect(order).toEqual(
16331633
expect.objectContaining({
@@ -2053,7 +2053,7 @@ medusaIntegrationTestRunner({
20532053
buy_rules_min_quantity: 1,
20542054
buy_rules: [promotionRule],
20552055
target_rules: [promotionRule],
2056-
currency_code: "USD",
2056+
currency_code: "usd",
20572057
},
20582058
rules: [promotionRule],
20592059
},
@@ -2216,7 +2216,7 @@ medusaIntegrationTestRunner({
22162216
type: PromotionType.BUYGET,
22172217
application_method: {
22182218
type: "fixed",
2219-
currency_code: "USD",
2219+
currency_code: "usd",
22202220
target_type: "items",
22212221
allocation: "across",
22222222
value: 100,

integration-tests/modules/__tests__/cart/store/add-promotions-to-cart.spec.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,6 @@ medusaIntegrationTestRunner({
240240
operator: "in",
241241
values: ["cus_test"],
242242
},
243-
{
244-
attribute: "currency_code",
245-
operator: "in",
246-
values: ["eur"],
247-
},
248243
],
249244
application_method: {
250245
type: "fixed",
@@ -275,11 +270,6 @@ medusaIntegrationTestRunner({
275270
operator: "in",
276271
values: ["cus_test"],
277272
},
278-
{
279-
attribute: "currency_code",
280-
operator: "in",
281-
values: ["eur"],
282-
},
283273
],
284274
application_method: {
285275
type: "fixed",
@@ -300,7 +290,7 @@ medusaIntegrationTestRunner({
300290
])
301291

302292
const cart = await cartModuleService.createCarts({
303-
currency_code: "eur",
293+
currency_code: "usd",
304294
customer_id: "cus_test",
305295
items: [
306296
{

0 commit comments

Comments
 (0)