Sistema de Notificaciones y Baja One-Click
This commit is contained in:
@@ -14,11 +14,19 @@ public class ChatController : ControllerBase
|
||||
{
|
||||
private readonly MotoresV2DbContext _context;
|
||||
private readonly INotificationService _notificationService;
|
||||
private readonly INotificationPreferenceService _prefService;
|
||||
private readonly string _frontendUrl;
|
||||
|
||||
public ChatController(MotoresV2DbContext context, INotificationService notificationService)
|
||||
public ChatController(
|
||||
MotoresV2DbContext context,
|
||||
INotificationService notificationService,
|
||||
INotificationPreferenceService prefService,
|
||||
IConfiguration config)
|
||||
{
|
||||
_context = context;
|
||||
_notificationService = notificationService;
|
||||
_prefService = prefService;
|
||||
_frontendUrl = config["AppSettings:FrontendUrl"]?.Split(',')[0].Trim() ?? "http://localhost:5173";
|
||||
}
|
||||
|
||||
[HttpPost("send")]
|
||||
@@ -39,26 +47,35 @@ public class ChatController : ControllerBase
|
||||
|
||||
if (receiver != null && !string.IsNullOrEmpty(receiver.Email))
|
||||
{
|
||||
// LÓGICA DE NOMBRE DE REMITENTE
|
||||
string senderDisplayName;
|
||||
|
||||
if (sender != null && sender.UserType == 3) // 3 = ADMIN
|
||||
// Solo enviar correo si la preferencia "mensajes" está habilitada
|
||||
if (await _prefService.IsEnabledAsync(receiver.UserID, NotificationCategory.Mensajes))
|
||||
{
|
||||
// Caso: Moderador escribe a Usuario
|
||||
senderDisplayName = "Un moderador de Motores Argentinos";
|
||||
}
|
||||
else
|
||||
{
|
||||
// Caso: Usuario responde a Moderador
|
||||
string name = sender?.UserName ?? "Un usuario";
|
||||
senderDisplayName = $"El usuario {name}";
|
||||
}
|
||||
// LÓGICA DE NOMBRE DE REMITENTE
|
||||
string senderDisplayName;
|
||||
|
||||
await _notificationService.SendChatNotificationEmailAsync(
|
||||
receiver.Email,
|
||||
senderDisplayName, // Pasamos el nombre formateado
|
||||
msg.MessageText,
|
||||
msg.AdID);
|
||||
if (sender != null && sender.UserType == 3) // 3 = ADMIN
|
||||
{
|
||||
// Caso: Moderador escribe a Usuario
|
||||
senderDisplayName = "Un moderador de Motores Argentinos";
|
||||
}
|
||||
else
|
||||
{
|
||||
// Caso: Usuario responde a Moderador
|
||||
string name = sender?.UserName ?? "Un usuario";
|
||||
senderDisplayName = $"El usuario {name}";
|
||||
}
|
||||
|
||||
// Generamos el token de baja para la categoría "mensajes"
|
||||
var rawToken = await _prefService.GetOrCreateUnsubscribeTokenAsync(receiver.UserID, NotificationCategory.Mensajes);
|
||||
var unsubscribeUrl = $"{_frontendUrl}/baja/procesar?token={Uri.EscapeDataString(rawToken)}";
|
||||
|
||||
await _notificationService.SendChatNotificationEmailAsync(
|
||||
receiver.Email,
|
||||
senderDisplayName, // Pasamos el nombre formateado
|
||||
msg.MessageText,
|
||||
msg.AdID,
|
||||
unsubscribeUrl); // Se incluye URL de baja en el footer
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in New Issue
Block a user