Skip to content

Commit acf6bbc

Browse files
willbouchwilliam bouchardcarlos-r-l-rodrigues
authored
chore(core-flows): throw error on invalid promo code (#13140)
* chore(core-flows): throw error on invalid promo code * add changelog * better error handling in test --------- Co-authored-by: william bouchard <[email protected]> Co-authored-by: Carlos R. L. Rodrigues <[email protected]>
1 parent ee0b873 commit acf6bbc

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

.changeset/ninety-kangaroos-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/core-flows": minor
3+
---
4+
5+
chore(code-flows): throw error on invalid promo code

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2815,6 +2815,24 @@ medusaIntegrationTestRunner({
28152815
)
28162816
})
28172817

2818+
it("should throw an error when adding a promotion that does not exist", async () => {
2819+
const invalidPromoCode = "SOME_INVALID_PROMO_CODE"
2820+
2821+
const { response } = await api
2822+
.post(
2823+
`/store/carts/${cart.id}/promotions`,
2824+
{ promo_codes: [invalidPromoCode] },
2825+
storeHeaders
2826+
)
2827+
.catch((e) => e)
2828+
2829+
expect(response.status).toEqual(400)
2830+
expect(response.data.type).toEqual("invalid_data")
2831+
expect(response.data.message).toEqual(
2832+
`The promotion code ${invalidPromoCode} is invalid`
2833+
)
2834+
})
2835+
28182836
it("should remove promotion adjustments when promotion is deleted", async () => {
28192837
let cartBeforeRemovingPromotion = (
28202838
await api.get(`/store/carts/${cart.id}`, storeHeaders)

packages/core/core-flows/src/cart/steps/get-promotion-codes-to-apply.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { IPromotionModuleService } from "@medusajs/framework/types"
2-
import { Modules, PromotionActions } from "@medusajs/framework/utils"
3-
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
2+
import {
3+
MedusaError,
4+
Modules,
5+
PromotionActions,
6+
} from "@medusajs/framework/utils"
7+
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
48

59
/**
610
* The details of the promotion codes to apply on a cart.
@@ -68,14 +72,12 @@ export const getPromotionCodesToApply = createStep(
6872
async (data: GetPromotionCodesToApplyStepInput, { container }) => {
6973
const { promo_codes = [], cart, action = PromotionActions.ADD } = data
7074
const { items = [], shipping_methods = [] } = cart
71-
const adjustmentCodes: string[] = []
7275
const promotionService = container.resolve<IPromotionModuleService>(
7376
Modules.PROMOTION
7477
)
7578

76-
const objects = items.concat(shipping_methods)
77-
78-
objects.forEach((object) => {
79+
const adjustmentCodes: string[] = []
80+
items.concat(shipping_methods).forEach((object) => {
7981
object.adjustments?.forEach((adjustment) => {
8082
if (adjustment.code && !adjustmentCodes.includes(adjustment.code)) {
8183
adjustmentCodes.push(adjustment.code)
@@ -94,17 +96,38 @@ export const getPromotionCodesToApply = createStep(
9496
: []
9597
)
9698

97-
if (action === PromotionActions.ADD) {
98-
promo_codes.forEach((code) => promotionCodesToApply.add(code))
99-
}
100-
10199
if (action === PromotionActions.REMOVE) {
102100
promo_codes.forEach((code) => promotionCodesToApply.delete(code))
103101
}
104102

105103
if (action === PromotionActions.REPLACE) {
106104
promotionCodesToApply.clear()
107-
promo_codes.forEach((code) => promotionCodesToApply.add(code))
105+
}
106+
107+
if (
108+
action === PromotionActions.ADD ||
109+
action === PromotionActions.REPLACE
110+
) {
111+
const validPromoCodes: Set<string> = new Set(
112+
promo_codes.length
113+
? (
114+
await promotionService.listPromotions(
115+
{ code: promo_codes },
116+
{ select: ["code"] }
117+
)
118+
).map((p) => p.code!)
119+
: []
120+
)
121+
122+
promo_codes.forEach((code) => {
123+
if (!validPromoCodes.has(code)) {
124+
throw new MedusaError(
125+
MedusaError.Types.INVALID_DATA,
126+
`The promotion code ${code} is invalid`
127+
)
128+
}
129+
promotionCodesToApply.add(code)
130+
})
108131
}
109132

110133
return new StepResponse(

0 commit comments

Comments
 (0)