Test Docker
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Threading.Tasks;
|
||||
using static Elecciones.Core.DTOs.BancaDto;
|
||||
|
||||
namespace Elecciones.Infrastructure.Services;
|
||||
|
||||
@@ -18,6 +19,8 @@ public class ElectoralApiService : IElectoralApiService
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
// --- MÉTODOS DE LA INTERFAZ ---
|
||||
|
||||
public async Task<string?> GetAuthTokenAsync()
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient("ElectoralApiClient");
|
||||
@@ -67,20 +70,84 @@ public class ElectoralApiService : IElectoralApiService
|
||||
public async Task<ResultadosDto?> GetResultadosAsync(string authToken, string distritoId, string seccionId, string municipioId)
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient("ElectoralApiClient");
|
||||
|
||||
// Construimos la URL con todos los parámetros requeridos. Usamos categoría 5 (Diputados) como ejemplo.
|
||||
var requestUri = $"/api/resultados/getResultados?distritold={distritoId}&seccionld={seccionId}&municipiold={municipioId}&categoriald=5";
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||
request.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
|
||||
var response = await client.SendAsync(request);
|
||||
return response.IsSuccessStatusCode
|
||||
? await response.Content.ReadFromJsonAsync<ResultadosDto>()
|
||||
: null;
|
||||
}
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
public async Task<RepartoBancasDto?> GetBancasAsync(string authToken, string distritoId, string seccionId)
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient("ElectoralApiClient");
|
||||
var requestUri = $"/api/resultados/getBancas?distritold={distritoId}&seccionld={seccionId}&categoriald=5";
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||
request.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
|
||||
return await response.Content.ReadFromJsonAsync<ResultadosDto>();
|
||||
var response = await client.SendAsync(request);
|
||||
return response.IsSuccessStatusCode
|
||||
? await response.Content.ReadFromJsonAsync<RepartoBancasDto>()
|
||||
: null;
|
||||
}
|
||||
|
||||
public async Task<List<string[]>?> GetTelegramasTotalizadosAsync(string authToken, string distritoId, string seccionId)
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient("ElectoralApiClient");
|
||||
var requestUri = $"/api/resultados/getTelegramasTotalizados?distritold={distritoId}&seccionld={seccionId}";
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||
request.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
|
||||
var response = await client.SendAsync(request);
|
||||
return response.IsSuccessStatusCode
|
||||
? await response.Content.ReadFromJsonAsync<List<string[]>>()
|
||||
: null;
|
||||
}
|
||||
|
||||
public async Task<TelegramaFileDto?> GetTelegramaFileAsync(string authToken, string mesaId)
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient("ElectoralApiClient");
|
||||
var requestUri = $"/api/resultados/getFile?mesald={mesaId}";
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||
request.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
|
||||
var response = await client.SendAsync(request);
|
||||
return response.IsSuccessStatusCode
|
||||
? await response.Content.ReadFromJsonAsync<TelegramaFileDto>()
|
||||
: null;
|
||||
}
|
||||
|
||||
public async Task<ResumenDto?> GetResumenAsync(string authToken, string distritoId)
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient("ElectoralApiClient");
|
||||
var requestUri = $"/api/resultados/getResumen?distritold={distritoId}";
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||
request.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
var response = await client.SendAsync(request);
|
||||
return response.IsSuccessStatusCode ? await response.Content.ReadFromJsonAsync<ResumenDto>() : null;
|
||||
}
|
||||
|
||||
public async Task<EstadoRecuentoGeneralDto?> GetEstadoRecuentoGeneralAsync(string authToken, string distritoId)
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient("ElectoralApiClient");
|
||||
var requestUri = $"/api/estados/estadoRecuento?distritold={distritoId}";
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||
request.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
var response = await client.SendAsync(request);
|
||||
return response.IsSuccessStatusCode ? await response.Content.ReadFromJsonAsync<EstadoRecuentoGeneralDto>() : null;
|
||||
}
|
||||
|
||||
public async Task<List<CategoriaDto>?> GetCategoriasAsync(string authToken)
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient("ElectoralApiClient");
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "/api/catalogo/getCategorias");
|
||||
request.Headers.Add("Authorization", $"Bearer {authToken}");
|
||||
var response = await client.SendAsync(request);
|
||||
return response.IsSuccessStatusCode
|
||||
? await response.Content.ReadFromJsonAsync<List<CategoriaDto>>()
|
||||
: null;
|
||||
}
|
||||
}
|
||||
@@ -1,101 +1,145 @@
|
||||
// src/Elecciones.Infrastructure/Services/FakeElectoralApiService.cs
|
||||
using Elecciones.Core.DTOs;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using static Elecciones.Core.DTOs.BancaDto;
|
||||
|
||||
namespace Elecciones.Infrastructure.Services;
|
||||
|
||||
public class FakeElectoralApiService : IElectoralApiService
|
||||
{
|
||||
private readonly ILogger<FakeElectoralApiService> _logger;
|
||||
private List<AmbitoDto>? _fakeAmbitosCache = null;
|
||||
private readonly Random _random = new Random();
|
||||
|
||||
public FakeElectoralApiService(ILogger<FakeElectoralApiService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private void GenerateFakeAmbitosFromGeoJson()
|
||||
{
|
||||
if (_fakeAmbitosCache != null) return;
|
||||
_logger.LogWarning("--- USANDO SERVICIO FALSO (FAKE) ---");
|
||||
_logger.LogInformation("Generando datos de prueba de ámbitos desde el archivo GeoJSON...");
|
||||
|
||||
var geoJsonPath = Path.Combine(AppContext.BaseDirectory, "buenos-aires-municipios.geojson");
|
||||
if (!File.Exists(geoJsonPath))
|
||||
{
|
||||
_logger.LogError("No se encontró el archivo buenos-aires-municipios.geojson.");
|
||||
_fakeAmbitosCache = new List<AmbitoDto>();
|
||||
return;
|
||||
}
|
||||
|
||||
var geoJsonString = File.ReadAllText(geoJsonPath);
|
||||
using var document = JsonDocument.Parse(geoJsonString);
|
||||
var features = document.RootElement.GetProperty("features").EnumerateArray().ToList();
|
||||
|
||||
var ambitos = new List<AmbitoDto>();
|
||||
ambitos.Add(new AmbitoDto { NivelId = 10, Nombre = "BUENOS AIRES", CodigoAmbitos = new CodigoAmbitoDto { DistritoId = "02" } });
|
||||
var secciones = new List<AmbitoDto> {
|
||||
new() { NivelId = 4, Nombre = "PRIMERA SECCION ELECTORAL", CodigoAmbitos = new CodigoAmbitoDto { DistritoId = "02", SeccionId = "0001" } },
|
||||
new() { NivelId = 4, Nombre = "SEGUNDA SECCION ELECTORAL", CodigoAmbitos = new CodigoAmbitoDto { DistritoId = "02", SeccionId = "0002" } },
|
||||
new() { NivelId = 4, Nombre = "TERCERA SECCION ELECTORAL", CodigoAmbitos = new CodigoAmbitoDto { DistritoId = "02", SeccionId = "0003" } }
|
||||
};
|
||||
ambitos.AddRange(secciones);
|
||||
|
||||
for (int i = 0; i < features.Count; i++)
|
||||
{
|
||||
var feature = features[i];
|
||||
var properties = feature.GetProperty("properties");
|
||||
var seccionAsignada = secciones[i % secciones.Count];
|
||||
ambitos.Add(new AmbitoDto { NivelId = 5, Nombre = properties.GetProperty("nam").GetString() ?? "Sin Nombre", CodigoAmbitos = new CodigoAmbitoDto { MunicipioId = properties.GetProperty("cca").GetString(), DistritoId = "02", SeccionId = seccionAsignada.CodigoAmbitos.SeccionId } });
|
||||
}
|
||||
_fakeAmbitosCache = ambitos;
|
||||
_logger.LogInformation("Se generaron {count} ámbitos de prueba (Provincia, Secciones y Municipios).", _fakeAmbitosCache.Count);
|
||||
}
|
||||
|
||||
public Task<ResumenDto?> GetResumenAsync(string authToken, string distritoId)
|
||||
{
|
||||
_logger.LogInformation("Simulando obtención de Resumen para distrito {DistritoId}...", distritoId);
|
||||
var resumen = new ResumenDto
|
||||
{
|
||||
ValoresTotalizadosPositivos = new List<ResumenPositivoDto>
|
||||
{
|
||||
new() { IdAgrupacion = "025", Votos = 2500000 + _random.Next(1000), VotosPorcentaje = 45.12m },
|
||||
new() { IdAgrupacion = "018", Votos = 2100000 + _random.Next(1000), VotosPorcentaje = 38.78m },
|
||||
new() { IdAgrupacion = "031", Votos = 800000 + _random.Next(1000), VotosPorcentaje = 14.10m }
|
||||
}
|
||||
};
|
||||
return Task.FromResult<ResumenDto?>(resumen);
|
||||
}
|
||||
|
||||
public Task<EstadoRecuentoGeneralDto?> GetEstadoRecuentoGeneralAsync(string authToken, string distritoId)
|
||||
{
|
||||
_logger.LogInformation("Simulando obtención de Estado de Recuento General para distrito {DistritoId}...", distritoId);
|
||||
var estado = new EstadoRecuentoGeneralDto
|
||||
{
|
||||
MesasEsperadas = 38000,
|
||||
MesasTotalizadas = _random.Next(28000, 37000),
|
||||
MesasTotalizadasPorcentaje = 95.5m,
|
||||
CantidadElectores = 12500000,
|
||||
CantidadVotantes = 9375000,
|
||||
ParticipacionPorcentaje = 75.0m
|
||||
};
|
||||
return Task.FromResult<EstadoRecuentoGeneralDto?>(estado);
|
||||
}
|
||||
|
||||
public Task<string?> GetAuthTokenAsync()
|
||||
{
|
||||
_logger.LogWarning("--- USANDO SERVICIO FALSO (FAKE) ---");
|
||||
_logger.LogInformation("Simulando obtención de token...");
|
||||
string fakeToken = "FAKE_TOKEN_FOR_DEVELOPMENT";
|
||||
return Task.FromResult<string?>(fakeToken);
|
||||
return Task.FromResult<string?>("FAKE_TOKEN_FOR_DEVELOPMENT");
|
||||
}
|
||||
|
||||
public Task<List<CatalogoDto>?> GetCatalogoCompletoAsync(string authToken)
|
||||
{
|
||||
_logger.LogInformation("Simulando obtención de Catálogo Completo...");
|
||||
|
||||
var catalogo = new List<CatalogoDto>
|
||||
{
|
||||
new() // Simulamos el catálogo para la categoría de Diputados (ID 5)
|
||||
{
|
||||
Version = 1,
|
||||
CategoriaId = 5,
|
||||
Ambitos =
|
||||
[
|
||||
new() { NivelId = 10, Nombre = "BUENOS AIRES", CodigoAmbitos = new() { DistritoId = "02" } },
|
||||
new() { NivelId = 5, Nombre = "LA PLATA", CodigoAmbitos = new() { DistritoId = "02", SeccionId = "0001", MunicipioId = "056" } },
|
||||
new() { NivelId = 5, Nombre = "MAR DEL PLATA", CodigoAmbitos = new() { DistritoId = "02", SeccionId = "0005", MunicipioId = "035" } }
|
||||
],
|
||||
Niveles =
|
||||
[
|
||||
new() { NivelId = 10, Nombre = "Provincia" },
|
||||
new() { NivelId = 5, Nombre = "Municipio" }
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
GenerateFakeAmbitosFromGeoJson();
|
||||
var catalogo = new List<CatalogoDto> { new() { Version = 1, CategoriaId = 5, Ambitos = _fakeAmbitosCache ?? new List<AmbitoDto>(), Niveles = new List<NivelDto> { new() { NivelId = 10, Nombre = "Provincia" }, new() { NivelId = 4, Nombre = "Seccion" }, new() { NivelId = 5, Nombre = "Municipio" } } } };
|
||||
return Task.FromResult<List<CatalogoDto>?>(catalogo);
|
||||
}
|
||||
|
||||
public Task<List<AgrupacionDto>?> GetAgrupacionesAsync(string authToken, string distritoId, int categoriaId)
|
||||
{
|
||||
_logger.LogInformation("Simulando obtención de Agrupaciones Políticas para el distrito {Distrito} y categoría {Categoria}...", distritoId, categoriaId);
|
||||
|
||||
var agrupaciones = new List<AgrupacionDto>
|
||||
{
|
||||
new() { IdAgrupacion = "018", IdAgrupacionTelegrama = "131", NombreAgrupacion = "FRENTE DE AVANZADA" },
|
||||
new() { IdAgrupacion = "025", IdAgrupacionTelegrama = "132", NombreAgrupacion = "ALIANZA POR EL FUTURO" },
|
||||
new() { IdAgrupacion = "031", IdAgrupacionTelegrama = "133", NombreAgrupacion = "UNION POPULAR" },
|
||||
new() { IdAgrupacion = "045", IdAgrupacionTelegrama = "134", NombreAgrupacion = "PARTIDO VECINALISTA" }
|
||||
};
|
||||
|
||||
var agrupaciones = new List<AgrupacionDto> { new() { IdAgrupacion = "018", IdAgrupacionTelegrama = "131", NombreAgrupacion = "FRENTE DE AVANZADA" }, new() { IdAgrupacion = "025", IdAgrupacionTelegrama = "132", NombreAgrupacion = "ALIANZA POR EL FUTURO" }, new() { IdAgrupacion = "031", IdAgrupacionTelegrama = "133", NombreAgrupacion = "UNION POPULAR" }, new() { IdAgrupacion = "045", IdAgrupacionTelegrama = "134", NombreAgrupacion = "PARTIDO VECINALISTA" } };
|
||||
return Task.FromResult<List<AgrupacionDto>?>(agrupaciones);
|
||||
}
|
||||
|
||||
public Task<ResultadosDto?> GetResultadosAsync(string authToken, string distritoId, string seccionId, string municipioId)
|
||||
{
|
||||
_logger.LogInformation("Simulando obtención de Resultados para el municipio {MunicipioId}...", municipioId);
|
||||
|
||||
// YA NO FILTRAMOS POR ID. DEVOLVEMOS DATOS SIMULADOS PARA CUALQUIER MUNICIPIO.
|
||||
|
||||
var random = new Random();
|
||||
var resultados = new ResultadosDto
|
||||
{
|
||||
FechaTotalizacion = DateTime.Now.ToString("o"),
|
||||
EstadoRecuento = new EstadoRecuentoDto
|
||||
{
|
||||
MesasEsperadas = random.Next(100, 2000), // Hacemos que varíe
|
||||
MesasTotalizadas = random.Next(50, 100),
|
||||
CantidadElectores = random.Next(50000, 600000),
|
||||
ParticipacionPorcentaje = random.Next(60, 85) + (decimal)random.NextDouble()
|
||||
},
|
||||
ValoresTotalizadosPositivos =
|
||||
[
|
||||
// Usamos los IDs reales de nuestro catálogo de agrupaciones falsas
|
||||
new() { IdAgrupacion = "018", NombreAgrupacion = "FRENTE DE AVANZADA", Votos = random.Next(10000, 20000) },
|
||||
new() { IdAgrupacion = "025", NombreAgrupacion = "ALIANZA POR EL FUTURO", Votos = random.Next(15000, 25000) },
|
||||
new() { IdAgrupacion = "031", NombreAgrupacion = "UNION POPULAR", Votos = random.Next(5000, 10000) },
|
||||
new() { IdAgrupacion = "045", NombreAgrupacion = "PARTIDO VECINALISTA", Votos = random.Next(2000, 5000) }
|
||||
],
|
||||
ValoresTotalizadosOtros = new VotosOtrosDto
|
||||
{
|
||||
VotosEnBlanco = random.Next(1000, 2000),
|
||||
VotosNulos = random.Next(500, 1000),
|
||||
VotosRecurridos = random.Next(20, 50)
|
||||
}
|
||||
};
|
||||
|
||||
var resultados = new ResultadosDto { FechaTotalizacion = DateTime.Now.ToString("o"), EstadoRecuento = new EstadoRecuentoDto { MesasEsperadas = _random.Next(100, 2000), MesasTotalizadas = _random.Next(50, 100), CantidadElectores = _random.Next(50000, 600000), ParticipacionPorcentaje = _random.Next(60, 85) + (decimal)_random.NextDouble() }, ValoresTotalizadosPositivos = new List<VotosPositivosDto> { new() { IdAgrupacion = "018", Votos = _random.Next(10000, 20000) }, new() { IdAgrupacion = "025", Votos = _random.Next(15000, 25000) }, new() { IdAgrupacion = "031", Votos = _random.Next(5000, 10000) }, new() { IdAgrupacion = "045", Votos = _random.Next(2000, 5000) } }, ValoresTotalizadosOtros = new VotosOtrosDto { VotosEnBlanco = _random.Next(1000, 2000), VotosNulos = _random.Next(500, 1000), VotosRecurridos = _random.Next(20, 50) } };
|
||||
return Task.FromResult<ResultadosDto?>(resultados);
|
||||
}
|
||||
|
||||
public Task<RepartoBancasDto?> GetBancasAsync(string authToken, string distritoId, string seccionId)
|
||||
{
|
||||
var reparto = new RepartoBancasDto { RepartoBancas = new List<BancaDto> { new() { IdAgrupacion = "025", NroBancas = _random.Next(5, 9) }, new() { IdAgrupacion = "018", NroBancas = _random.Next(3, 7) } } };
|
||||
return Task.FromResult<RepartoBancasDto?>(reparto);
|
||||
}
|
||||
|
||||
public Task<List<string[]>?> GetTelegramasTotalizadosAsync(string authToken, string distritoId, string seccionId)
|
||||
{
|
||||
var lista = new List<string[]> { new[] { $"02{seccionId}0001M" }, new[] { $"02{seccionId}0002M" } };
|
||||
return Task.FromResult<List<string[]>?>(lista);
|
||||
}
|
||||
|
||||
public Task<TelegramaFileDto?> GetTelegramaFileAsync(string authToken, string mesaId)
|
||||
{
|
||||
var file = new TelegramaFileDto { NombreArchivo = mesaId, Imagen = "FAKE_BASE64_PDF_CONTENT", FechaEscaneo = DateTime.UtcNow.AddMinutes(-10).ToString("o"), FechaTotalizacion = DateTime.UtcNow.ToString("o") };
|
||||
return Task.FromResult<TelegramaFileDto?>(file);
|
||||
}
|
||||
|
||||
public Task<List<CategoriaDto>?> GetCategoriasAsync(string authToken)
|
||||
{
|
||||
_logger.LogInformation("Simulando obtención de Categorías Electorales...");
|
||||
var categorias = new List<CategoriaDto>
|
||||
{
|
||||
new() { CategoriaId = 5, Nombre = "DIPUTADOS NACIONALES", Orden = 1 },
|
||||
new() { CategoriaId = 6, Nombre = "SENADORES NACIONALES", Orden = 2 }
|
||||
};
|
||||
return Task.FromResult<List<CategoriaDto>?>(categorias);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,24 @@
|
||||
// src/Elecciones.Infrastructure/Services/IElectoralApiService.cs
|
||||
using Elecciones.Core.DTOs;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using static Elecciones.Core.DTOs.BancaDto;
|
||||
|
||||
namespace Elecciones.Infrastructure.Services;
|
||||
|
||||
public interface IElectoralApiService
|
||||
{
|
||||
Task<string?> GetAuthTokenAsync();
|
||||
|
||||
// Métodos para catálogos
|
||||
Task<List<CatalogoDto>?> GetCatalogoCompletoAsync(string authToken);
|
||||
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<RepartoBancasDto?> GetBancasAsync(string authToken, string distritoId, string seccionId);
|
||||
Task<List<string[]>?> GetTelegramasTotalizadosAsync(string authToken, string distritoId, string seccionId);
|
||||
Task<TelegramaFileDto?> GetTelegramaFileAsync(string authToken, string mesaId);
|
||||
Task<ResumenDto?> GetResumenAsync(string authToken, string distritoId);
|
||||
Task<EstadoRecuentoGeneralDto?> GetEstadoRecuentoGeneralAsync(string authToken, string distritoId);
|
||||
Task<List<CategoriaDto>?> GetCategoriasAsync(string authToken);
|
||||
}
|
||||
Reference in New Issue
Block a user