42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package cart
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
messages "git.k6n.net/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
|
|
}
|