feature/pubsub #7
@@ -38,16 +38,24 @@ func AddItem(g *CartGrain, m *messages.AddItem) error {
|
|||||||
if !sameStore {
|
if !sameStore {
|
||||||
continue
|
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)
|
existing.Quantity += int(m.Quantity)
|
||||||
|
}
|
||||||
existing.Stock = uint16(m.Stock)
|
existing.Stock = uint16(m.Stock)
|
||||||
// If existing had nil store but new has one, adopt it.
|
// If existing had nil store but new has one, adopt it.
|
||||||
if existing.StoreId == nil && m.StoreId != nil {
|
if existing.StoreId == nil && m.StoreId != nil {
|
||||||
existing.StoreId = m.StoreId
|
existing.StoreId = m.StoreId
|
||||||
}
|
}
|
||||||
if m.ReservationEndTime != nil {
|
|
||||||
t := m.ReservationEndTime.AsTime()
|
|
||||||
existing.ReservationEndTime = &t
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,8 +48,15 @@ func ChangeQuantity(g *CartGrain, m *messages.ChangeQuantity) error {
|
|||||||
g.UpdateTotals()
|
g.UpdateTotals()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
item := g.Items[foundIndex]
|
||||||
g.Items[foundIndex].Quantity = int(m.Quantity)
|
if item == nil {
|
||||||
|
return fmt.Errorf("ChangeQuantity: item id %d not found", m.Id)
|
||||||
|
}
|
||||||
|
if item.ReservationEndTime != nil {
|
||||||
|
return fmt.Errorf("ChangeQuantity: cannot change quantity of reserved item id %d", m.Id)
|
||||||
|
} else {
|
||||||
|
item.Quantity = int(m.Quantity)
|
||||||
g.UpdateTotals()
|
g.UpdateTotals()
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package cart
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
messages "git.k6n.net/go-cart-actor/pkg/messages"
|
messages "git.k6n.net/go-cart-actor/pkg/messages"
|
||||||
)
|
)
|
||||||
@@ -36,6 +37,14 @@ func InitializeCheckout(g *CartGrain, m *messages.InitializeCheckout) error {
|
|||||||
if m.OrderId == "" {
|
if m.OrderId == "" {
|
||||||
return fmt.Errorf("InitializeCheckout: missing orderId")
|
return fmt.Errorf("InitializeCheckout: missing orderId")
|
||||||
}
|
}
|
||||||
|
now := time.Now()
|
||||||
|
for _, item := range g.Items {
|
||||||
|
if item.ReservationEndTime != nil {
|
||||||
|
if now.After(*item.ReservationEndTime) {
|
||||||
|
return fmt.Errorf("InitializeCheckout: item id %d reservation has expired", item.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
g.OrderReference = m.OrderId
|
g.OrderReference = m.OrderId
|
||||||
g.PaymentStatus = m.Status
|
g.PaymentStatus = m.Status
|
||||||
|
|||||||
Reference in New Issue
Block a user