feature/pubsub #7

Merged
mats merged 67 commits from feature/pubsub into main 2025-11-28 17:45:22 +01:00
2 changed files with 15 additions and 17 deletions
Showing only changes of commit 990708597b - Show all commits

View File

@@ -58,8 +58,8 @@ func NewPoolServer(pool actor.GrainPool[*cart.CartGrain], pod_name string, klarn
inventory.NewInventoryChangeListener(inventoryRedisClient, context.Background(), func(changes []inventory.InventoryChange) {
for _, change := range changes {
srv.GrainPool.GetPubSub().Publish(actor.Event{
Topic: fmt.Sprintf("inventory:%s:%s", change.SKU, change.StockLocationID),
Payload: change.Value,
Topic: fmt.Sprintf("inventory:%s", change.SKU),
Payload: change,
})
}
})

View File

@@ -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
}
}