Preserve vigencia notifications on reimport
This commit is contained in:
parent
ae2145ac71
commit
e73f181b33
|
|
@ -1176,15 +1176,6 @@ namespace line_gestao_api.Controllers
|
||||||
var startRow = headerRow.RowNumber() + 1;
|
var startRow = headerRow.RowNumber() + 1;
|
||||||
var lastRow = ws.LastRowUsed()?.RowNumber() ?? startRow;
|
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();
|
await _db.VigenciaLines.ExecuteDeleteAsync();
|
||||||
|
|
||||||
var buffer = new List<VigenciaLine>(600);
|
var buffer = new List<VigenciaLine>(600);
|
||||||
|
|
|
||||||
|
|
@ -190,13 +190,26 @@ public class VigenciaNotificationBackgroundService : BackgroundService
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var dedupKeys = candidates.Select(c => c.DedupKey).Distinct().ToList();
|
var candidateTipos = candidates.Select(c => c.Tipo).Distinct().ToList();
|
||||||
var existingKeys = await db.Notifications.AsNoTracking()
|
var candidateDates = candidates
|
||||||
.Where(n => dedupKeys.Contains(n.DedupKey))
|
.Where(c => c.ReferenciaData.HasValue)
|
||||||
.Select(n => n.DedupKey)
|
.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);
|
.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
|
var toInsert = candidates
|
||||||
.Where(c => !existingSet.Contains(c.DedupKey))
|
.Where(c => !existingSet.Contains(c.DedupKey))
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
@ -232,7 +245,7 @@ public class VigenciaNotificationBackgroundService : BackgroundService
|
||||||
ReferenciaData = referenciaData,
|
ReferenciaData = referenciaData,
|
||||||
DiasParaVencer = diasParaVencer,
|
DiasParaVencer = diasParaVencer,
|
||||||
Lida = false,
|
Lida = false,
|
||||||
DedupKey = BuildDedupKey(tipo, vigenciaLineId, referenciaData, diasParaVencer, usuario, cliente, linha),
|
DedupKey = BuildDedupKey(tipo, referenciaData, diasParaVencer, usuario, cliente, linha),
|
||||||
UserId = userId,
|
UserId = userId,
|
||||||
Usuario = usuario,
|
Usuario = usuario,
|
||||||
Cliente = cliente,
|
Cliente = cliente,
|
||||||
|
|
@ -244,7 +257,6 @@ public class VigenciaNotificationBackgroundService : BackgroundService
|
||||||
|
|
||||||
private static string BuildDedupKey(
|
private static string BuildDedupKey(
|
||||||
string tipo,
|
string tipo,
|
||||||
Guid vigenciaLineId,
|
|
||||||
DateTime referenciaData,
|
DateTime referenciaData,
|
||||||
int diasParaVencer,
|
int diasParaVencer,
|
||||||
string? usuario,
|
string? usuario,
|
||||||
|
|
@ -254,7 +266,6 @@ public class VigenciaNotificationBackgroundService : BackgroundService
|
||||||
var parts = new[]
|
var parts = new[]
|
||||||
{
|
{
|
||||||
tipo.Trim().ToLowerInvariant(),
|
tipo.Trim().ToLowerInvariant(),
|
||||||
vigenciaLineId.ToString(),
|
|
||||||
referenciaData.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
|
referenciaData.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
|
||||||
diasParaVencer.ToString(CultureInfo.InvariantCulture),
|
diasParaVencer.ToString(CultureInfo.InvariantCulture),
|
||||||
(usuario ?? string.Empty).Trim().ToLowerInvariant(),
|
(usuario ?? string.Empty).Trim().ToLowerInvariant(),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue