-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: notificação via e-mail de pedido editado
- Loading branch information
1 parent
764914d
commit ee727f3
Showing
13 changed files
with
185 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
using Domain.Pkg.Entities; | ||
using Mensageria.Model; | ||
|
||
namespace Mensageria.Interfaces; | ||
|
||
public interface IMovimentacaoDeProdutoService | ||
{ | ||
Task MovimentarProdutosAsync(Pedido pedido, string referer); | ||
Task MovimentarProdutosAsync(IList<MovimentacaoDeEstoqueDto> movimentacaoDeEstoqueDtos, string referer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Mensageria.Model; | ||
|
||
namespace Mensageria.Interfaces; | ||
|
||
public interface INotificarPedidoEditadoService | ||
{ | ||
Task NotificarAsync(NotificarPedidoEditadoModel notificarPedidoEditadoModel); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Domain.Pkg.Enum; | ||
using Mensageria.Interfaces; | ||
using Mensageria.Model; | ||
using RabbitMQ.Client; | ||
using RabbitMQ.Client.Events; | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace Mensageria.Mensageria.Consumers; | ||
|
||
public class PedidoEntregueConsumer : BackgroundService | ||
{ | ||
private readonly IModel _channel; | ||
private const string ExchangeName = "pedido-entregue"; | ||
private readonly string _queueName; | ||
private readonly IServiceProvider _provider; | ||
private const string _mensagemDeMovimentacaoEstoque = "Entrega de pedido!"; | ||
|
||
public PedidoEntregueConsumer(IModel channel, IServiceProvider provider) | ||
{ | ||
_provider = provider; | ||
_channel = channel; | ||
_channel.ExchangeDeclare(ExchangeName, ExchangeType.Fanout); | ||
_queueName = _channel.QueueDeclare().QueueName; | ||
_channel.QueueBind(_queueName, ExchangeName, ""); | ||
} | ||
protected override Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
var consumer = new EventingBasicConsumer(_channel); | ||
|
||
consumer.Received += async (sender, e) => | ||
{ | ||
var headers = e.BasicProperties.Headers; | ||
|
||
if (headers.TryGetValue("Referer", out object? value)) | ||
{ | ||
var referer = Encoding.UTF8.GetString((byte[])value); | ||
|
||
var body = e.Body.ToArray(); | ||
var message = Encoding.UTF8.GetString(body); | ||
var pedidoCreateModel = JsonSerializer.Deserialize<PedidoCreateModel>(message); | ||
|
||
if (pedidoCreateModel != null) | ||
{ | ||
using var scope = _provider.CreateScope(); | ||
|
||
try | ||
{ | ||
var service = scope.ServiceProvider.GetRequiredService<IMovimentacaoDeProdutoService>(); | ||
var itensMovimentos = pedidoCreateModel | ||
.Pedido | ||
.ItensPedido | ||
.Select(x => new MovimentacaoDeEstoqueDto() | ||
{ | ||
TipoMovimentacaoDeProduto = TipoMovimentacaoDeProduto.Saida, | ||
Observacao = _mensagemDeMovimentacaoEstoque, | ||
PesoId = x.PesoId, | ||
ProdutoId = x.ProdutoId, | ||
Quantidade = x.Quantidade, | ||
TamanhoId = x.TamanhoId | ||
}).ToList(); | ||
await service.MovimentarProdutosAsync(itensMovimentos, referer); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Erro rabbitMq movimentar o estoque: {ex.Message}"); | ||
throw new Exception(ex.Message); | ||
} | ||
} | ||
} | ||
|
||
_channel.BasicAck(e.DeliveryTag, false); | ||
}; | ||
|
||
_channel.BasicConsume(_queueName, false, consumer); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Domain.Pkg.Enum; | ||
|
||
namespace Mensageria.Model; | ||
|
||
public class MovimentacaoDeEstoqueDto | ||
{ | ||
public Guid ProdutoId { get; set; } | ||
public Guid? PesoId { get; set; } | ||
public Guid? TamanhoId { get; set; } | ||
public decimal Quantidade { get; set; } | ||
public string? Observacao { get; set; } | ||
public TipoMovimentacaoDeProduto TipoMovimentacaoDeProduto { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Domain.Pkg.Entities; | ||
|
||
namespace Mensageria.Model; | ||
|
||
public class NotificarPedidoEditadoModel | ||
{ | ||
public string EmailEnvio { get; set; } = string.Empty; | ||
public Pedido Pedido { get; set; } = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Domain.Pkg.Interfaces; | ||
using Domain.Pkg.Model; | ||
using Mensageria.Interfaces; | ||
using Mensageria.Model; | ||
|
||
namespace Mensageria.Service; | ||
|
||
public sealed class NotificarPedidoEditadoService : INotificarPedidoEditadoService | ||
{ | ||
private readonly IEmailService _emailService; | ||
private readonly FromEnvioEmailModel _fromEmailModel; | ||
|
||
public NotificarPedidoEditadoService(IEmailService emailService) | ||
{ | ||
_fromEmailModel = new FromEnvioEmailModel() | ||
{ | ||
Email = VariaveisDeAmbiente.GetVariavel("EMAIL"), | ||
Servidor = VariaveisDeAmbiente.GetVariavel("SERVER"), | ||
Senha = VariaveisDeAmbiente.GetVariavel("SENHA"), | ||
Porta = int.Parse(VariaveisDeAmbiente.GetVariavel("PORT")) | ||
}; | ||
_emailService = emailService; | ||
} | ||
|
||
public async Task NotificarAsync(NotificarPedidoEditadoModel notificarPedidoEditadoModel) | ||
{ | ||
var message = $"Pedido editado!\nN. do pedido : {notificarPedidoEditadoModel.Pedido.Numero}"; | ||
var assunto = "Pedido editado!"; | ||
|
||
var emailModel = new ToEnvioEmailModel() | ||
{ | ||
Assunto = assunto, | ||
Email = notificarPedidoEditadoModel.EmailEnvio, | ||
Mensagem = message | ||
}; | ||
|
||
await _emailService.SendEmail(emailModel, _fromEmailModel); | ||
} | ||
} |