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

@@ -113,6 +113,23 @@ func msgCreateCheckoutOrder(terms, country string) *messages.CreateCheckoutOrder
return &messages.CreateCheckoutOrder{Terms: terms, Country: country}
}
func msgAddGiftcard(value int64, deliveryDate, recipient, recipientType, message string, designConfig *anypb.Any) *messages.AddGiftcard {
return &messages.AddGiftcard{
Giftcard: &messages.GiftcardItem{
Value: value,
DeliveryDate: deliveryDate,
Recipient: recipient,
RecipientType: recipientType,
Message: message,
DesignConfig: designConfig,
},
}
}
func msgRemoveGiftcard(id uint32) *messages.RemoveGiftcard {
return &messages.RemoveGiftcard{Id: id}
}
func ptr[T any](v T) *T { return &v }
// ----------------------
@@ -181,6 +198,8 @@ func TestMutationRegistryCoverage(t *testing.T) {
"PaymentDeclined",
"ConfirmationViewed",
"CreateCheckoutOrder",
"AddGiftcard",
"RemoveGiftcard",
}
names := reg.(*actor.ProtoMutationRegistry).RegisteredMutations()
@@ -675,7 +694,7 @@ func TestConfirmationViewed(t *testing.T) {
// Initial state
if g.Confirmation != nil {
t.Fatalf("confirmation should be nil, got %d", g.Confirmation)
t.Fatalf("confirmation should be nil, got %v", g.Confirmation)
}
// First view
@@ -717,3 +736,43 @@ func TestCreateCheckoutOrder(t *testing.T) {
// Terms not accepted
applyErrorContains(t, reg, g, msgCreateCheckoutOrder("no", ""), "terms must be accepted")
}
func TestAddGiftcard(t *testing.T) {
reg := newRegistry()
g := newTestGrain()
designConfig, _ := anypb.New(&messages.AddItem{}) // example
applyOK(t, reg, g, msgAddGiftcard(5000, "2023-12-25", "John", "email", "Happy Birthday!", designConfig))
if len(g.Giftcards) != 1 {
t.Fatalf("expected 1 giftcard, got %d", len(g.Giftcards))
}
gc := g.Giftcards[0]
if gc.Value.IncVat != 5000 || gc.DeliveryDate != "2023-12-25" || gc.Recipient != "John" || gc.RecipientType != "email" || gc.Message != "Happy Birthday!" {
t.Fatalf("giftcard not set correctly: %+v", gc)
}
if g.TotalPrice.IncVat != 5000 {
t.Fatalf("total price not updated, got %d", g.TotalPrice.IncVat)
}
// Test invalid value
applyErrorContains(t, reg, g, msgAddGiftcard(0, "", "", "", "", nil), "must be positive")
}
func TestRemoveGiftcard(t *testing.T) {
reg := newRegistry()
g := newTestGrain()
applyOK(t, reg, g, msgAddGiftcard(1000, "2023-01-01", "Jane", "sms", "Cheers!", nil))
id := g.Giftcards[0].Id
applyOK(t, reg, g, msgRemoveGiftcard(id))
if len(g.Giftcards) != 0 {
t.Fatalf("giftcard not removed")
}
if g.TotalPrice.IncVat != 0 {
t.Fatalf("total price not updated after removal, got %d", g.TotalPrice.IncVat)
}
applyErrorContains(t, reg, g, msgRemoveGiftcard(id), "not found")
}