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 <williambouchard@williams-MacBook-Pro.local> Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
3 files changed
+57
-11
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/core-flows": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
chore(code-flows): throw error on invalid promo code
|
||||||
@@ -2815,6 +2815,24 @@ medusaIntegrationTestRunner({
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should throw an error when adding a promotion that does not exist", async () => {
|
||||||
|
const invalidPromoCode = "SOME_INVALID_PROMO_CODE"
|
||||||
|
|
||||||
|
const { response } = await api
|
||||||
|
.post(
|
||||||
|
`/store/carts/${cart.id}/promotions`,
|
||||||
|
{ promo_codes: [invalidPromoCode] },
|
||||||
|
storeHeaders
|
||||||
|
)
|
||||||
|
.catch((e) => e)
|
||||||
|
|
||||||
|
expect(response.status).toEqual(400)
|
||||||
|
expect(response.data.type).toEqual("invalid_data")
|
||||||
|
expect(response.data.message).toEqual(
|
||||||
|
`The promotion code ${invalidPromoCode} is invalid`
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
it("should remove promotion adjustments when promotion is deleted", async () => {
|
it("should remove promotion adjustments when promotion is deleted", async () => {
|
||||||
let cartBeforeRemovingPromotion = (
|
let cartBeforeRemovingPromotion = (
|
||||||
await api.get(`/store/carts/${cart.id}`, storeHeaders)
|
await api.get(`/store/carts/${cart.id}`, storeHeaders)
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
import { IPromotionModuleService } from "@medusajs/framework/types"
|
import { IPromotionModuleService } from "@medusajs/framework/types"
|
||||||
import { Modules, PromotionActions } from "@medusajs/framework/utils"
|
import {
|
||||||
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
|
MedusaError,
|
||||||
|
Modules,
|
||||||
|
PromotionActions,
|
||||||
|
} from "@medusajs/framework/utils"
|
||||||
|
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The details of the promotion codes to apply on a cart.
|
* The details of the promotion codes to apply on a cart.
|
||||||
@@ -68,14 +72,12 @@ export const getPromotionCodesToApply = createStep(
|
|||||||
async (data: GetPromotionCodesToApplyStepInput, { container }) => {
|
async (data: GetPromotionCodesToApplyStepInput, { container }) => {
|
||||||
const { promo_codes = [], cart, action = PromotionActions.ADD } = data
|
const { promo_codes = [], cart, action = PromotionActions.ADD } = data
|
||||||
const { items = [], shipping_methods = [] } = cart
|
const { items = [], shipping_methods = [] } = cart
|
||||||
const adjustmentCodes: string[] = []
|
|
||||||
const promotionService = container.resolve<IPromotionModuleService>(
|
const promotionService = container.resolve<IPromotionModuleService>(
|
||||||
Modules.PROMOTION
|
Modules.PROMOTION
|
||||||
)
|
)
|
||||||
|
|
||||||
const objects = items.concat(shipping_methods)
|
const adjustmentCodes: string[] = []
|
||||||
|
items.concat(shipping_methods).forEach((object) => {
|
||||||
objects.forEach((object) => {
|
|
||||||
object.adjustments?.forEach((adjustment) => {
|
object.adjustments?.forEach((adjustment) => {
|
||||||
if (adjustment.code && !adjustmentCodes.includes(adjustment.code)) {
|
if (adjustment.code && !adjustmentCodes.includes(adjustment.code)) {
|
||||||
adjustmentCodes.push(adjustment.code)
|
adjustmentCodes.push(adjustment.code)
|
||||||
@@ -94,17 +96,38 @@ export const getPromotionCodesToApply = createStep(
|
|||||||
: []
|
: []
|
||||||
)
|
)
|
||||||
|
|
||||||
if (action === PromotionActions.ADD) {
|
|
||||||
promo_codes.forEach((code) => promotionCodesToApply.add(code))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (action === PromotionActions.REMOVE) {
|
if (action === PromotionActions.REMOVE) {
|
||||||
promo_codes.forEach((code) => promotionCodesToApply.delete(code))
|
promo_codes.forEach((code) => promotionCodesToApply.delete(code))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action === PromotionActions.REPLACE) {
|
if (action === PromotionActions.REPLACE) {
|
||||||
promotionCodesToApply.clear()
|
promotionCodesToApply.clear()
|
||||||
promo_codes.forEach((code) => promotionCodesToApply.add(code))
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
action === PromotionActions.ADD ||
|
||||||
|
action === PromotionActions.REPLACE
|
||||||
|
) {
|
||||||
|
const validPromoCodes: Set<string> = new Set(
|
||||||
|
promo_codes.length
|
||||||
|
? (
|
||||||
|
await promotionService.listPromotions(
|
||||||
|
{ code: promo_codes },
|
||||||
|
{ select: ["code"] }
|
||||||
|
)
|
||||||
|
).map((p) => p.code!)
|
||||||
|
: []
|
||||||
|
)
|
||||||
|
|
||||||
|
promo_codes.forEach((code) => {
|
||||||
|
if (!validPromoCodes.has(code)) {
|
||||||
|
throw new MedusaError(
|
||||||
|
MedusaError.Types.INVALID_DATA,
|
||||||
|
`The promotion code ${code} is invalid`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
promotionCodesToApply.add(code)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return new StepResponse(
|
return new StepResponse(
|
||||||
|
|||||||
Reference in new issue
Block a user