add giftcards

This commit is contained in:
matst80
2025-11-21 10:14:33 +01:00
parent 896bf7672b
commit 886e3ce48f
7 changed files with 374 additions and 16 deletions

View File

@@ -0,0 +1,41 @@
package cart
import (
"encoding/json"
"fmt"
messages "git.tornberg.me/go-cart-actor/pkg/messages"
"google.golang.org/protobuf/proto"
)
func AddGiftcard(grain *CartGrain, req *messages.AddGiftcard) error {
if req.Giftcard == nil {
return fmt.Errorf("giftcard cannot be nil")
}
if req.Giftcard.Value <= 0 {
return fmt.Errorf("giftcard value must be positive")
}
grain.lastGiftcardId++
designConfig := json.RawMessage{}
if req.Giftcard.DesignConfig != nil {
// Convert Any to RawMessage
data, err := proto.Marshal(req.Giftcard.DesignConfig)
if err != nil {
return fmt.Errorf("failed to marshal designConfig: %w", err)
}
designConfig = data
}
value := NewPriceFromIncVat(req.Giftcard.Value, 25) // Assuming 25% tax; adjust as needed
item := &GiftcardItem{
Id: grain.lastGiftcardId,
Value: *value,
DeliveryDate: req.Giftcard.DeliveryDate,
Recipient: req.Giftcard.Recipient,
RecipientType: req.Giftcard.RecipientType,
Message: req.Giftcard.Message,
DesignConfig: designConfig,
}
grain.Giftcards = append(grain.Giftcards, item)
grain.UpdateTotals()
return nil
}