Fix 1200
This commit is contained in:
		| @@ -40,7 +40,7 @@ public class ElectoralApiService : IElectoralApiService | |||||||
|             : null; |             : null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public async Task<List<CatalogoDto>?> GetCatalogoAmbitosAsync(string authToken, int categoriaId) |     public async Task<CatalogoDto?> GetCatalogoAmbitosAsync(string authToken, int categoriaId) | ||||||
|     { |     { | ||||||
|         var client = _httpClientFactory.CreateClient("ElectoralApiClient"); |         var client = _httpClientFactory.CreateClient("ElectoralApiClient"); | ||||||
|         var request = new HttpRequestMessage(HttpMethod.Get, $"/api/catalogo/getCatalogo?categoriaId={categoriaId}"); |         var request = new HttpRequestMessage(HttpMethod.Get, $"/api/catalogo/getCatalogo?categoriaId={categoriaId}"); | ||||||
| @@ -48,7 +48,7 @@ public class ElectoralApiService : IElectoralApiService | |||||||
|  |  | ||||||
|         var response = await client.SendAsync(request); |         var response = await client.SendAsync(request); | ||||||
|         return response.IsSuccessStatusCode |         return response.IsSuccessStatusCode | ||||||
|             ? await response.Content.ReadFromJsonAsync<List<CatalogoDto>>() |             ? await response.Content.ReadFromJsonAsync<CatalogoDto>() | ||||||
|             : null; |             : null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -10,10 +10,8 @@ public interface IElectoralApiService | |||||||
|     Task<string?> GetAuthTokenAsync(); |     Task<string?> GetAuthTokenAsync(); | ||||||
|  |  | ||||||
|     // Métodos para catálogos |     // Métodos para catálogos | ||||||
|     Task<List<CatalogoDto>?> GetCatalogoAmbitosAsync(string authToken, int categoriaId); |     Task<CatalogoDto?> GetCatalogoAmbitosAsync(string authToken, int categoriaId); | ||||||
|     Task<List<AgrupacionDto>?> GetAgrupacionesAsync(string authToken, string distritoId, int categoriaId); |     Task<List<AgrupacionDto>?> GetAgrupacionesAsync(string authToken, string distritoId, int categoriaId); | ||||||
|  |  | ||||||
|     // Métodos para resultados y datos dinámicos |  | ||||||
|     Task<ResultadosDto?> GetResultadosAsync(string authToken, string distritoId, string seccionId, string municipioId); |     Task<ResultadosDto?> GetResultadosAsync(string authToken, string distritoId, string seccionId, string municipioId); | ||||||
|     Task<RepartoBancasDto?> GetBancasAsync(string authToken, string distritoId, string seccionId); |     Task<RepartoBancasDto?> GetBancasAsync(string authToken, string distritoId, string seccionId); | ||||||
|     Task<List<string[]>?> GetTelegramasTotalizadosAsync(string authToken, string distritoId, string seccionId); |     Task<List<string[]>?> GetTelegramasTotalizadosAsync(string authToken, string distritoId, string seccionId); | ||||||
|   | |||||||
| @@ -170,11 +170,11 @@ public class Worker : BackgroundService | |||||||
|             using var scope = _serviceProvider.CreateScope(); |             using var scope = _serviceProvider.CreateScope(); | ||||||
|             var dbContext = scope.ServiceProvider.GetRequiredService<EleccionesDbContext>(); |             var dbContext = scope.ServiceProvider.GetRequiredService<EleccionesDbContext>(); | ||||||
|  |  | ||||||
|             // --- 1. OBTENEMOS LA LISTA COMPLETA DE CATEGORÍAS DISPONIBLES --- |             // --- 1. SINCRONIZAR CATEGORÍAS --- | ||||||
|             var categoriasApi = await _apiService.GetCategoriasAsync(_authToken); |             var categoriasApi = await _apiService.GetCategoriasAsync(_authToken); | ||||||
|             if (categoriasApi is null || !categoriasApi.Any()) |             if (categoriasApi is null || !categoriasApi.Any()) | ||||||
|             { |             { | ||||||
|                 _logger.LogWarning("No se recibieron datos del catálogo de Categorías. No se puede continuar."); |                 _logger.LogWarning("No se recibieron datos del catálogo de Categorías. No se puede continuar con la sincronización."); | ||||||
|                 return; |                 return; | ||||||
|             } |             } | ||||||
|  |  | ||||||
| @@ -186,17 +186,18 @@ public class Worker : BackgroundService | |||||||
|  |  | ||||||
|             _logger.LogInformation("Se procesarán {count} categorías electorales.", distinctCategorias.Count); |             _logger.LogInformation("Se procesarán {count} categorías electorales.", distinctCategorias.Count); | ||||||
|  |  | ||||||
|             // --- 2. GUARDAMOS/ACTUALIZAMOS LAS CATEGORÍAS EN LA BD --- |  | ||||||
|             var categoriasEnDb = await dbContext.CategoriasElectorales.ToDictionaryAsync(c => c.Id, c => c, stoppingToken); |             var categoriasEnDb = await dbContext.CategoriasElectorales.ToDictionaryAsync(c => c.Id, c => c, stoppingToken); | ||||||
|             foreach (var categoriaDto in distinctCategorias) |             foreach (var categoriaDto in distinctCategorias) | ||||||
|             { |             { | ||||||
|                 if (categoriasEnDb.TryGetValue(categoriaDto.CategoriaId, out var categoriaExistente)) |                 if (categoriasEnDb.TryGetValue(categoriaDto.CategoriaId, out var categoriaExistente)) | ||||||
|                 { |                 { | ||||||
|  |                     // Actualizar por si cambia el nombre o el orden | ||||||
|                     categoriaExistente.Nombre = categoriaDto.Nombre; |                     categoriaExistente.Nombre = categoriaDto.Nombre; | ||||||
|                     categoriaExistente.Orden = categoriaDto.Orden; |                     categoriaExistente.Orden = categoriaDto.Orden; | ||||||
|                 } |                 } | ||||||
|                 else |                 else | ||||||
|                 { |                 { | ||||||
|  |                     // Añadir nueva categoría | ||||||
|                     dbContext.CategoriasElectorales.Add(new CategoriaElectoral |                     dbContext.CategoriasElectorales.Add(new CategoriaElectoral | ||||||
|                     { |                     { | ||||||
|                         Id = categoriaDto.CategoriaId, |                         Id = categoriaDto.CategoriaId, | ||||||
| @@ -206,26 +207,81 @@ public class Worker : BackgroundService | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             // --- 3. ITERAMOS SOBRE CADA CATEGORÍA PARA DESCARGAR EL RESTO DE CATÁLOGOS --- |             // --- 2. ITERAR PARA SINCRONIZAR ÁMBITOS Y AGRUPACIONES --- | ||||||
|             foreach (var categoria in distinctCategorias) |             foreach (var categoria in distinctCategorias) | ||||||
|             { |             { | ||||||
|                 if (stoppingToken.IsCancellationRequested) break; |                 if (stoppingToken.IsCancellationRequested) break; | ||||||
|                 _logger.LogInformation("--- Sincronizando Ámbitos y Agrupaciones para la categoría: {Nombre} (ID: {Id}) ---", categoria.Nombre, categoria.CategoriaId); |                 _logger.LogInformation("--- Sincronizando Ámbitos y Agrupaciones para la categoría: {Nombre} (ID: {Id}) ---", categoria.Nombre, categoria.CategoriaId); | ||||||
|  |  | ||||||
|                 var catalogoAmbitosApi = await _apiService.GetCatalogoAmbitosAsync(_authToken, categoria.CategoriaId); |                 var catalogoDto = await _apiService.GetCatalogoAmbitosAsync(_authToken, categoria.CategoriaId); | ||||||
|                 if (catalogoAmbitosApi != null && catalogoAmbitosApi.Any()) |                 if (catalogoDto != null) | ||||||
|                 { |                 { | ||||||
|                     // La lógica para procesar ámbitos y agrupaciones irá aquí... |                     // SINCRONIZAR ÁMBITOS | ||||||
|                     // Por ahora, se saltará debido a los errores 400 de la API. |                     // Usamos una clave compuesta para identificar unívocamente cada ámbito geográfico | ||||||
|                     _logger.LogInformation("Catálogo de Ámbitos para la categoría {Id} obtenido con éxito.", categoria.CategoriaId); |                     var ambitosEnDb = await dbContext.AmbitosGeograficos.ToDictionaryAsync(a => (a.DistritoId, a.SeccionId, a.MunicipioId), a => a, stoppingToken); | ||||||
|  |                     foreach (var ambitoDto in catalogoDto.Ambitos) | ||||||
|  |                     { | ||||||
|  |                         var claveUnica = (ambitoDto.CodigoAmbitos.DistritoId, ambitoDto.CodigoAmbitos.SeccionId, ambitoDto.CodigoAmbitos.MunicipioId); | ||||||
|  |                         if (ambitosEnDb.TryGetValue(claveUnica, out var ambitoExistente)) | ||||||
|  |                         { | ||||||
|  |                             // Actualizar datos descriptivos | ||||||
|  |                             ambitoExistente.Nombre = ambitoDto.Nombre; | ||||||
|  |                             ambitoExistente.NivelId = ambitoDto.NivelId; | ||||||
|  |                         } | ||||||
|  |                         else | ||||||
|  |                         { | ||||||
|  |                             // Añadir nuevo ámbito | ||||||
|  |                             dbContext.AmbitosGeograficos.Add(new AmbitoGeografico | ||||||
|  |                             { | ||||||
|  |                                 Nombre = ambitoDto.Nombre, | ||||||
|  |                                 NivelId = ambitoDto.NivelId, | ||||||
|  |                                 DistritoId = ambitoDto.CodigoAmbitos.DistritoId, | ||||||
|  |                                 SeccionId = ambitoDto.CodigoAmbitos.SeccionId, | ||||||
|  |                                 MunicipioId = ambitoDto.CodigoAmbitos.MunicipioId, | ||||||
|  |                                 SeccionProvincialId = ambitoDto.CodigoAmbitos.SeccionProvincialId | ||||||
|  |                             }); | ||||||
|  |                         } | ||||||
|  |                     } | ||||||
|  |                     _logger.LogInformation("Sincronización de Ámbitos Geográficos completada para la categoría actual."); | ||||||
|  |  | ||||||
|  |                     // SINCRONIZAR AGRUPACIONES | ||||||
|  |                     var provincia = catalogoDto.Ambitos.FirstOrDefault(a => a.NivelId == 10); | ||||||
|  |                     if (provincia != null && provincia.CodigoAmbitos.DistritoId != null) | ||||||
|  |                     { | ||||||
|  |                         var agrupacionesApi = await _apiService.GetAgrupacionesAsync(_authToken, provincia.CodigoAmbitos.DistritoId, categoria.CategoriaId); | ||||||
|  |                         if (agrupacionesApi != null && agrupacionesApi.Any()) | ||||||
|  |                         { | ||||||
|  |                             var agrupacionesEnDb = await dbContext.AgrupacionesPoliticas.ToDictionaryAsync(a => a.Id, a => a, stoppingToken); | ||||||
|  |                             foreach (var agrupacionDto in agrupacionesApi) | ||||||
|  |                             { | ||||||
|  |                                 if (agrupacionesEnDb.TryGetValue(agrupacionDto.IdAgrupacion, out var agrupacionExistente)) | ||||||
|  |                                 { | ||||||
|  |                                     // Actualizar datos descriptivos | ||||||
|  |                                     agrupacionExistente.Nombre = agrupacionDto.NombreAgrupacion; | ||||||
|  |                                     agrupacionExistente.IdTelegrama = agrupacionDto.IdAgrupacionTelegrama; | ||||||
|  |                                 } | ||||||
|  |                                 else | ||||||
|  |                                 { | ||||||
|  |                                     // Añadir nueva agrupación | ||||||
|  |                                     dbContext.AgrupacionesPoliticas.Add(new AgrupacionPolitica | ||||||
|  |                                     { | ||||||
|  |                                         Id = agrupacionDto.IdAgrupacion, | ||||||
|  |                                         IdTelegrama = agrupacionDto.IdAgrupacionTelegrama, | ||||||
|  |                                         Nombre = agrupacionDto.NombreAgrupacion | ||||||
|  |                                     }); | ||||||
|  |                                 } | ||||||
|  |                             } | ||||||
|  |                             _logger.LogInformation("Sincronización de Agrupaciones Políticas completada para la categoría actual."); | ||||||
|  |                         } | ||||||
|  |                     } | ||||||
|                 } |                 } | ||||||
|                 else |                 else | ||||||
|                 { |                 { | ||||||
|                     _logger.LogWarning("No se recibieron datos del catálogo de Ámbitos para la categoría {Id} (Respuesta: 400 Bad Request).", categoria.CategoriaId); |                     _logger.LogWarning("No se recibieron datos del catálogo de Ámbitos para la categoría {Id}.", categoria.CategoriaId); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             // --- 4. GUARDADO FINAL --- |             // --- 3. GUARDADO FINAL --- | ||||||
|             int cambiosGuardados = await dbContext.SaveChangesAsync(stoppingToken); |             int cambiosGuardados = await dbContext.SaveChangesAsync(stoppingToken); | ||||||
|             _logger.LogInformation("{count} cambios totales en los catálogos han sido guardados en la base de datos.", cambiosGuardados); |             _logger.LogInformation("{count} cambios totales en los catálogos han sido guardados en la base de datos.", cambiosGuardados); | ||||||
|             _logger.LogInformation("Sincronización de catálogos maestros finalizada."); |             _logger.LogInformation("Sincronización de catálogos maestros finalizada."); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user