redo pubsub
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 34s
Build and Publish / BuildAndDeployArm64 (push) Successful in 3m49s

This commit is contained in:
matst80
2025-11-25 23:06:36 +01:00
parent f640cd7d2c
commit d5d2b3e711
6 changed files with 80 additions and 142 deletions

View File

@@ -2,13 +2,10 @@ package cart
import (
"encoding/json"
"log"
"slices"
"strings"
"sync"
"time"
"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"
@@ -230,43 +227,12 @@ func (c *CartGrain) GetCurrentState() (*CartGrain, error) {
return c, nil
}
// Notify handles incoming events, e.g., inventory changes.
func (c *CartGrain) Notify(event actor.Event) {
c.mu.Lock()
defer c.mu.Unlock()
// Example: if event is inventory change for a SKU in the cart
if strings.HasPrefix(event.Topic, "inventory:") {
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
}
for _, item := range c.Items {
if item.Sku == sku && update.StockLocationID == *item.StoreId {
// Update stock status based on payload, e.g., if payload is bool available
log.Printf("cart grain got item stock update %d", update.Value)
item.Stock = uint16(update.Value)
break
}
}
}
}
func (c *CartGrain) UpdateSubscriptions(pubsub *actor.PubSub) {
pubsub.UnsubscribeAll(c.GetId())
skuSet := make(map[string]bool)
func (c *CartGrain) HandleInventoryChange(change inventory.InventoryChange) {
for _, item := range c.Items {
skuSet[item.Sku] = true
}
for sku := range skuSet {
pubsub.Subscribe("inventory:"+sku, c.GetId())
if item.Sku == change.SKU && change.StockLocationID == *item.StoreId {
item.Stock = uint16(change.Value)
break
}
}
}