Skip to content

Commit

Permalink
feat(core): Pass RequestContext to PromotionAction functions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The PromotionAction `execute()`
function signature has changed: the first argument is now the
RequestContext of the current request.
  • Loading branch information
michaelbromley committed Oct 27, 2020
1 parent eae71f0 commit 0a35a12
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }%' }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/config/promotion/promotion-action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ConfigArg } from '@vendure/common/lib/generated-types';

import { RequestContext } from '../../api/common/request-context';
import {
ConfigArgs,
ConfigArgValues,
Expand All @@ -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>,
Expand All @@ -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>;
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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));
}
}
11 changes: 8 additions & 3 deletions packages/core/src/entity/promotion/promotion.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,25 @@ 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) {
const promotionAction = this.allActions[action.code];
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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
});
Expand All @@ -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;
},
});
Expand All @@ -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);
},
});
Expand All @@ -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);
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 0a35a12

Please sign in to comment.