update subscriptions
This commit is contained in:
@@ -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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user