Fix Worker 1447
This commit is contained in:
@@ -225,14 +225,18 @@ public class Worker : BackgroundService
|
||||
using var scope = _serviceProvider.CreateScope();
|
||||
var dbContext = scope.ServiceProvider.GetRequiredService<EleccionesDbContext>();
|
||||
|
||||
// Buscamos los ámbitos de nivel Municipio/Partido
|
||||
var municipiosASondear = await dbContext.AmbitosGeograficos
|
||||
.AsNoTracking()
|
||||
.Where(a => a.NivelId == 5 && a.MunicipioId != null && a.DistritoId != null && a.SeccionId != null)
|
||||
.Select(a => new { a.Id, a.MunicipioId, a.SeccionId, a.DistritoId })
|
||||
// Añadimos a.Nombre aquí para que esté disponible en el bucle
|
||||
.Select(a => new { a.Id, a.Nombre, a.MunicipioId, a.SeccionId, a.DistritoId })
|
||||
.ToListAsync(stoppingToken);
|
||||
|
||||
if (!municipiosASondear.Any()) return;
|
||||
if (!municipiosASondear.Any())
|
||||
{
|
||||
_logger.LogWarning("No se encontraron Partidos (NivelId 5) en la BD para sondear resultados.");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Iniciando sondeo de resultados para {count} municipios (Partidos)...", municipiosASondear.Count);
|
||||
|
||||
@@ -246,19 +250,27 @@ public class Worker : BackgroundService
|
||||
|
||||
if (categoriaConcejales != null)
|
||||
{
|
||||
// Para obtener resultados del PARTIDO completo, pasamos 'municipioId' como null.
|
||||
// Usamos el 'seccionId' del registro, que según la aclaración, es el ID del Partido.
|
||||
var resultados = await _apiService.GetResultadosAsync(
|
||||
authToken,
|
||||
municipio.DistritoId!,
|
||||
municipio.SeccionId!,
|
||||
null, // <- AHORA ES NULL
|
||||
null,
|
||||
categoriaConcejales.Id
|
||||
);
|
||||
|
||||
if (resultados != null)
|
||||
{
|
||||
await GuardarResultadosDeAmbitoAsync(dbContext, municipio.Id, resultados, stoppingToken);
|
||||
try
|
||||
{
|
||||
await GuardarResultadosDeAmbitoAsync(dbContext, municipio.Id, resultados, stoppingToken);
|
||||
// Ahora 'municipio.Nombre' existe y el log funcionará
|
||||
_logger.LogInformation("Resultados para el municipio '{nombre}' (ID: {id}) guardados/actualizados.", municipio.Nombre, municipio.MunicipioId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Y aquí también funcionará, dándonos un error mucho más útil
|
||||
_logger.LogError(ex, "FALLO CRÍTICO al guardar resultados para el municipio '{nombre}' (ID: {id}).", municipio.Nombre, municipio.MunicipioId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user