From e127251a60c8b6d52808b0ab7aa116a3479af3ae Mon Sep 17 00:00:00 2001 From: matst80 Date: Sat, 18 Oct 2025 15:25:44 +0200 Subject: [PATCH] add state file parser --- pkg/cart/mutation_test.go | 2 +- pkg/promotions/types.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pkg/cart/mutation_test.go b/pkg/cart/mutation_test.go index 68c37a2..50fff32 100644 --- a/pkg/cart/mutation_test.go +++ b/pkg/cart/mutation_test.go @@ -66,7 +66,7 @@ func msgClearCart() *messages.ClearCartRequest { return &messages.ClearCartRequest{} } -func msgAddVoucher(code string, value int64, rules ...*messages.VoucherRule) *messages.AddVoucher { +func msgAddVoucher(code string, value int64, rules ...string) *messages.AddVoucher { return &messages.AddVoucher{Code: code, Value: value, VoucherRules: rules} } diff --git a/pkg/promotions/types.go b/pkg/promotions/types.go index 3646c60..bf5b370 100644 --- a/pkg/promotions/types.go +++ b/pkg/promotions/types.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "os" "strconv" ) @@ -369,3 +370,36 @@ func WalkConditions(conds []Condition, fn func(c Condition) bool) { } } } + +type PromotionState struct { + Vouchers []PromotionRule `json:"promotions"` +} + +type StateFile struct { + State PromotionState `json:"state"` + Version int `json:"version"` +} + +func (sf *StateFile) GetPromotion(id string) (*PromotionRule, bool) { + for _, v := range sf.State.Vouchers { + if v.ID == id { + return &v, true + } + } + return nil, false +} + +func LoadStateFile(fileName string) (*PromotionState, error) { + f, err := os.Open(fileName) + if err != nil { + return nil, err + } + defer f.Close() + dec := json.NewDecoder(f) + sf := &PromotionState{} + err = dec.Decode(sf) + if err != nil { + return nil, err + } + return sf, nil +}