add giftcards
This commit is contained in:
41
pkg/cart/mutation_add_giftcard.go
Normal file
41
pkg/cart/mutation_add_giftcard.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user