diff --git a/src/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs b/src/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs index 9b0ea73c9..71bcc6546 100644 --- a/src/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs +++ b/src/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs @@ -22,6 +22,15 @@ public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent confirmedOrderStockItems.Add(confirmedOrderStockItem); } + else + { + // Product no longer exists in catalog (may have been deleted) + // Treat as out of stock to reject the order + logger.LogWarning("Product with id {ProductId} in order {OrderId} not found in catalog", + orderStockItem.ProductId, @event.OrderId); + var confirmedOrderStockItem = new ConfirmedOrderStockItem(orderStockItem.ProductId, false); + confirmedOrderStockItems.Add(confirmedOrderStockItem); + } } var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.HasStock) diff --git a/src/WebApp/Services/BasketState.cs b/src/WebApp/Services/BasketState.cs index e03b37730..fcaa9f619 100644 --- a/src/WebApp/Services/BasketState.cs +++ b/src/WebApp/Services/BasketState.cs @@ -10,7 +10,8 @@ public class BasketState( BasketService basketService, CatalogService catalogService, OrderingService orderingService, - AuthenticationStateProvider authenticationStateProvider) : IBasketState + AuthenticationStateProvider authenticationStateProvider, + ILogger logger) : IBasketState { private Task>? _cachedBasket; private HashSet _changeSubscriptions = new(); @@ -132,7 +133,13 @@ async Task> FetchCoreAsync() var catalogItems = (await catalogService.GetCatalogItems(productIds)).ToDictionary(k => k.Id, v => v); foreach (var item in quantities) { - var catalogItem = catalogItems[item.ProductId]; + // Skip items that no longer exist in the catalog (may have been deleted) + if (!catalogItems.TryGetValue(item.ProductId, out var catalogItem)) + { + logger.LogWarning("Basket item with ProductId {ProductId} not found in catalog and will be skipped", item.ProductId); + continue; + } + var orderItem = new BasketItem { Id = Guid.NewGuid().ToString(), // TODO: this value is meaningless, use ProductId instead.