2023-03-21 18:55:21 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-05-11 10:10:53 -05:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-03-21 18:55:21 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using MSAdminUsuarios.Context;
|
|
|
|
|
using MSAdminUsuarios.Controllers;
|
|
|
|
|
using MSAdminUsuarios.Models;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using RabbitMQ.Client;
|
|
|
|
|
using RabbitMQ.Client.Events;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace MSAdminUsuarios.Utils
|
|
|
|
|
{
|
2023-12-06 14:11:53 -05:00
|
|
|
|
public readonly struct MQExchanges
|
|
|
|
|
{
|
|
|
|
|
public static readonly string AgendaMedica = "MsAgendaMedica";
|
|
|
|
|
public static readonly string Usuarios = "MSAdminUsuarios";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public readonly struct MQueues
|
|
|
|
|
{
|
2024-05-16 16:37:29 -05:00
|
|
|
|
//AdminUsuarios
|
2023-12-06 14:11:53 -05:00
|
|
|
|
public static readonly string PerfilesPorUsuario = "PerfilesPorUsuario";
|
|
|
|
|
public static readonly string Usuarios = "Usuarios";
|
2024-05-16 16:37:29 -05:00
|
|
|
|
|
|
|
|
|
// Agenda
|
2023-12-06 14:11:53 -05:00
|
|
|
|
public static readonly string FirmaUsuario = $"{Usuarios}.Firma";
|
2024-05-16 16:37:29 -05:00
|
|
|
|
public static readonly string medico = "UsuarioMedico";
|
2023-12-06 14:11:53 -05:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public class MsComunicadoresModel
|
|
|
|
|
{
|
|
|
|
|
public string exchange { get; set; } = "";
|
|
|
|
|
public List<string> queues { get; set; } = new();
|
|
|
|
|
}
|
2023-03-21 18:55:21 -05:00
|
|
|
|
public static class RabbitMQService
|
|
|
|
|
{
|
|
|
|
|
private static string[] _queues = Array.Empty<string>();
|
|
|
|
|
private static string _exchange = "MSAdminUsuarios";
|
2023-12-06 14:11:53 -05:00
|
|
|
|
private static string Exchange
|
|
|
|
|
{
|
|
|
|
|
get { return _exchange; }
|
|
|
|
|
set { _exchange = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static readonly MsComunicadoresModel ExchangePrincipal = new()
|
|
|
|
|
{
|
|
|
|
|
exchange = Exchange,
|
|
|
|
|
queues = { MQueues.PerfilesPorUsuario, MQueues.Usuarios }
|
2024-07-15 08:01:51 -05:00
|
|
|
|
|
2023-12-06 14:11:53 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static readonly List<MsComunicadoresModel> ListaExchanges = new() {
|
|
|
|
|
ExchangePrincipal,
|
|
|
|
|
new() {
|
|
|
|
|
exchange = MQExchanges.AgendaMedica,
|
|
|
|
|
queues = { MQueues.medico }
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static IModel GetRabbitMQChannel(IServiceProvider serviceProvider, string projectName)
|
|
|
|
|
{
|
|
|
|
|
var connection = serviceProvider.GetService<IConnection>();
|
|
|
|
|
|
|
|
|
|
if (connection == null)
|
|
|
|
|
throw new Exception("Connection is null.");
|
|
|
|
|
|
|
|
|
|
var channel = connection.CreateModel();
|
|
|
|
|
|
|
|
|
|
//Declare exchange if it doesnt already exist
|
|
|
|
|
var exchangeName = projectName;
|
2023-12-06 14:11:53 -05:00
|
|
|
|
Exchange = exchangeName;
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
2024-07-15 08:01:51 -05:00
|
|
|
|
ListaExchanges.ForEach(ex =>
|
|
|
|
|
{
|
2023-12-06 14:11:53 -05:00
|
|
|
|
channel.ExchangeDeclare(
|
|
|
|
|
exchange: ex.exchange,
|
|
|
|
|
type: ExchangeType.Topic,
|
2023-03-21 18:55:21 -05:00
|
|
|
|
durable: true,
|
|
|
|
|
autoDelete: false,
|
|
|
|
|
arguments: null
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-15 08:01:51 -05:00
|
|
|
|
ex.queues.ForEach(q =>
|
|
|
|
|
{
|
2023-12-06 14:11:53 -05:00
|
|
|
|
string queue = $"{ex.exchange}.{q}";
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
2023-12-06 14:11:53 -05:00
|
|
|
|
channel.QueueDeclare(
|
|
|
|
|
queue: queue,
|
|
|
|
|
durable: true,
|
|
|
|
|
exclusive: false,
|
|
|
|
|
autoDelete: false,
|
|
|
|
|
arguments: null
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
channel.QueueBind(
|
|
|
|
|
queue: queue,
|
|
|
|
|
exchange: ex.exchange,
|
|
|
|
|
routingKey: $"{queue}.*"
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-11-07 08:35:29 -05:00
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
return channel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ListenForIntegrationEvents(string projectName, WebApplicationBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
RabbitMQConfig mqConfig = builder.Configuration.GetSection("RabbitMQConfig").Get<RabbitMQConfig>();
|
|
|
|
|
|
|
|
|
|
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().AddEventLog());
|
2023-12-06 14:11:53 -05:00
|
|
|
|
//var logger = loggerFactory.CreateLogger("Rabbit2");
|
|
|
|
|
//logger.LogInformation("Inicia Rabbitmq con");
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
2023-12-06 14:11:53 -05:00
|
|
|
|
ConnectionFactory factory = new()
|
|
|
|
|
{
|
|
|
|
|
HostName = mqConfig.HostName,
|
|
|
|
|
UserName = mqConfig.UserName,
|
|
|
|
|
Password = mqConfig.Password,
|
2024-05-16 16:37:29 -05:00
|
|
|
|
Port = mqConfig.Port
|
2023-12-06 14:11:53 -05:00
|
|
|
|
};
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
2023-12-06 14:11:53 -05:00
|
|
|
|
IConnection connection = factory.CreateConnection();
|
|
|
|
|
IModel channelReceptor = connection.CreateModel();
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
2023-12-06 14:11:53 -05:00
|
|
|
|
EventingBasicConsumer consumer = new(channelReceptor);
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
2023-12-06 14:11:53 -05:00
|
|
|
|
consumer.Received += RabbitMQService.ConsumeRabbitMQEvent;
|
2023-11-07 08:35:29 -05:00
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
2023-12-06 14:11:53 -05:00
|
|
|
|
foreach (var queue in ExchangePrincipal.queues)
|
2023-03-21 18:55:21 -05:00
|
|
|
|
{
|
2023-12-06 14:11:53 -05:00
|
|
|
|
channelReceptor.BasicConsume(
|
|
|
|
|
queue: $"{ExchangePrincipal.exchange}.{queue}",
|
|
|
|
|
autoAck: false,
|
|
|
|
|
consumer: consumer
|
|
|
|
|
);
|
2023-03-21 18:55:21 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static async void ConsumeRabbitMQEvent(object? sender, BasicDeliverEventArgs ea)
|
|
|
|
|
{
|
|
|
|
|
var body = ea.Body.ToArray();
|
|
|
|
|
var message = Encoding.UTF8.GetString(body);
|
|
|
|
|
|
|
|
|
|
var route = ea.RoutingKey;
|
|
|
|
|
|
|
|
|
|
if (message == null || message.Length == 0)
|
2024-07-15 08:01:51 -05:00
|
|
|
|
{
|
2023-03-21 18:55:21 -05:00
|
|
|
|
throw new Exception("Datos no recibidos");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (route == null || route.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("RouteKey no recibida");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var controller = route.Split(".");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var consumer = (EventingBasicConsumer)sender!;
|
|
|
|
|
var model = consumer.Model;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().AddEventLog());
|
|
|
|
|
var logger = loggerFactory.CreateLogger("RMQ.MsAdminUsuarios");
|
|
|
|
|
|
|
|
|
|
ModelContext context = new();
|
|
|
|
|
|
2024-07-15 08:01:51 -05:00
|
|
|
|
/*if (controller[1] == "PerfilesPorUsuario")
|
2023-03-21 18:55:21 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var data = JsonConvert.DeserializeObject<PERFILPORUSUARIO[]>(message);
|
|
|
|
|
|
|
|
|
|
var PPUCtrl = new PerfilesPorUsuarioController(context);
|
|
|
|
|
var result = await PPUCtrl.GuardarPerfilesPorUsuario(data!);
|
2024-07-15 08:01:51 -05:00
|
|
|
|
logger.LogWarning(20, result.ToString());
|
2023-05-11 10:10:53 -05:00
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
|
|
|
|
if (result is OkResult)
|
|
|
|
|
{
|
|
|
|
|
model.BasicAck(ea.DeliveryTag, false);
|
|
|
|
|
}
|
|
|
|
|
else throw new Exception();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// Temporalmente solo logeamos el error en lugar de rechazar
|
2023-05-11 10:10:53 -05:00
|
|
|
|
logger.LogCritical(1, ex.ToString());
|
|
|
|
|
logger.LogCritical(2, ex.Message);
|
|
|
|
|
logger.LogCritical(3, ex.InnerException?.ToString());
|
2023-03-21 18:55:21 -05:00
|
|
|
|
logger.LogCritical(ExMessage(ex));
|
|
|
|
|
//model.BasicReject(ea.DeliveryTag, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model.BasicAck(ea.DeliveryTag, false);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (controller[1] == "Usuarios")
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-07-15 08:01:51 -05:00
|
|
|
|
|
2023-12-06 14:11:53 -05:00
|
|
|
|
var UsuariosCtrl = new UsuariosController(context, model);
|
2023-09-21 15:11:09 -05:00
|
|
|
|
if (controller[2] == "post")
|
|
|
|
|
{
|
|
|
|
|
var data = JsonConvert.DeserializeObject<USUARIO>(message);
|
|
|
|
|
IActionResult result = await UsuariosCtrl.GuardarUsuarios(data!);
|
|
|
|
|
if (result is OkResult)
|
|
|
|
|
{
|
|
|
|
|
model.BasicAck(ea.DeliveryTag, false);
|
|
|
|
|
}
|
|
|
|
|
else throw new Exception(((ConflictObjectResult)result).Value?.ToString() ?? "No registra log");
|
|
|
|
|
}
|
|
|
|
|
if (controller[2] == "patch")
|
|
|
|
|
{
|
2024-07-15 08:01:51 -05:00
|
|
|
|
if (controller.Length == 3)
|
2023-09-21 15:11:09 -05:00
|
|
|
|
{
|
|
|
|
|
var data = JsonConvert.DeserializeObject<USUARIO>(message);
|
|
|
|
|
IActionResult result = await UsuariosCtrl.EditarUsuarios(data!);
|
|
|
|
|
if (result is OkResult)
|
|
|
|
|
{
|
|
|
|
|
model.BasicAck(ea.DeliveryTag, false);
|
|
|
|
|
}
|
|
|
|
|
else throw new Exception(((ConflictObjectResult)result).Value?.ToString() ?? "No registra log");
|
|
|
|
|
}
|
|
|
|
|
if (controller[3] == "Firma")
|
|
|
|
|
{
|
|
|
|
|
var data = JsonConvert.DeserializeObject<USUARIO>(message);
|
|
|
|
|
IActionResult result = await UsuariosCtrl.EditarFirma(data!);
|
|
|
|
|
if (result is OkResult)
|
|
|
|
|
{
|
|
|
|
|
model.BasicAck(ea.DeliveryTag, false);
|
|
|
|
|
}
|
|
|
|
|
else throw new Exception(((ConflictObjectResult)result).Value?.ToString() ?? "No registra log");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// Temporalmente solo logeamos el error en lugar de rechazar
|
|
|
|
|
logger.LogCritical(ExMessage(ex));
|
|
|
|
|
//model.BasicReject(ea.DeliveryTag, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model.BasicAck(ea.DeliveryTag, false);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2024-07-15 08:01:51 -05:00
|
|
|
|
}*/
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string ExMessage(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
sb.Append(ex.Message)
|
|
|
|
|
.Append(ex.InnerException);
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|