Skip to content

Commit

Permalink
Merge pull request #6804 from reactioncommerce/fix/prevent-applied-co…
Browse files Browse the repository at this point in the history
…upon-when-is-archived

fix: prevent applied coupon when is archived
  • Loading branch information
zenweasel committed Feb 20, 2023
2 parents 7d6032c + b2297cd commit d4578d7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export default async function applyCouponToCart(context, input) {
$or: [
{ expirationDate: { $gte: now } },
{ expirationDate: null }
]
],
isArchived: { $ne: true }
}).toArray();
if (coupons.length > 1) {
throw new ReactionError("invalid-params", "The coupon have duplicate with other promotion. Please contact admin for more information");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export default async function createStandardCoupon(context, input) {
const promotion = await Promotions.findOne({ _id: promotionId, shopId });
if (!promotion) throw new ReactionError("not-found", "Promotion not found");

if (promotion.triggerType !== "explicit") {
throw new ReactionError("invalid-params", "Coupon can only be created for explicit promotions");
}

const existsCoupons = await Coupons.find({ code, shopId, isArchived: { $ne: true } }).toArray();
if (existsCoupons.length > 0) {
const promotionIds = _.map(existsCoupons, "promotionId");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test("throws if validation check fails", async () => {
test("throws error when coupon code already created", async () => {
const input = { name: "test", code: "CODE", shopId: "123", promotionId: "123", canUseInStore: true };
const coupon = { _id: "123", code: "CODE", promotionId: "promotionId" };
const promotion = { _id: "promotionId" };
const promotion = { _id: "promotionId", triggerType: "explicit" };
mockContext.collections = {
Promotions: {
findOne: jest.fn().mockResolvedValueOnce(Promise.resolve(promotion)),
Expand Down Expand Up @@ -57,11 +57,30 @@ test("throws error when promotion does not exist", async () => {
}
});

test("throws error when promotion is not explicit", async () => {
const input = { name: "test", code: "CODE", shopId: "123", promotionId: "123", canUseInStore: true };
const promotion = { _id: "123", triggerType: "automatic" };
mockContext.collections = {
Coupons: {
findOne: jest.fn().mockResolvedValueOnce(Promise.resolve(null))
},
Promotions: {
findOne: jest.fn().mockResolvedValueOnce(Promise.resolve(promotion))
}
};

try {
await createStandardCoupon(mockContext, input);
} catch (error) {
expect(error.message).toEqual("Coupon can only be created for explicit promotions");
}
});

test("throws error when coupon code already exists in promotion window", async () => {
const now = new Date();
const input = { name: "test", code: "CODE", shopId: "123", promotionId: "123", canUseInStore: true };
const promotion = { _id: "123", startDate: now, endDate: now };
const existsPromotion = { _id: "1234", startDate: now, endDate: now };
const promotion = { _id: "123", startDate: now, endDate: now, triggerType: "explicit" };
const existsPromotion = { _id: "1234", startDate: now, endDate: now, triggerType: "explicit" };
const coupon = { _id: "123", code: "CODE", promotionId: "123" };
mockContext.collections = {
Coupons: {
Expand All @@ -88,7 +107,7 @@ test("throws error when coupon code already exists in promotion window", async (
test("should insert a new coupon and return the created results", async () => {
const now = new Date();
const input = { name: "test", code: "CODE", shopId: "123", promotionId: "123", canUseInStore: true };
const promotion = { _id: "123", endDate: now };
const promotion = { _id: "123", endDate: now, triggerType: "explicit" };

mockContext.collections = {
Coupons: {
Expand Down

0 comments on commit d4578d7

Please sign in to comment.