diff --git a/src/building blocks/JSE.Core/Messages/Integration/PedidoAutorizadoIntegrationEvent.cs b/src/building blocks/JSE.Core/Messages/Integration/PedidoAutorizadoIntegrationEvent.cs index fbeea69..9052888 100644 --- a/src/building blocks/JSE.Core/Messages/Integration/PedidoAutorizadoIntegrationEvent.cs +++ b/src/building blocks/JSE.Core/Messages/Integration/PedidoAutorizadoIntegrationEvent.cs @@ -13,4 +13,5 @@ public PedidoAutorizadoIntegrationEvent(Guid clienteId, Guid pedidoId, IDictiona Itens = itens; } } + } \ No newline at end of file diff --git a/src/services/JSE.Pedido.API/Services/PedidoIntegrationHandler.cs b/src/services/JSE.Pedido.API/Services/PedidoIntegrationHandler.cs new file mode 100644 index 0000000..598874d --- /dev/null +++ b/src/services/JSE.Pedido.API/Services/PedidoIntegrationHandler.cs @@ -0,0 +1,69 @@ +using JSE.Core.DomainObjects; +using JSE.Core.Messages.Integration; +using JSE.MessageBus; +using JSE.Pedidos.Domain.Pedidos; + +namespace JSE.Pedidos.API.Services +{ + public class PedidoIntegrationHandler : BackgroundService + { + private readonly IMessageBus _bus; + private readonly IServiceProvider _serviceProvider; + + public PedidoIntegrationHandler(IServiceProvider serviceProvider, IMessageBus bus) + { + _serviceProvider = serviceProvider; + _bus = bus; + } + protected override Task ExecuteAsync(CancellationToken stoppingToken) + { + SetSubscribers(); + return Task.CompletedTask; + } + + private void SetSubscribers() + { + _bus.SubscribeAsync("PedidoCancelado", + async request => await CancelarPedido(request)); + + _bus.SubscribeAsync("PedidoPago", + async request => await FinalizarPedido(request)); + } + + private async Task CancelarPedido(PedidoCanceladoIntegrationEvent message) + { + using (var scope = _serviceProvider.CreateScope()) + { + var pedidoRepository = scope.ServiceProvider.GetRequiredService(); + + var pedido = await pedidoRepository.ObterPorId(message.PedidoId); + pedido.CancelarPedido(); + + pedidoRepository.Atualizar(pedido); + + if (!await pedidoRepository.UnitOfWork.Commit()) + { + throw new DomainException($"Problemas ao cancelar o pedido {message.PedidoId}"); + } + } + } + + private async Task FinalizarPedido(PedidoPagoIntegrationEvent message) + { + using (var scope = _serviceProvider.CreateScope()) + { + var pedidoRepository = scope.ServiceProvider.GetRequiredService(); + + var pedido = await pedidoRepository.ObterPorId(message.PedidoId); + pedido.FinalizarPedido(); + + pedidoRepository.Atualizar(pedido); + + if (!await pedidoRepository.UnitOfWork.Commit()) + { + throw new DomainException($"Problemas ao finalizar o pedido {message.PedidoId}"); + } + } + } + } +} \ No newline at end of file diff --git a/src/services/JSE.Pedido.Domain/Pedidos/Pedido.cs b/src/services/JSE.Pedido.Domain/Pedidos/Pedido.cs index 5c60d06..f91130a 100644 --- a/src/services/JSE.Pedido.Domain/Pedidos/Pedido.cs +++ b/src/services/JSE.Pedido.Domain/Pedidos/Pedido.cs @@ -41,6 +41,16 @@ public void AutorizarPedido() PedidoStatus = PedidoStatus.Autorizado; } + public void CancelarPedido() + { + PedidoStatus = PedidoStatus.Cancelado; + } + + public void FinalizarPedido() + { + PedidoStatus = PedidoStatus.Pago; + } + public void AtribuirVoucher(Voucher voucher) { VoucherUtilizado = true;