add state file parser
All checks were successful
Build and Publish / Metadata (push) Successful in 11s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 1m12s
Build and Publish / BuildAndDeployArm64 (push) Successful in 4m25s

This commit is contained in:
matst80
2025-10-18 15:25:44 +02:00
parent b7f0990269
commit e127251a60
2 changed files with 35 additions and 1 deletions

View File

@@ -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}
}

View File

@@ -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
}