Corrigir classificação de notificações vencidas

This commit is contained in:
Eduardo Lopes 2026-02-02 11:06:44 -03:00
parent aca7f4e74a
commit 2371d0fff8
1 changed files with 91 additions and 0 deletions

View File

@ -119,6 +119,11 @@ public class VigenciaNotificationBackgroundService : BackgroundService
.Where(v => v.DtTerminoFidelizacao != null) .Where(v => v.DtTerminoFidelizacao != null)
.ToListAsync(stoppingToken); .ToListAsync(stoppingToken);
if (vigencias.Count > 0)
{
await CleanupOutdatedNotificationsAsync(db, vigencias, reminderDays, today, stoppingToken);
}
var candidates = new List<Notification>(); var candidates = new List<Notification>();
foreach (var vigencia in vigencias) foreach (var vigencia in vigencias)
{ {
@ -223,6 +228,92 @@ public class VigenciaNotificationBackgroundService : BackgroundService
await db.SaveChangesAsync(stoppingToken); await db.SaveChangesAsync(stoppingToken);
} }
private static async Task CleanupOutdatedNotificationsAsync(
AppDbContext db,
IReadOnlyCollection<VigenciaLine> vigencias,
IReadOnlyCollection<int> reminderDays,
DateTime today,
CancellationToken stoppingToken)
{
var vigenciasById = vigencias.ToDictionary(v => v.Id, v => v);
var vigenciasByLinha = vigencias
.Where(v => !string.IsNullOrWhiteSpace(v.Linha))
.GroupBy(v => v.Linha!)
.Select(g => g.OrderByDescending(v => v.UpdatedAt).First())
.ToDictionary(v => v.Linha!, v => v);
var existingNotifications = await db.Notifications.AsNoTracking()
.Where(n => n.Tipo == "Vencido" || n.Tipo == "AVencer")
.ToListAsync(stoppingToken);
if (existingNotifications.Count == 0)
{
return;
}
var idsToDelete = new List<Guid>();
foreach (var notification in existingNotifications)
{
var vigencia = ResolveVigencia(notification, vigenciasById, vigenciasByLinha);
if (vigencia?.DtTerminoFidelizacao is null)
{
continue;
}
var endDate = vigencia.DtTerminoFidelizacao.Value.Date;
if (endDate < today)
{
if (notification.Tipo != "Vencido")
{
idsToDelete.Add(notification.Id);
}
continue;
}
var daysUntil = (endDate - today).Days;
if (notification.Tipo == "Vencido")
{
idsToDelete.Add(notification.Id);
continue;
}
if (!reminderDays.Contains(daysUntil) || notification.DiasParaVencer != daysUntil)
{
idsToDelete.Add(notification.Id);
}
}
if (idsToDelete.Count == 0)
{
return;
}
await db.Notifications
.Where(n => idsToDelete.Contains(n.Id))
.ExecuteDeleteAsync(stoppingToken);
}
private static VigenciaLine? ResolveVigencia(
Notification notification,
IReadOnlyDictionary<Guid, VigenciaLine> vigenciasById,
IReadOnlyDictionary<string, VigenciaLine> vigenciasByLinha)
{
if (notification.VigenciaLineId.HasValue
&& vigenciasById.TryGetValue(notification.VigenciaLineId.Value, out var byId))
{
return byId;
}
if (!string.IsNullOrWhiteSpace(notification.Linha)
&& vigenciasByLinha.TryGetValue(notification.Linha, out var byLinha))
{
return byLinha;
}
return null;
}
private static Notification BuildNotification( private static Notification BuildNotification(
string tipo, string tipo,
string titulo, string titulo,