try to update inventory on spawn
This commit is contained in:
@@ -71,6 +71,21 @@ type CartChangeEvent struct {
|
|||||||
Mutations []actor.ApplyResult `json:"mutations"`
|
Mutations []actor.ApplyResult `json:"mutations"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func matchesSkuAndLocation(update inventory.InventoryResult, item cart.CartItem) bool {
|
||||||
|
if string(update.SKU) == item.Sku {
|
||||||
|
if update.LocationID == "se" && item.StoreId == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if item.StoreId == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if *item.StoreId == string(update.LocationID) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
controlPlaneConfig := actor.DefaultServerConfig()
|
controlPlaneConfig := actor.DefaultServerConfig()
|
||||||
@@ -129,8 +144,32 @@ func main() {
|
|||||||
grainSpawns.Inc()
|
grainSpawns.Inc()
|
||||||
ret := cart.NewCartGrain(id, time.Now())
|
ret := cart.NewCartGrain(id, time.Now())
|
||||||
// Set baseline lastChange at spawn; replay may update it to last event timestamp.
|
// Set baseline lastChange at spawn; replay may update it to last event timestamp.
|
||||||
|
|
||||||
inventoryPubSub.Subscribe(ret.HandleInventoryChange)
|
inventoryPubSub.Subscribe(ret.HandleInventoryChange)
|
||||||
err := diskStorage.LoadEvents(ctx, id, ret)
|
err := diskStorage.LoadEvents(ctx, id, ret)
|
||||||
|
if err == nil && inventoryService != nil {
|
||||||
|
refs := make([]*inventory.InventoryReference, 0)
|
||||||
|
for _, item := range ret.Items {
|
||||||
|
refs = append(refs, &inventory.InventoryReference{
|
||||||
|
SKU: inventory.SKU(item.Sku),
|
||||||
|
LocationID: getLocationId(item),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
_, span := tracer.Start(ctx, "update inventory")
|
||||||
|
defer span.End()
|
||||||
|
res, err := inventoryService.GetInventoryBatch(ctx, refs...)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("unable to update inventory %v", err)
|
||||||
|
} else {
|
||||||
|
for _, update := range res {
|
||||||
|
for _, item := range ret.Items {
|
||||||
|
if matchesSkuAndLocation(update, *item) {
|
||||||
|
item.Quantity = uint16(update.Quantity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return ret, err
|
return ret, err
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user