Skip to content

Commit

Permalink
fix(core): Remove applyOrderLinePromotions Method
Browse files Browse the repository at this point in the history
  • Loading branch information
Feelw00 committed Jul 25, 2024
1 parent 32dab29 commit 37d95ea
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 47 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/entity/promotion/promotion.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ export class Promotion
if (promotionAction instanceof PromotionItemAction) {
if (this.isOrderItemArg(args)) {
const { orderLine } = args;
amount += roundMoney(
await promotionAction.execute(ctx, orderLine, action.args, state, this),
);
amount +=
roundMoney(await promotionAction.execute(ctx, orderLine, action.args, state, this)) *
orderLine.quantity;
}
} else if (promotionAction instanceof PromotionLineAction) {
if (this.isOrderLineArg(args)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ export class OrderCalculator {
*/
private async applyPromotions(ctx: RequestContext, order: Order, promotions: Promotion[]): Promise<void> {
await this.applyOrderItemPromotions(ctx, order, promotions);
await this.applyOrderLinePromotions(ctx, order, promotions);
await this.applyOrderPromotions(ctx, order, promotions);
return;
}
Expand All @@ -177,6 +176,8 @@ export class OrderCalculator {
* Applies promotions to OrderItems. This is a quite complex function, due to the inherent complexity
* of applying the promotions, and also due to added complexity in the name of performance
* optimization. Therefore, it is heavily annotated so that the purpose of each step is clear.
* Additionally, this is used in both promotionItemAction and promotionLineAction,
* as it is difficult to separate action types at this stage.
*/
private async applyOrderItemPromotions(
ctx: RequestContext,
Expand All @@ -199,49 +200,6 @@ export class OrderCalculator {
const state = typeof applicableOrState === 'object' ? applicableOrState : undefined;
// for (const item of line.items) {
const adjustment = await promotion.apply(ctx, { orderLine: line }, state);
if (adjustment) {
adjustment.amount = adjustment.amount * line.quantity;
line.addAdjustment(adjustment);
priceAdjusted = true;
}
if (priceAdjusted) {
this.calculateOrderTotals(order);
priceAdjusted = false;
}
this.addPromotion(order, promotion);
}
}
this.calculateOrderTotals(order);
}
return;
}

/**
* @description
* Applies a promotion to the OrderLine.
* Unlike applyOrderItemPromotions, which applies promotions to OrderItems based on Quantity,
* this was created to apply promotions to the OrderLine regardless of Quantity.
*/
private async applyOrderLinePromotions(
ctx: RequestContext,
order: Order,
promotions: Promotion[],
): Promise<void> {
for (const line of order.lines) {
// Must be re-calculated for each line, since the previous lines may have triggered promotions
// which affected the order price.
const applicablePromotions = await filterAsync(promotions, p => p.test(ctx, order).then(Boolean));
line.clearAdjustments();

for (const promotion of applicablePromotions) {
let priceAdjusted = false;
// We need to test the promotion *again*, even though we've tested them for the line.
// This is because the previous Promotions may have adjusted the Order in such a way
// as to render later promotions no longer applicable.
const applicableOrState = await promotion.test(ctx, order);
if (applicableOrState) {
const state = typeof applicableOrState === 'object' ? applicableOrState : undefined;
const adjustment = await promotion.apply(ctx, { orderLine: line }, state);
if (adjustment) {
line.addAdjustment(adjustment);
priceAdjusted = true;
Expand Down

0 comments on commit 37d95ea

Please sign in to comment.