update
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 35s
Build and Publish / BuildAndDeployArm64 (push) Successful in 3m45s

This commit is contained in:
matst80
2025-11-27 12:45:34 +01:00
parent c2a137d8d4
commit aea168160e
14 changed files with 335 additions and 335 deletions

View File

@@ -1,7 +1,9 @@
package cart
import (
"context"
"fmt"
"log"
messages "git.k6n.net/go-cart-actor/pkg/messages"
)
@@ -21,6 +23,7 @@ import (
// must keep this handler in sync.
func (c *CartMutationContext) AddItem(g *CartGrain, m *messages.AddItem) error {
ctx := context.Background()
if m == nil {
return fmt.Errorf("AddItem: nil payload")
}
@@ -38,18 +41,15 @@ func (c *CartMutationContext) AddItem(g *CartGrain, m *messages.AddItem) error {
if !sameStore {
continue
}
if m.ReservationEndTime != nil {
t := m.ReservationEndTime.AsTime()
if existing.ReservationEndTime == nil || existing.ReservationEndTime.Before(m.ReservationEndTime.AsTime()) {
existing.ReservationEndTime = &t
existing.Quantity += int(m.Quantity)
} else {
existing.ReservationEndTime = &t
}
} else {
existing.Quantity += int(m.Quantity)
if err := c.ReleaseItem(ctx, g.Id, existing.Sku, existing.StoreId); err != nil {
log.Printf("failed to release item %d: %v", existing.Id, err)
}
endTime, err := c.ReserveItem(ctx, g.Id, existing.Sku, existing.StoreId, existing.Quantity+uint16(m.Quantity))
if err != nil {
return err
}
existing.ReservationEndTime = endTime
existing.Quantity += uint16(m.Quantity)
existing.Stock = uint16(m.Stock)
// If existing had nil store but new has one, adopt it.
if existing.StoreId == nil && m.StoreId != nil {
@@ -69,10 +69,15 @@ func (c *CartMutationContext) AddItem(g *CartGrain, m *messages.AddItem) error {
}
pricePerItem := NewPriceFromIncVat(m.Price, taxRate)
endTime, err := c.ReserveItem(ctx, g.Id, m.Sku, m.StoreId, uint16(m.Quantity))
if err != nil {
return err
}
cartItem := &CartItem{
Id: g.lastItemId,
ItemId: uint32(m.ItemId),
Quantity: int(m.Quantity),
Quantity: uint16(m.Quantity),
Sku: m.Sku,
Tax: int(taxRate * 100),
Meta: &ItemMeta{
@@ -101,12 +106,10 @@ func (c *CartMutationContext) AddItem(g *CartGrain, m *messages.AddItem) error {
OrgPrice: getOrgPrice(m.OrgPrice, taxRate),
ArticleType: m.ArticleType,
StoreId: m.StoreId,
}
if m.ReservationEndTime != nil {
t := m.ReservationEndTime.AsTime()
cartItem.ReservationEndTime = &t
StoreId: m.StoreId,
ReservationEndTime: endTime,
}
g.Items = append(g.Items, cartItem)
g.UpdateTotals()
return nil