Sistema de Notificaciones y Baja One-Click

This commit is contained in:
2026-03-12 13:52:33 -03:00
parent f1a9bb9099
commit 96fca4d9c7
21 changed files with 1384 additions and 79 deletions

View File

@@ -18,12 +18,21 @@ public class AdminController : ControllerBase
private readonly MotoresV2DbContext _context;
private readonly IAdSyncService _syncService;
private readonly INotificationService _notificationService;
private readonly INotificationPreferenceService _prefService;
private readonly string _frontendUrl;
public AdminController(MotoresV2DbContext context, IAdSyncService syncService, INotificationService notificationService)
public AdminController(
MotoresV2DbContext context,
IAdSyncService syncService,
INotificationService notificationService,
INotificationPreferenceService prefService,
IConfiguration config)
{
_context = context;
_syncService = syncService;
_notificationService = notificationService;
_prefService = prefService;
_frontendUrl = config["AppSettings:FrontendUrl"]?.Split(',')[0].Trim() ?? "http://localhost:5173";
}
// --- MODERACIÓN ---
@@ -160,12 +169,22 @@ public class AdminController : ControllerBase
});
await _context.SaveChangesAsync();
// Sincronizar a Legacy
// Sincronizar a Legacy y notificar aprobación (categoría: sistema)
try
{
await _syncService.SyncAdToLegacyAsync(id);
var adTitle = $"{ad.Brand?.Name} {ad.VersionName}";
await _notificationService.SendAdStatusChangedEmailAsync(ad.User?.Email ?? string.Empty, adTitle, "APROBADO");
// Generamos el token de baja para la categoría "sistema"
string? unsubscribeUrl = null;
if (ad.User != null)
{
var rawToken = await _prefService.GetOrCreateUnsubscribeTokenAsync(ad.User.UserID, NotificationCategory.Sistema);
unsubscribeUrl = $"{_frontendUrl}/baja/procesar?token={Uri.EscapeDataString(rawToken)}";
}
await _notificationService.SendAdStatusChangedEmailAsync(
ad.User?.Email ?? string.Empty, adTitle, "APROBADO", null, unsubscribeUrl);
}
catch (Exception)
{
@@ -197,9 +216,19 @@ public class AdminController : ControllerBase
});
await _context.SaveChangesAsync();
// Notificar rechazo
// Notificar rechazo (categoría: sistema)
var adTitle = $"{ad.Brand?.Name} {ad.VersionName}";
await _notificationService.SendAdStatusChangedEmailAsync(ad.User?.Email ?? string.Empty, adTitle, "RECHAZADO", reason);
// Generamos el token de baja para la categoría "sistema"
string? unsubscribeUrl = null;
if (ad.User != null)
{
var rawToken = await _prefService.GetOrCreateUnsubscribeTokenAsync(ad.User.UserID, NotificationCategory.Sistema);
unsubscribeUrl = $"{_frontendUrl}/baja/procesar?token={Uri.EscapeDataString(rawToken)}";
}
await _notificationService.SendAdStatusChangedEmailAsync(
ad.User?.Email ?? string.Empty, adTitle, "RECHAZADO", reason, unsubscribeUrl);
return Ok(new { message = "Aviso rechazado." });
}