Skip to content

Commit

Permalink
feat(core): Add maxDiscountAmount to PromotionItemAction
Browse files Browse the repository at this point in the history
  • Loading branch information
Feelw00 committed Jul 19, 2024
1 parent d40b199 commit f6bdc5f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
41 changes: 41 additions & 0 deletions packages/core/src/config/promotion/promotion-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ export interface PromotionItemActionConfig<T extends ConfigArgs, U extends Promo
* the OrderLine, i.e. the number should be negative.
*/
execute: ExecutePromotionItemActionFn<T, U>;

/**
* @description
* The maximum discount amount that can be applied to this action.
*
* @default 0
*/
maxDiscountAmount?: ExecutePromotionItemActionFn<T, U>;
}

/**
Expand Down Expand Up @@ -321,10 +329,15 @@ export class PromotionItemAction<
T extends ConfigArgs = ConfigArgs,
U extends Array<PromotionCondition<any>> = [],
> extends PromotionAction<T, U> {
/** @internal */
private readonly executeFn: ExecutePromotionItemActionFn<T, U>;
/** @internal */
private readonly maxDiscountAmountFn: ExecutePromotionItemActionFn<T, U>;

constructor(config: PromotionItemActionConfig<T, U>) {
super(config);
this.executeFn = config.execute;
this.maxDiscountAmountFn = config.maxDiscountAmount || (() => 0);
}

/** @internal */
Expand All @@ -349,6 +362,34 @@ export class PromotionItemAction<
promotion,
);
}

/**
* @description
* The maximum discount amount that can be applied to this action.
*
* @default 0
*/
maxDiscountAmount(
ctx: RequestContext,
orderLine: OrderLine,
args: ConfigArg[],
state: PromotionState,
promotion: Promotion,
) {
const actionState = this.conditions
? pick(
state,
this.conditions.map(c => c.code),
)
: {};
return this.maxDiscountAmountFn(
ctx,
orderLine,
this.argsArrayToHash(args),
actionState as ConditionState<U>,
promotion,
);
}
}

/**
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/entity/promotion/promotion.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,17 @@ export class Promotion
if (promotionAction instanceof PromotionItemAction) {
if (this.isOrderItemArg(args)) {
const { orderLine } = args;
amount += roundMoney(
const discountAmount = roundMoney(
await promotionAction.execute(ctx, orderLine, action.args, state, this),
);
) * orderLine.quantity;
const maxDiscountAmount =
roundMoney(
await promotionAction.maxDiscountAmount(ctx, orderLine, action.args, state, this),
);
amount +=
maxDiscountAmount && maxDiscountAmount > discountAmount
? maxDiscountAmount
: discountAmount;
}
} else if (promotionAction instanceof PromotionOrderAction) {
if (this.isOrderArg(args)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ export class OrderCalculator {
// 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;
}
Expand Down

0 comments on commit f6bdc5f

Please sign in to comment.