Merge branch 'main' of git-ssh.tornberg.me:mats/go-cart-actor
This commit is contained in:
@@ -92,10 +92,12 @@ type CartGrain struct {
|
||||
}
|
||||
|
||||
type Voucher struct {
|
||||
Code string `json:"code"`
|
||||
Rules []*messages.VoucherRule `json:"rules"`
|
||||
Id uint32 `json:"id"`
|
||||
Value int64 `json:"value"`
|
||||
Code string `json:"code"`
|
||||
Applied bool `json:"applied"`
|
||||
Rules []string `json:"rules"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Id uint32 `json:"id"`
|
||||
Value int64 `json:"value"`
|
||||
}
|
||||
|
||||
func (v *Voucher) AppliesTo(cart *CartGrain) ([]*CartItem, bool) {
|
||||
@@ -127,8 +129,8 @@ func (v *Voucher) AppliesTo(cart *CartGrain) ([]*CartItem, bool) {
|
||||
}
|
||||
|
||||
// All voucher rules must pass (logical AND)
|
||||
for _, rule := range v.Rules {
|
||||
expr := rule.GetCondition()
|
||||
for _, expr := range v.Rules {
|
||||
|
||||
if expr == "" {
|
||||
// Empty condition treated as pass (acts like a comment / placeholder)
|
||||
continue
|
||||
@@ -249,31 +251,36 @@ func (c *CartGrain) UpdateTotals() {
|
||||
for _, item := range c.Items {
|
||||
rowTotal := MultiplyPrice(item.Price, int64(item.Quantity))
|
||||
|
||||
item.TotalPrice = *rowTotal
|
||||
|
||||
c.TotalPrice.Add(*rowTotal)
|
||||
|
||||
if item.OrgPrice != nil {
|
||||
diff := NewPrice()
|
||||
diff.Add(*item.OrgPrice)
|
||||
diff.Subtract(item.Price)
|
||||
diff.Multiply(int64(item.Quantity))
|
||||
rowTotal.Subtract(*diff)
|
||||
item.Discount = diff
|
||||
if diff.IncVat > 0 {
|
||||
c.TotalDiscount.Add(*diff)
|
||||
}
|
||||
}
|
||||
|
||||
item.TotalPrice = *rowTotal
|
||||
|
||||
c.TotalPrice.Add(*rowTotal)
|
||||
|
||||
}
|
||||
for _, delivery := range c.Deliveries {
|
||||
c.TotalPrice.Add(delivery.Price)
|
||||
}
|
||||
for _, voucher := range c.Vouchers {
|
||||
if _, ok := voucher.AppliesTo(c); ok {
|
||||
_, ok := voucher.AppliesTo(c)
|
||||
voucher.Applied = false
|
||||
if ok {
|
||||
value := NewPriceFromIncVat(voucher.Value, 25)
|
||||
if c.TotalPrice.IncVat <= value.IncVat {
|
||||
// don't apply discounts to more than the total price
|
||||
continue
|
||||
}
|
||||
|
||||
voucher.Applied = true
|
||||
c.TotalDiscount.Add(*value)
|
||||
c.TotalPrice.Subtract(*value)
|
||||
}
|
||||
|
||||
@@ -45,6 +45,9 @@ func NewCartMultationRegistry() actor.MutationRegistry {
|
||||
actor.NewMutation(UpsertSubscriptionDetails, func() *messages.UpsertSubscriptionDetails {
|
||||
return &messages.UpsertSubscriptionDetails{}
|
||||
}),
|
||||
actor.NewMutation(PreConditionFailed, func() *messages.PreConditionFailed {
|
||||
return &messages.PreConditionFailed{}
|
||||
}),
|
||||
)
|
||||
return reg
|
||||
|
||||
|
||||
@@ -54,10 +54,12 @@ func AddVoucher(g *CartGrain, m *messages.AddVoucher) error {
|
||||
|
||||
g.lastVoucherId++
|
||||
g.Vouchers = append(g.Vouchers, &Voucher{
|
||||
Id: g.lastVoucherId,
|
||||
Code: m.Code,
|
||||
Rules: m.VoucherRules,
|
||||
Value: m.Value,
|
||||
Id: g.lastVoucherId,
|
||||
Applied: false,
|
||||
Description: m.Description,
|
||||
Code: m.Code,
|
||||
Rules: m.VoucherRules,
|
||||
Value: m.Value,
|
||||
})
|
||||
g.UpdateTotals()
|
||||
return nil
|
||||
|
||||
7
pkg/cart/mutation_precondition.go
Normal file
7
pkg/cart/mutation_precondition.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package cart
|
||||
|
||||
import messages "git.tornberg.me/go-cart-actor/pkg/messages"
|
||||
|
||||
func PreConditionFailed(g *CartGrain, m *messages.PreConditionFailed) error {
|
||||
return nil
|
||||
}
|
||||
@@ -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}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user