diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderCompletedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderCompletedIntegrationEventHandler.cs new file mode 100644 index 000000000..dcc3f0c43 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderCompletedIntegrationEventHandler.cs @@ -0,0 +1,40 @@ +namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.IntegrationEvents.EventHandling; + +public class OrderCompletedIntegrationEventHandler : IIntegrationEventHandler +{ + private readonly IMediator _mediator; + private readonly ILogger _logger; + + public OrderCompletedIntegrationEventHandler( + IMediator mediator, + ILogger logger) + { + _mediator = mediator; + _logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); + } + + /// + /// Event handler which confirms order is completed. + /// + /// + /// + /// + public async Task Handle(OrderCompletedIntegrationEvent @event) + { + using (_logger.BeginScope(new List> { new ("IntegrationEventContext", @event.Id) })) + { + _logger.LogInformation("Handling integration event: {IntegrationEventId} - ({@IntegrationEvent})", @event.Id, @event); + + var command = new SetAwaitingValidationOrderStatusCommand(@event.OrderId); + + _logger.LogInformation( + "Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})", + command.GetGenericTypeName(), + nameof(command.OrderNumber), + command.OrderNumber, + command); + + await _mediator.Send(command); + } + } +} diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderCompletedIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderCompletedIntegrationEvent.cs new file mode 100644 index 000000000..555cb1ee4 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderCompletedIntegrationEvent.cs @@ -0,0 +1,10 @@ +namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.IntegrationEvents.Events; + +public record OrderCompletedIntegrationEvent : IntegrationEvent +{ + public int OrderId { get; } + + public OrderCompletedIntegrationEvent(int orderId) => + OrderId = orderId; +} + diff --git a/src/Services/Ordering/Ordering.API/Program.cs b/src/Services/Ordering/Ordering.API/Program.cs index cab0e0b95..e3bd21f2a 100644 --- a/src/Services/Ordering/Ordering.API/Program.cs +++ b/src/Services/Ordering/Ordering.API/Program.cs @@ -35,6 +35,7 @@ services.AddScoped(); // Add integration event handlers. services.AddTransient, GracePeriodConfirmedIntegrationEventHandler>(); +services.AddTransient, OrderCompletedIntegrationEventHandler>(); services.AddTransient, OrderPaymentFailedIntegrationEventHandler>(); services.AddTransient, OrderPaymentSucceededIntegrationEventHandler>(); services.AddTransient, OrderStockConfirmedIntegrationEventHandler>(); @@ -52,6 +53,7 @@ var eventBus = app.Services.GetRequiredService(); eventBus.Subscribe>(); eventBus.Subscribe>(); +eventBus.Subscribe>(); eventBus.Subscribe>(); eventBus.Subscribe>(); eventBus.Subscribe>();