update subscriptions
Some checks failed
Build and Publish / BuildAndDeployAmd64 (push) Successful in 37s
Build and Publish / BuildAndDeployArm64 (push) Has been cancelled

This commit is contained in:
2025-11-25 21:13:44 +01:00
parent e7e572b0f7
commit 990708597b
2 changed files with 15 additions and 17 deletions

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) { inventory.NewInventoryChangeListener(inventoryRedisClient, context.Background(), func(changes []inventory.InventoryChange) {
for _, change := range changes { for _, change := range changes {
srv.GrainPool.GetPubSub().Publish(actor.Event{ srv.GrainPool.GetPubSub().Publish(actor.Event{
Topic: fmt.Sprintf("inventory:%s:%s", change.SKU, change.StockLocationID), Topic: fmt.Sprintf("inventory:%s", change.SKU),
Payload: change.Value, Payload: change,
}) })
} }
}) })

View File

@@ -11,6 +11,7 @@ import (
"git.k6n.net/go-cart-actor/pkg/actor" "git.k6n.net/go-cart-actor/pkg/actor"
messages "git.k6n.net/go-cart-actor/pkg/messages" messages "git.k6n.net/go-cart-actor/pkg/messages"
"git.k6n.net/go-cart-actor/pkg/voucher" "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. // 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() defer c.mu.Unlock()
// Example: if event is inventory change for a SKU in the cart // Example: if event is inventory change for a SKU in the cart
if strings.HasPrefix(event.Topic, "inventory:") { if strings.HasPrefix(event.Topic, "inventory:") {
skuAndLocationId := strings.TrimPrefix(event.Topic, "inventory:") sku := strings.TrimPrefix(event.Topic, "inventory:")
parts := strings.Split(skuAndLocationId, ":")
if len(parts) != 2 { 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 return
} }
sku := parts[0]
locationId := parts[1]
log.Printf("cart grain got inventory update: %s", event.Topic)
for _, item := range c.Items { 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 // 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", update.Value)
log.Printf("cart grain got item stock update %d", quantity)
if quantity > 0 { item.Stock = uint16(update.Value)
item.Stock = 1 // assuming 1 is in stock
} else {
item.Stock = 0 // out of stock
}
}
break break
} }
} }