update subscriptions
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"git.k6n.net/go-cart-actor/pkg/actor"
|
||||
messages "git.k6n.net/go-cart-actor/pkg/messages"
|
||||
"git.k6n.net/go-cart-actor/pkg/voucher"
|
||||
"github.com/matst80/go-redis-inventory/pkg/inventory"
|
||||
)
|
||||
|
||||
// Legacy padded [16]byte CartId and its helper methods removed.
|
||||
@@ -235,26 +236,23 @@ func (c *CartGrain) Notify(event actor.Event) {
|
||||
defer c.mu.Unlock()
|
||||
// Example: if event is inventory change for a SKU in the cart
|
||||
if strings.HasPrefix(event.Topic, "inventory:") {
|
||||
skuAndLocationId := strings.TrimPrefix(event.Topic, "inventory:")
|
||||
parts := strings.Split(skuAndLocationId, ":")
|
||||
if len(parts) != 2 {
|
||||
sku := strings.TrimPrefix(event.Topic, "inventory:")
|
||||
|
||||
log.Printf("cart grain got inventory update: %s", event.Topic)
|
||||
update, ok := event.Payload.(inventory.InventoryChange)
|
||||
if !ok {
|
||||
log.Printf("cart grain inventory update has invalid payload")
|
||||
return
|
||||
}
|
||||
sku := parts[0]
|
||||
locationId := parts[1]
|
||||
log.Printf("cart grain got inventory update: %s", event.Topic)
|
||||
for _, item := range c.Items {
|
||||
if item.Sku == sku && item.StoreId != nil && *item.StoreId == locationId {
|
||||
|
||||
if item.Sku == sku && update.StockLocationID == *item.StoreId {
|
||||
// Update stock status based on payload, e.g., if payload is bool available
|
||||
|
||||
if quantity, ok := event.Payload.(int64); ok {
|
||||
log.Printf("cart grain got item stock update %d", quantity)
|
||||
if quantity > 0 {
|
||||
item.Stock = 1 // assuming 1 is in stock
|
||||
} else {
|
||||
item.Stock = 0 // out of stock
|
||||
}
|
||||
}
|
||||
log.Printf("cart grain got item stock update %d", update.Value)
|
||||
|
||||
item.Stock = uint16(update.Value)
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user