Complete refactor to new grpc control plane and only http proxy for carts #4
@@ -2,8 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"git.tornberg.me/go-cart-actor/pkg/messages"
|
"git.tornberg.me/go-cart-actor/pkg/messages"
|
||||||
|
"git.tornberg.me/go-cart-actor/pkg/voucher"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (ctx *MutationContext) AddVoucher(g *CartGrain, m *messages.AddVoucher) error {
|
func (ctx *MutationContext) AddVoucher(g *CartGrain, m *messages.AddVoucher) error {
|
||||||
@@ -11,16 +13,22 @@ func (ctx *MutationContext) AddVoucher(g *CartGrain, m *messages.AddVoucher) err
|
|||||||
return fmt.Errorf("AddVoucher: nil payload")
|
return fmt.Errorf("AddVoucher: nil payload")
|
||||||
}
|
}
|
||||||
|
|
||||||
voucher, err := ctx.VoucherService.GetVoucher(m.Code)
|
if slices.ContainsFunc(g.Vouchers, func(v *voucher.Voucher) bool {
|
||||||
if err != nil {
|
return v.Code == m.Code
|
||||||
return fmt.Errorf("AddVoucher: %w", err)
|
}) {
|
||||||
|
return fmt.Errorf("voucher already applied")
|
||||||
}
|
}
|
||||||
|
|
||||||
if voucher == nil {
|
voucherData, err := ctx.VoucherService.GetVoucher(m.Code)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cant find voucher: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if voucherData == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
g.Vouchers = append(g.Vouchers, voucher)
|
g.Vouchers = append(g.Vouchers, voucherData)
|
||||||
g.UpdateTotals()
|
g.UpdateTotals()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user