dont allow duplicate
All checks were successful
Build and Publish / Metadata (push) Successful in 9s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 1m16s
Build and Publish / BuildAndDeployArm64 (push) Successful in 3m52s

This commit is contained in:
matst80
2025-10-14 14:52:42 +02:00
parent b842efae6a
commit cbbae3dfd3

View File

@@ -2,8 +2,10 @@ package main
import (
"fmt"
"slices"
"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 {
@@ -11,16 +13,22 @@ func (ctx *MutationContext) AddVoucher(g *CartGrain, m *messages.AddVoucher) err
return fmt.Errorf("AddVoucher: nil payload")
}
voucher, err := ctx.VoucherService.GetVoucher(m.Code)
if err != nil {
return fmt.Errorf("AddVoucher: %w", err)
if slices.ContainsFunc(g.Vouchers, func(v *voucher.Voucher) bool {
return v.Code == m.Code
}) {
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
}
g.Vouchers = append(g.Vouchers, voucher)
g.Vouchers = append(g.Vouchers, voucherData)
g.UpdateTotals()
return nil
}