+
Skip to content

feat: update coupon trigger parameters #6770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Coupon } from "../simpleSchemas.js";
const inputSchema = new SimpleSchema({
shopId: String,
promotionId: String,
name: String,
code: String,
canUseInStore: Boolean,
maxUsageTimesPerUser: {
Expand Down Expand Up @@ -50,6 +51,7 @@ export default async function createStandardCoupon(context, input) {
const now = new Date();
const coupon = {
_id: Random.id(),
name: input.name,
code: input.code,
shopId,
promotionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test("throws if validation check fails", async () => {
});

test("throws error when coupon code already created", async () => {
const input = { code: "CODE", shopId: "123", promotionId: "123", canUseInStore: true };
const input = { name: "test", code: "CODE", shopId: "123", promotionId: "123", canUseInStore: true };
const coupon = { _id: "123", code: "CODE", promotionId: "promotionId" };
const promotion = { _id: "promotionId" };
mockContext.collections = {
Expand Down Expand Up @@ -40,7 +40,7 @@ test("throws error when coupon code already created", async () => {
});

test("throws error when promotion does not exist", async () => {
const input = { code: "CODE", shopId: "123", promotionId: "123", canUseInStore: true };
const input = { name: "test", code: "CODE", shopId: "123", promotionId: "123", canUseInStore: true };
mockContext.collections = {
Coupons: {
findOne: jest.fn().mockResolvedValueOnce(Promise.resolve(null))
Expand All @@ -59,7 +59,7 @@ test("throws error when promotion does not exist", async () => {

test("throws error when coupon code already exists in promotion window", async () => {
const now = new Date();
const input = { code: "CODE", shopId: "123", promotionId: "123", canUseInStore: true };
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 coupon = { _id: "123", code: "CODE", promotionId: "123" };
Expand All @@ -86,7 +86,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 = { code: "CODE", shopId: "123", promotionId: "123", canUseInStore: true };
const input = { name: "test", code: "CODE", shopId: "123", promotionId: "123", canUseInStore: true };
const promotion = { _id: "123", endDate: now };

mockContext.collections = {
Expand All @@ -112,6 +112,7 @@ test("should insert a new coupon and return the created results", async () => {
coupon: {
_id: "123",
canUseInStore: true,
name: "test",
code: "CODE",
createdAt: jasmine.any(Date),
expirationDate: now,
Expand Down
11 changes: 10 additions & 1 deletion packages/api-plugin-promotions-coupons/src/preStartup.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import _ from "lodash";
import SimpleSchema from "simpl-schema";
import { CouponTriggerCondition, CouponTriggerParameters } from "./simpleSchemas.js";

/**
* @summary This is a preStartup function that is called before the app starts up.
* @param {Object} context - The application context
* @returns {undefined}
*/
export default async function preStartupPromotionCoupon(context) {
const { simpleSchemas: { Cart, Promotion }, promotions: pluginPromotions } = context;
const { simpleSchemas: { Cart, Promotion, RuleExpression }, promotions: pluginPromotions } = context;

CouponTriggerCondition.extend({
conditions: RuleExpression
});

CouponTriggerParameters.extend({
conditions: RuleExpression
});

// because we're reusing the offer trigger, we need to promotion-discounts plugin to be installed first
const offerTrigger = pluginPromotions.triggers.find((trigger) => trigger.key === "offers");
Expand Down
45 changes: 4 additions & 41 deletions packages/api-plugin-promotions-coupons/src/schemas/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type Coupon {
"The coupon owner ID"
userId: ID

"The coupon name"
name: String!

"The coupon code"
code: String!

Expand Down Expand Up @@ -64,48 +67,8 @@ input CreateStandardCouponInput {
"The promotion ID"
promotionId: ID!

"The coupon code"
code: String!

"Can use this coupon in the store"
canUseInStore: Boolean!

"The number of times this coupon can be used per user"
maxUsageTimesPerUser: Int

"The number of times this coupon can be used"
maxUsageTimes: Int
}

input CouponQueryInput {
"The unique ID of the coupon"
_id: String!

"The unique ID of the shop"
shopId: String!
}

input CouponFilter {
"The expiration date of the coupon"
expirationDate: Date

"The related promotion ID"
promotionId: ID

"The coupon code"
code: String

"The coupon name"
userId: ID
}

"The input for the createStandardCoupon mutation"
input CreateStandardCouponInput {
"The shop ID"
shopId: ID!

"The promotion ID"
promotionId: ID!
name: String!

"The coupon code"
code: String!
Expand Down
20 changes: 17 additions & 3 deletions packages/api-plugin-promotions-coupons/src/simpleSchemas.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import SimpleSchema from "simpl-schema";

export const CouponTriggerCondition = new SimpleSchema({
conditions: {
type: Object
}
});

export const CouponTriggerParameters = new SimpleSchema({
name: String,
couponCode: {
type: String
conditions: {
type: Object
},
inclusionRules: {
type: CouponTriggerCondition,
optional: true
},
exclusionRules: {
type: CouponTriggerCondition,
optional: true
}
});

export const Coupon = new SimpleSchema({
_id: String,
name: String,
code: String,
shopId: String,
promotionId: String,
Expand Down
5 changes: 3 additions & 2 deletions packages/api-plugin-promotions-offers/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import triggers from "./triggers/index.js";
import enhancers from "./enhancers/index.js";
import facts from "./facts/index.js";
import { promotionOfferFacts, registerPromotionOfferFacts } from "./registration.js";
import { ConditionRule } from "./simpleSchemas.js";
import { ConditionRule, RuleExpression } from "./simpleSchemas.js";
import preStartupPromotionOffer from "./preStartup.js";

const require = createRequire(import.meta.url);
Expand Down Expand Up @@ -32,7 +32,8 @@ export default async function register(app) {
},
promotionOfferFacts: facts,
simpleSchemas: {
ConditionRule
ConditionRule,
RuleExpression
}
});
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Random from "@reactioncommerce/random";
import ReactionError from "@reactioncommerce/reaction-error";
import validateActionParams from "./validateActionParams.js";
import validateTriggerParams from "./validateTriggerParams.js";

Expand All @@ -16,6 +17,7 @@ export default async function createPromotion(context, promotion) {
const [firstTrigger] = promotion.triggers; // currently support only one trigger
const { triggerKey } = firstTrigger;
const trigger = promotions.triggers.find((tr) => tr.key === triggerKey);
if (!trigger) throw new ReactionError("invalid-params", `No trigger found with key ${triggerKey}`);
promotion.triggerType = trigger.type;
}
promotion.state = "created";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,14 @@ test("will insert a record if it passes validation", async () => {
expect(error).toBeUndefined();
}
});

test("should throw error when triggerKey is not valid", async () => {
const promotion = _.cloneDeep(CreateOrderPromotion);
promotion.triggers[0].triggerKey = "invalid";
try {
await createPromotion(mockContext, promotion);
} catch (error) {
expect(error.error).toEqual("invalid-params");
expect(error.message).toEqual("No trigger found with key invalid");
}
});
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载