diff --git a/Services/NotificationOptions.cs b/Services/NotificationOptions.cs index ba20378..5068082 100644 --- a/Services/NotificationOptions.cs +++ b/Services/NotificationOptions.cs @@ -4,5 +4,8 @@ public class NotificationOptions { public int CheckIntervalMinutes { get; set; } = 60; public bool NotifyAllFutureDates { get; set; } = true; + // Limit how far ahead "A vencer" notifications can be generated. + // Prevents alerting contracts years in the future (e.g. 2028) when the system runs daily. + public int MaxFutureDays { get; set; } = 30; public List ReminderDays { get; set; } = new() { 30, 15, 7 }; } diff --git a/Services/VigenciaNotificationSyncService.cs b/Services/VigenciaNotificationSyncService.cs index 187a452..45b4f44 100644 --- a/Services/VigenciaNotificationSyncService.cs +++ b/Services/VigenciaNotificationSyncService.cs @@ -64,6 +64,7 @@ public class VigenciaNotificationSyncService : IVigenciaNotificationSyncService { var today = DateTime.SpecifyKind(DateTime.UtcNow.Date, DateTimeKind.Utc); var notifyAllFutureDates = _options.NotifyAllFutureDates; + var maxFutureDays = _options.MaxFutureDays <= 0 ? 30 : _options.MaxFutureDays; var reminderDays = _options.ReminderDays .Distinct() .Where(d => d > 0) @@ -86,7 +87,7 @@ public class VigenciaNotificationSyncService : IVigenciaNotificationSyncService .Where(v => v.DtTerminoFidelizacao != null) .ToListAsync(cancellationToken); - await CleanupOutdatedNotificationsAsync(vigencias, notifyAllFutureDates, reminderDays, today, cancellationToken); + await CleanupOutdatedNotificationsAsync(vigencias, notifyAllFutureDates, reminderDays, today, maxFutureDays, cancellationToken); var candidates = new List(); foreach (var vigencia in vigencias) @@ -133,6 +134,10 @@ public class VigenciaNotificationSyncService : IVigenciaNotificationSyncService } var daysUntil = (endDate - today).Days; + if (daysUntil > maxFutureDays) + { + continue; + } if (!notifyAllFutureDates && !reminderDays.Contains(daysUntil)) { continue; @@ -213,8 +218,14 @@ public class VigenciaNotificationSyncService : IVigenciaNotificationSyncService bool notifyAllFutureDates, IReadOnlyCollection reminderDays, DateTime today, + int maxFutureDays, CancellationToken cancellationToken) { + if (maxFutureDays <= 0) + { + maxFutureDays = 30; + } + var vigenciasById = vigencias.ToDictionary(v => v.Id, v => v); var vigenciasByLinha = vigencias .Where(v => !string.IsNullOrWhiteSpace(v.Linha)) @@ -253,6 +264,11 @@ public class VigenciaNotificationSyncService : IVigenciaNotificationSyncService } var daysUntil = (endDate - today).Days; + if (daysUntil > maxFutureDays) + { + idsToDelete.Add(notification.Id); + continue; + } if (notification.Tipo == "Vencido") { idsToDelete.Add(notification.Id); diff --git a/appsettings.Development.json b/appsettings.Development.json index d3bf1c2..ea2b463 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -25,6 +25,7 @@ "Notifications": { "CheckIntervalMinutes": 60, "NotifyAllFutureDates": true, + "MaxFutureDays": 30, "ReminderDays": [30, 15, 7] }, "Seed": { diff --git a/appsettings.json b/appsettings.json index 73f507e..bc7a8bb 100644 --- a/appsettings.json +++ b/appsettings.json @@ -25,6 +25,7 @@ "Notifications": { "CheckIntervalMinutes": 60, "NotifyAllFutureDates": true, + "MaxFutureDays": 30, "ReminderDays": [ 30, 15, 7 ] }, "Seed": {