Files
go-cart-actor/cmd/cart/mutation_add_voucher.go
matst80 9f83717ea9
All checks were successful
Build and Publish / Metadata (push) Successful in 9s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 1m27s
Build and Publish / BuildAndDeployArm64 (push) Successful in 4m0s
try new errors
2025-10-14 15:52:59 +02:00

48 lines
956 B
Go

package main
import (
"fmt"
"slices"
"git.tornberg.me/go-cart-actor/pkg/actor"
"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 {
if m == nil {
return &actor.MutationError{
Message: "AddVoucher: nil payload",
Code: 1001,
StatusCode: 400,
}
}
if slices.ContainsFunc(g.Vouchers, func(v *voucher.Voucher) bool {
return v.Code == m.Code
}) {
return &actor.MutationError{
Message: "voucher already applied",
Code: 1002,
StatusCode: 400,
}
}
voucherData, err := ctx.VoucherService.GetVoucher(m.Code)
if err != nil {
return &actor.MutationError{
Message: fmt.Sprintf("cant find voucher: %v", err),
Code: 1003,
StatusCode: 400,
}
}
if voucherData == nil {
return nil
}
g.Vouchers = append(g.Vouchers, voucherData)
g.UpdateTotals()
return nil
}