all the refactor
This commit is contained in:
@@ -7,6 +7,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"git.k6n.net/mats/platform/money"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -74,7 +76,7 @@ const (
|
||||
type ruleCondition struct {
|
||||
Kind RuleKind
|
||||
StringVals []string // For sku / category multi-value list
|
||||
MinValue *int64 // For numeric threshold rules
|
||||
MinValue *money.Cents // For numeric threshold rules (minor units)
|
||||
// Operator reserved for future (e.g., >, >=, ==). Currently always ">=" for numeric kinds.
|
||||
Operator string
|
||||
}
|
||||
@@ -90,13 +92,13 @@ type RuleSet struct {
|
||||
type Item struct {
|
||||
Sku string
|
||||
Category string
|
||||
UnitPrice int64 // Inc VAT (single unit)
|
||||
UnitPrice money.Cents // Inc VAT (single unit)
|
||||
}
|
||||
|
||||
// EvalContext bundles cart-like data necessary for evaluation.
|
||||
type EvalContext struct {
|
||||
Items []Item
|
||||
CartTotalInc int64
|
||||
CartTotalInc money.Cents
|
||||
}
|
||||
|
||||
// Applies returns true if all rule conditions pass for the context.
|
||||
@@ -268,9 +270,10 @@ func parseNumericRule(frag string) (ruleCondition, error) {
|
||||
return ruleCondition{}, fmt.Errorf("negative threshold %d", value)
|
||||
}
|
||||
|
||||
mv := money.Cents(value)
|
||||
return ruleCondition{
|
||||
Kind: kind,
|
||||
MinValue: &value,
|
||||
MinValue: &mv,
|
||||
Operator: ">=",
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ func TestParseRules_Invalid(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRuleSet_Applies(t *testing.T) {
|
||||
rs := MustParseRules("sku=ABC123|XYZ999; category=Shoes|min_total>=10000; min_item_price>=3000")
|
||||
rs := MustParseRules("sku=ABC123|XYZ999; category=Shoes; min_total>=10000; min_item_price>=3000")
|
||||
|
||||
ctx := EvalContext{
|
||||
Items: []Item{
|
||||
|
||||
@@ -7,18 +7,19 @@ import (
|
||||
"os"
|
||||
|
||||
messages "git.k6n.net/mats/go-cart-actor/proto/cart"
|
||||
"git.k6n.net/mats/platform/money"
|
||||
)
|
||||
|
||||
type Rule struct {
|
||||
Type string `json:"type"`
|
||||
Value int64 `json:"value"`
|
||||
Type string `json:"type"`
|
||||
Value money.Cents `json:"value"`
|
||||
}
|
||||
|
||||
type Voucher struct {
|
||||
Code string `json:"code"`
|
||||
Value int64 `json:"value"`
|
||||
Rules string `json:"rules"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Code string `json:"code"`
|
||||
Value money.Cents `json:"value"`
|
||||
Rules string `json:"rules"`
|
||||
Description string `json:"description,omitempty"`
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
@@ -42,7 +43,7 @@ func (s *Service) GetVoucher(code string) (*messages.AddVoucher, error) {
|
||||
|
||||
return &messages.AddVoucher{
|
||||
Code: code,
|
||||
Value: v.Value,
|
||||
Value: v.Value.Int64(), // proto boundary: AddVoucher.Value is int64
|
||||
Description: v.Description,
|
||||
VoucherRules: []string{
|
||||
v.Rules,
|
||||
|
||||
Reference in New Issue
Block a user