Skip to content

#249 - Capturar e cancelar pagamentos - feature/sp7/#249 #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public PedidoAutorizadoIntegrationEvent(Guid clienteId, Guid pedidoId, IDictiona
Itens = itens;
}
}

}
69 changes: 69 additions & 0 deletions src/services/JSE.Pedido.API/Services/PedidoIntegrationHandler.cs
Original file line number Diff line number Diff line change
@@ -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<PedidoCanceladoIntegrationEvent>("PedidoCancelado",
async request => await CancelarPedido(request));

_bus.SubscribeAsync<PedidoPagoIntegrationEvent>("PedidoPago",
async request => await FinalizarPedido(request));
}

private async Task CancelarPedido(PedidoCanceladoIntegrationEvent message)
{
using (var scope = _serviceProvider.CreateScope())
{
var pedidoRepository = scope.ServiceProvider.GetRequiredService<IPedidoRepository>();

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<IPedidoRepository>();

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}");
}
}
}
}
}
10 changes: 10 additions & 0 deletions src/services/JSE.Pedido.Domain/Pedidos/Pedido.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading