import { ConfigArg } from '@subit/common/lib/generated-types'; import { RequestContext } from '../../api/common/request-context'; import { ConfigArgs, ConfigArgValues, ConfigurableOperationDef, ConfigurableOperationDefOptions } from '../../common/configurable-operation'; import { PromotionState } from '../../entity'; import { OrderItem } from '../../entity/order-item/order-item.entity'; import { OrderLine } from '../../entity/order-line/order-line.entity'; import { Order } from '../../entity/order/order.entity'; import { ShippingLine } from '../../entity/shipping-line/shipping-line.entity'; import { PromotionCondition } from './promotion-condition'; /** * Unwrap a promise type */ declare type Awaited = T extends PromiseLike ? Awaited : T; /** * Extract the (non-false) return value of the PromotionCondition "check" function. */ declare type ConditionCheckReturnType> = Exclude>, false>; /** * Converts an array of PromotionCondition types into a tuple, thus preserving the * distinct type of each condition in the array. */ export declare type ConditionTuple>> = [...C]; /** * Converts an array of PromotionConditions into a tuple of the type: * [, ] */ declare type CodesStateTuple>>> = { [K in keyof T]: T[K] extends PromotionCondition ? [T[K]['code'], ConditionCheckReturnType] : never; }; /** * Convert a tuple into a union * [[string, number], [number, boolean]] => [string, number] | [number, boolean] */ declare type TupleToUnion = T[number]; /** * Converts an array of PromotionConditions into an object of the type: * { * [PromotionCondition.code]: ReturnType * } */ export declare type ConditionState>, T extends [string, any] = TupleToUnion>>> = { [key in T[0]]: Extract[1]; }; /** * @description * The function which is used by a PromotionItemAction to calculate the * discount on the OrderItem. * * @docsCategory promotions * @docsPage promotion-action */ export declare type ExecutePromotionItemActionFn>> = (ctx: RequestContext, orderItem: OrderItem, orderLine: OrderLine, args: ConfigArgValues, state: ConditionState) => number | Promise; /** * @description * The function which is used by a PromotionOrderAction to calculate the * discount on the Order. * * @docsCategory promotions * @docsPage promotion-action */ export declare type ExecutePromotionOrderActionFn>> = (ctx: RequestContext, order: Order, args: ConfigArgValues, state: ConditionState) => number | Promise; /** * @description * The function which is used by a PromotionOrderAction to calculate the * discount on the Order. * * @docsCategory promotions * @docsPage promotion-action */ export declare type ExecutePromotionShippingActionFn>> = (ctx: RequestContext, shippingLine: ShippingLine, order: Order, args: ConfigArgValues, state: ConditionState) => number | Promise; /** * @description * Configuration for all types of {@link PromotionAction}. * * @docsCategory promotions * @docsPage promotion-action */ export interface PromotionActionConfig> | undefined> extends ConfigurableOperationDefOptions { /** * @description * Used to determine the order of application of multiple Promotions * on the same Order. See the {@link Promotion} `priorityScore` field for * more information. * * @default 0 */ priorityValue?: number; /** * @description * Allows PromotionActions to define one or more PromotionConditions as dependencies. Having a PromotionCondition * as a dependency has the following consequences: * 1. A Promotion using this PromotionAction is only valid if it also contains all PromotionConditions * on which it depends. * 2. The `execute()` function will receive a statically-typed `state` argument which will contain * the return values of the PromotionConditions' `check()` function. */ conditions?: U extends undefined ? undefined : ConditionTuple>; } /** * @description * Configuration for a {@link PromotionItemAction} * * @docsCategory promotions * @docsPage promotion-action */ export interface PromotionItemActionConfig extends PromotionActionConfig { /** * @description * The function which contains the promotion calculation logic. */ execute: ExecutePromotionItemActionFn; } /** * @description * * @docsCategory promotions * @docsPage promotion-action */ export interface PromotionOrderActionConfig extends PromotionActionConfig { /** * @description * The function which contains the promotion calculation logic. */ execute: ExecutePromotionOrderActionFn; } /** * @description * * @docsCategory promotions * @docsPage promotion-action */ export interface PromotionShippingActionConfig extends PromotionActionConfig { /** * @description * The function which contains the promotion calculation logic. */ execute: ExecutePromotionShippingActionFn; } /** * @description * An abstract class which is extended by {@link PromotionItemAction}, {@link PromotionOrderAction}, * and {@link PromotionShippingAction}. * * @docsCategory promotions * @docsPage promotion-action * @docsWeight 0 */ export declare abstract class PromotionAction extends ConfigurableOperationDef { /** * @description * Used to determine the order of application of multiple Promotions * on the same Order. See the {@link Promotion} `priorityScore` field for * more information. * * @default 0 */ readonly priorityValue: number; readonly conditions?: U; protected constructor(config: PromotionActionConfig); } /** * @description * Represents a PromotionAction which applies to individual {@link OrderItem}s. * * @example * ```ts * // Applies a percentage discount to each OrderItem * const itemPercentageDiscount = new PromotionItemAction({ * code: 'item_percentage_discount', * args: { discount: 'percentage' }, * execute(ctx, orderItem, orderLine, args) { * return -orderLine.unitPrice * (args.discount / 100); * }, * description: 'Discount every item by { discount }%', * }); * ``` * * @docsCategory promotions * @docsPage promotion-action * @docsWeight 1 */ export declare class PromotionItemAction> = []> extends PromotionAction { private readonly executeFn; constructor(config: PromotionItemActionConfig); /** @internal */ execute(ctx: RequestContext, orderItem: OrderItem, orderLine: OrderLine, args: ConfigArg[], state: PromotionState): number | Promise; } /** * @description * Represents a PromotionAction which applies to the {@link Order} as a whole. * * @example * ```ts * // Applies a percentage discount to the entire Order * const orderPercentageDiscount = new PromotionOrderAction({ * code: 'order_percentage_discount', * args: { discount: 'percentage' }, * execute(ctx, order, args) { * return -order.subTotal * (args.discount / 100); * }, * description: 'Discount order by { discount }%', * }); * ``` * * @docsCategory promotions * @docsPage promotion-action * @docsWeight 2 */ export declare class PromotionOrderAction extends PromotionAction { private readonly executeFn; constructor(config: PromotionOrderActionConfig); /** @internal */ execute(ctx: RequestContext, order: Order, args: ConfigArg[], state: PromotionState): number | Promise; } /** * @description * Represents a PromotionAction which applies to the shipping cost of an Order. * * @docsCategory promotions * @docsPage promotion-action * @docsWeight 3 */ export declare class PromotionShippingAction extends PromotionAction { private readonly executeFn; constructor(config: PromotionShippingActionConfig); /** @internal */ execute(ctx: RequestContext, shippingLine: ShippingLine, order: Order, args: ConfigArg[], state: PromotionState): number | Promise; } export {};