promotions
This commit is contained in:
+10
-2
@@ -149,8 +149,11 @@ func main() {
|
||||
// any matched actions (e.g. the Volymrabatt volume discount), which adjust
|
||||
// TotalPrice/TotalDiscount on top of line items and vouchers.
|
||||
promotionCtx := promotions.NewContextFromCart(g, promotions.WithNow(time.Now()))
|
||||
_, actions := promotionService.EvaluateAll(promotionStore.Snapshot(), promotionCtx)
|
||||
promotions.ApplyActions(g, actions)
|
||||
results, _ := promotionService.EvaluateAll(promotionStore.Snapshot(), promotionCtx)
|
||||
// ApplyResults applies qualifying actions in priority order and records
|
||||
// every effect — both applied discounts and pending "spend X more for ..."
|
||||
// nudges with their progress — in g.AppliedPromotions.
|
||||
promotionService.ApplyResults(g, results, promotionCtx)
|
||||
g.Version++
|
||||
return nil
|
||||
}),
|
||||
@@ -275,6 +278,11 @@ func main() {
|
||||
mux.Handle("/mcp", promotionMCP.Handler())
|
||||
mux.Handle("/mcp/", promotionMCP.Handler())
|
||||
|
||||
// Stateless promotion evaluation: POST a (possibly partial) eval context and
|
||||
// get back the resulting totals plus the applied/pending effect breakdown,
|
||||
// without creating or mutating a real cart.
|
||||
mux.HandleFunc("POST /promotions/evaluate", newPromotionEvaluateHandler(promotionStore, promotionService))
|
||||
|
||||
// only for local
|
||||
mux.HandleFunc("GET /add/remote/{host}", func(w http.ResponseWriter, r *http.Request) {
|
||||
pool.AddRemote(r.PathValue("host"))
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/promotions"
|
||||
)
|
||||
|
||||
// newPromotionEvaluateHandler serves POST /promotions/evaluate: it takes a
|
||||
// (possibly partial) evaluation context and returns the totals plus the
|
||||
// applied/pending promotion effects, without creating or mutating a real cart.
|
||||
// An empty body is treated as an empty context (evaluates against no items).
|
||||
func newPromotionEvaluateHandler(store *promotions.Store, svc *promotions.PromotionService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req promotions.EvaluateRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil && !errors.Is(err, io.EOF) {
|
||||
http.Error(w, "invalid JSON body: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
resp := svc.Evaluate(store.Snapshot(), req)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user