public static async Task processShipments( [CosmosDBTrigger( databaseName: "inventory", collectionName: "shipments", ConnectionStringSetting = "CosmosDBConnection", LeaseCollectionName = "ShipmentLeases", CreateLeaseCollectionIfNotExists = true)] IReadOnlyList <Document> documents, [DurableClient] IDurableOrchestrationClient starter, ILogger log) { string firstId = documents[0].Id; int count = documents.Count; log.LogInformation($"Processing '{count}' shipment events - start id: '{firstId}'."); foreach (Document document in documents) { ShipmentEvent shipmentEvent = JsonConvert.DeserializeObject <ShipmentEvent>(document.ToString()); foreach (ShipmentItem item in shipmentEvent.Items) { var inventoryEvent = new InventoryEvent(); inventoryEvent.Id = shipmentEvent.Id; inventoryEvent.DivisionId = shipmentEvent.DivisionId; inventoryEvent.StoreId = shipmentEvent.StoreId; inventoryEvent.CountAdjustment = item.ShipmentAmount; inventoryEvent.Upc = item.Upc; inventoryEvent.LastShipmentTimestamp = shipmentEvent.LastShipmentTimestamp; await starter.StartNewAsync <InventoryEvent>("InventoryOrchestration", null, inventoryEvent).ConfigureAwait(false); } } }
public static async Task processOnHand( [CosmosDBTrigger( databaseName: "inventory", collectionName: "onHand", ConnectionStringSetting = "CosmosDBConnection", LeaseCollectionName = "OnHandLeases", CreateLeaseCollectionIfNotExists = true)] IReadOnlyList <Document> documents, [DurableClient] IDurableOrchestrationClient starter, ILogger log) { string firstId = documents[0].Id; int count = documents.Count; log.LogInformation($"Processing '{count}' onHand events - start id: '{firstId}'."); foreach (Document document in documents) { OnHandEvent onHandEvent = JsonConvert.DeserializeObject <OnHandEvent>(document.ToString()); var inventoryEvent = new InventoryEvent(); inventoryEvent.Id = onHandEvent.Id; inventoryEvent.DivisionId = onHandEvent.DivisionId; inventoryEvent.StoreId = onHandEvent.StoreId; inventoryEvent.CountAdjustment = onHandEvent.InventoryCount; inventoryEvent.Upc = onHandEvent.Upc; inventoryEvent.Description = onHandEvent.Description; inventoryEvent.ProductName = onHandEvent.ProductName; inventoryEvent.LastUpdateTimestamp = onHandEvent.LastUpdateTimestamp; await starter.StartNewAsync <InventoryEvent>("InventoryOrchestration", null, inventoryEvent).ConfigureAwait(false); } }
public static async Task RunOrchestrator( [OrchestrationTrigger] IDurableOrchestrationContext context) { InventoryEvent data = context.GetInput <InventoryEvent>(); var entityId = new EntityId("Store", data.StoreId); var proxy = context.CreateEntityProxy <IStore>(entityId); var itemInventory = await proxy.process(data); await context.CallActivityAsync <string>("MDSWrite", itemInventory); }
Task <ItemInventory> process(InventoryEvent inventoryEvent) { if (DivisionId == null) { DivisionId = inventoryEvent.DivisionId; StoreId = inventoryEvent.StoreId; } if (!Items.TryGetValue(inventoryEvent.Upc, out var currentItem)) { this.Items[inventoryEvent.Upc] = new StoreItem() { Upc = inventoryEvent.Upc, InventoryCount = inventoryEvent.CountAdjustment, ProductName = inventoryEvent.ProductName, Description = inventoryEvent.Description, LastUpdateTimestamp = inventoryEvent.LastShipmentTimestamp, LastShipmentTimestamp = inventoryEvent.LastShipmentTimestamp }; } else { if (inventoryEvent.LastShipmentTimestamp == null) { currentItem.InventoryCount = inventoryEvent.CountAdjustment; } else { currentItem.InventoryCount += inventoryEvent.CountAdjustment; } } currentItem = Items[inventoryEvent.Upc]; ItemInventory itemInventory = new ItemInventory(); itemInventory.Id = BuildItemId(DivisionId, StoreId, currentItem.Upc); itemInventory.DivisionId = DivisionId; itemInventory.StoreId = StoreId; itemInventory.Upc = currentItem.Upc; itemInventory.ProductName = itemInventory.ProductName; itemInventory.Description = currentItem.Description; itemInventory.LastShipmentTimestamp = itemInventory.LastShipmentTimestamp; itemInventory.LastUpdateTimestamp = itemInventory.LastUpdateTimestamp; Console.WriteLine($"[{DivisionId}/{StoreId}] Inventory event '{inventoryEvent.Id}"); return(Task.FromResult(itemInventory)); }