Browse Source

feat(core): Pass RequestContext to PromotionAction functions

BREAKING CHANGE: The PromotionAction `execute()`
function signature has changed: the first argument is now the
RequestContext of the current request.
Michael Bromley 5 years ago
parent
commit
0a35a1208a

+ 1 - 1
packages/core/src/config/promotion/actions/facet-values-discount-action.ts

@@ -24,7 +24,7 @@ export const discountOnItemWithFacets = new PromotionItemAction({
     init(injector) {
         facetValueChecker = new FacetValueChecker(injector.getConnection());
     },
-    async execute(orderItem, orderLine, args) {
+    async execute(ctx, orderItem, orderLine, args) {
         if (await facetValueChecker.hasFacetValues(orderLine, args.facets)) {
             return -orderItem.unitPriceWithPromotions * (args.discount / 100);
         }

+ 1 - 1
packages/core/src/config/promotion/actions/order-percentage-discount-action.ts

@@ -13,7 +13,7 @@ export const orderPercentageDiscount = new PromotionOrderAction({
             },
         },
     },
-    execute(order, args) {
+    execute(ctx, order, args) {
         return -order.subTotal * (args.discount / 100);
     },
     description: [{ languageCode: LanguageCode.en, value: 'Discount order by { discount }%' }],

+ 1 - 1
packages/core/src/config/promotion/actions/product-discount-action.ts

@@ -24,7 +24,7 @@ export const productsPercentageDiscount = new PromotionItemAction({
         },
     },
 
-    execute(orderItem, orderLine, args) {
+    execute(ctx, orderItem, orderLine, args) {
         if (lineContainsIds(args.productVariantIds, orderLine)) {
             return -orderItem.unitPriceWithPromotions * (args.discount / 100);
         }

+ 7 - 4
packages/core/src/config/promotion/promotion-action.ts

@@ -1,5 +1,6 @@
 import { ConfigArg } from '@vendure/common/lib/generated-types';
 
+import { RequestContext } from '../../api/common/request-context';
 import {
     ConfigArgs,
     ConfigArgValues,
@@ -19,6 +20,7 @@ import { Order } from '../../entity/order/order.entity';
  * @docsPage promotion-action
  */
 export type ExecutePromotionItemActionFn<T extends ConfigArgs> = (
+    ctx: RequestContext,
     orderItem: OrderItem,
     orderLine: OrderLine,
     args: ConfigArgValues<T>,
@@ -33,6 +35,7 @@ export type ExecutePromotionItemActionFn<T extends ConfigArgs> = (
  * @docsPage promotion-action
  */
 export type ExecutePromotionOrderActionFn<T extends ConfigArgs> = (
+    ctx: RequestContext,
     order: Order,
     args: ConfigArgValues<T>,
 ) => number | Promise<number>;
@@ -123,8 +126,8 @@ export class PromotionItemAction<T extends ConfigArgs = ConfigArgs> extends Prom
     }
 
     /** @internal */
-    execute(orderItem: OrderItem, orderLine: OrderLine, args: ConfigArg[]) {
-        return this.executeFn(orderItem, orderLine, this.argsArrayToHash(args));
+    execute(ctx: RequestContext, orderItem: OrderItem, orderLine: OrderLine, args: ConfigArg[]) {
+        return this.executeFn(ctx, orderItem, orderLine, this.argsArrayToHash(args));
     }
 }
 
@@ -157,7 +160,7 @@ export class PromotionOrderAction<T extends ConfigArgs = ConfigArgs> extends Pro
     }
 
     /** @internal */
-    execute(order: Order, args: ConfigArg[]) {
-        return this.executeFn(order, this.argsArrayToHash(args));
+    execute(ctx: RequestContext, order: Order, args: ConfigArg[]) {
+        return this.executeFn(ctx, order, this.argsArrayToHash(args));
     }
 }

+ 8 - 3
packages/core/src/entity/promotion/promotion.entity.ts

@@ -101,7 +101,10 @@ export class Promotion extends AdjustmentSource implements ChannelAware, SoftDel
      */
     @Column() priorityScore: number;
 
-    async apply(args: ApplyOrderActionArgs | ApplyOrderItemActionArgs): Promise<Adjustment | undefined> {
+    async apply(
+        ctx: RequestContext,
+        args: ApplyOrderActionArgs | ApplyOrderItemActionArgs,
+    ): Promise<Adjustment | undefined> {
         let amount = 0;
 
         for (const action of this.actions) {
@@ -109,12 +112,14 @@ export class Promotion extends AdjustmentSource implements ChannelAware, SoftDel
             if (this.isItemAction(promotionAction)) {
                 if (this.isOrderItemArg(args)) {
                     const { orderItem, orderLine } = args;
-                    amount += Math.round(await promotionAction.execute(orderItem, orderLine, action.args));
+                    amount += Math.round(
+                        await promotionAction.execute(ctx, orderItem, orderLine, action.args),
+                    );
                 }
             } else {
                 if (!this.isOrderItemArg(args)) {
                     const { order } = args;
-                    amount += Math.round(await promotionAction.execute(order, action.args));
+                    amount += Math.round(await promotionAction.execute(ctx, order, action.args));
                 }
             }
         }

+ 4 - 4
packages/core/src/service/helpers/order-calculator/order-calculator.spec.ts

@@ -155,7 +155,7 @@ describe('OrderCalculator', () => {
             code: 'fixed_price_item_action',
             description: [{ languageCode: LanguageCode.en, value: '' }],
             args: {},
-            execute(item) {
+            execute(ctx, item) {
                 return -item.unitPrice + 42;
             },
         });
@@ -164,7 +164,7 @@ describe('OrderCalculator', () => {
             code: 'fixed_price_order_action',
             description: [{ languageCode: LanguageCode.en, value: '' }],
             args: {},
-            execute(order) {
+            execute(ctx, order) {
                 return -order.total + 42;
             },
         });
@@ -173,7 +173,7 @@ describe('OrderCalculator', () => {
             code: 'percentage_item_action',
             description: [{ languageCode: LanguageCode.en, value: '' }],
             args: { discount: { type: 'int' } },
-            async execute(orderItem, orderLine, args) {
+            async execute(ctx, orderItem, orderLine, args) {
                 return -orderLine.unitPrice * (args.discount / 100);
             },
         });
@@ -182,7 +182,7 @@ describe('OrderCalculator', () => {
             code: 'percentage_order_action',
             description: [{ languageCode: LanguageCode.en, value: '' }],
             args: { discount: { type: 'int' } },
-            execute(order, args) {
+            execute(ctx, order, args) {
                 return -order.subTotal * (args.discount / 100);
             },
         });

+ 2 - 2
packages/core/src/service/helpers/order-calculator/order-calculator.ts

@@ -199,7 +199,7 @@ export class OrderCalculator {
                 // as to render later promotions no longer applicable.
                 if (await promotion.test(ctx, order)) {
                     for (const item of line.items) {
-                        const adjustment = await promotion.apply({
+                        const adjustment = await promotion.apply(ctx, {
                             orderItem: item,
                             orderLine: line,
                         });
@@ -262,7 +262,7 @@ export class OrderCalculator {
                 // re-test the promotion on each iteration, since the order total
                 // may be modified by a previously-applied promotion
                 if (await promotion.test(ctx, order)) {
-                    const adjustment = await promotion.apply({ order });
+                    const adjustment = await promotion.apply(ctx, { order });
                     if (adjustment) {
                         order.pendingAdjustments = order.pendingAdjustments.concat(adjustment);
                     }