promotions
This commit is contained in:
+21
-2
@@ -100,14 +100,33 @@ func NewContextFromCart(g *cart.CartGrain, opts ...ContextOption) *PromotionEval
|
||||
// PromotionService evaluates PromotionRules against a PromotionEvalContext.
|
||||
type PromotionService struct {
|
||||
tracer Tracer
|
||||
// effects is the per-action-type behaviour registry. Each Effect both applies
|
||||
// its action to the cart (when the owning rule qualifies) and reports its own
|
||||
// progress, so any effect type can surface "spend X more for ..." nudges —
|
||||
// even one that has already applied (e.g. a tiered discount at 5% pointing at
|
||||
// the next 9% tier). Register a new Effect to add a new action type.
|
||||
effects map[ActionType]Effect
|
||||
}
|
||||
|
||||
// NewPromotionService constructs a PromotionService with an optional tracer.
|
||||
// NewPromotionService constructs a PromotionService with an optional tracer and
|
||||
// the built-in effect handlers.
|
||||
func NewPromotionService(t Tracer) *PromotionService {
|
||||
if t == nil {
|
||||
t = NoopTracer{}
|
||||
}
|
||||
return &PromotionService{tracer: t}
|
||||
return &PromotionService{
|
||||
tracer: t,
|
||||
effects: defaultEffects(),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterEffect adds (or overrides) the handler for an action type, so new
|
||||
// applied/pending effects can be plugged in without touching the engine.
|
||||
func (s *PromotionService) RegisterEffect(e Effect) {
|
||||
if s.effects == nil {
|
||||
s.effects = map[ActionType]Effect{}
|
||||
}
|
||||
s.effects[e.Type()] = e
|
||||
}
|
||||
|
||||
// EvaluationResult holds the outcome of evaluating a single rule.
|
||||
|
||||
Reference in New Issue
Block a user