update
This commit is contained in:
@@ -2,6 +2,7 @@ package cart
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -234,12 +235,21 @@ 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:") {
|
||||
sku := strings.TrimPrefix(event.Topic, "inventory:")
|
||||
skuAndLocationId := strings.TrimPrefix(event.Topic, "inventory:")
|
||||
parts := strings.Split(skuAndLocationId, ":")
|
||||
if len(parts) != 2 {
|
||||
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 {
|
||||
if item.Sku == sku && item.StoreId != nil && *item.StoreId == locationId {
|
||||
// Update stock status based on payload, e.g., if payload is bool available
|
||||
if available, ok := event.Payload.(bool); ok {
|
||||
if 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
|
||||
|
||||
Reference in New Issue
Block a user