Preserve vigencia notifications on reimport

This commit is contained in:
Eduardo Lopes 2026-01-27 17:13:24 -03:00
parent ae2145ac71
commit e73f181b33
2 changed files with 19 additions and 17 deletions

View File

@ -1176,15 +1176,6 @@ namespace line_gestao_api.Controllers
var startRow = headerRow.RowNumber() + 1;
var lastRow = ws.LastRowUsed()?.RowNumber() ?? startRow;
var tenantId = GetTenantIdFromClaims();
var notificationsQuery = _db.Notifications
.IgnoreQueryFilters()
.Where(n => n.VigenciaLineId != null);
if (tenantId.HasValue)
{
notificationsQuery = notificationsQuery.Where(n => n.TenantId == tenantId.Value);
}
await notificationsQuery.ExecuteDeleteAsync();
await _db.VigenciaLines.ExecuteDeleteAsync();
var buffer = new List<VigenciaLine>(600);

View File

@ -190,13 +190,26 @@ public class VigenciaNotificationBackgroundService : BackgroundService
return;
}
var dedupKeys = candidates.Select(c => c.DedupKey).Distinct().ToList();
var existingKeys = await db.Notifications.AsNoTracking()
.Where(n => dedupKeys.Contains(n.DedupKey))
.Select(n => n.DedupKey)
var candidateTipos = candidates.Select(c => c.Tipo).Distinct().ToList();
var candidateDates = candidates
.Where(c => c.ReferenciaData.HasValue)
.Select(c => c.ReferenciaData!.Value.Date)
.Distinct()
.ToList();
var existingNotifications = await db.Notifications.AsNoTracking()
.Where(n => n.TenantId == tenantId)
.Where(n => candidateTipos.Contains(n.Tipo))
.Where(n => n.ReferenciaData != null && candidateDates.Contains(n.ReferenciaData.Value.Date))
.ToListAsync(stoppingToken);
var existingSet = new HashSet<string>(existingKeys);
var existingSet = new HashSet<string>(existingNotifications.Select(n =>
BuildDedupKey(
n.Tipo,
n.ReferenciaData!.Value,
n.DiasParaVencer ?? 0,
n.Usuario,
n.Cliente,
n.Linha)));
var toInsert = candidates
.Where(c => !existingSet.Contains(c.DedupKey))
.ToList();
@ -232,7 +245,7 @@ public class VigenciaNotificationBackgroundService : BackgroundService
ReferenciaData = referenciaData,
DiasParaVencer = diasParaVencer,
Lida = false,
DedupKey = BuildDedupKey(tipo, vigenciaLineId, referenciaData, diasParaVencer, usuario, cliente, linha),
DedupKey = BuildDedupKey(tipo, referenciaData, diasParaVencer, usuario, cliente, linha),
UserId = userId,
Usuario = usuario,
Cliente = cliente,
@ -244,7 +257,6 @@ public class VigenciaNotificationBackgroundService : BackgroundService
private static string BuildDedupKey(
string tipo,
Guid vigenciaLineId,
DateTime referenciaData,
int diasParaVencer,
string? usuario,
@ -254,7 +266,6 @@ public class VigenciaNotificationBackgroundService : BackgroundService
var parts = new[]
{
tipo.Trim().ToLowerInvariant(),
vigenciaLineId.ToString(),
referenciaData.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
diasParaVencer.ToString(CultureInfo.InvariantCulture),
(usuario ?? string.Empty).Trim().ToLowerInvariant(),