78 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			78 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| // reconcile_ids.js
 | |
| const fs = require('fs');
 | |
| const path = require('path');
 | |
| const http = require('http');
 | |
| 
 | |
| // --- CONFIGURACIÓN ---
 | |
| const API_URL = 'http://localhost:5217/api/catalogos/municipios'; // ¡Asegúrate que tu API esté corriendo en este puerto!
 | |
| const GEOJSON_PATH = path.join(__dirname, 'Elecciones-Web', 'frontend', 'public', 'buenos-aires-municipios.geojson');
 | |
| const OUTPUT_PATH = path.join(__dirname, 'Elecciones-Web', 'frontend', 'public', 'municipioIdMap.json');
 | |
| 
 | |
| const normalizeString = (str) => {
 | |
|     if (!str) return '';
 | |
|     return str.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
 | |
| };
 | |
| 
 | |
| async function reconcile() {
 | |
|     console.log('Iniciando el script de reconciliación de IDs...');
 | |
| 
 | |
|     try {
 | |
|         const apiData = await new Promise((resolve, reject) => {
 | |
|             http.get(API_URL, res => {
 | |
|                 const { statusCode } = res;
 | |
|                 if (statusCode !== 200) {
 | |
|                     reject(new Error(`La petición a la API falló con el código de estado: ${statusCode}`));
 | |
|                     res.resume();
 | |
|                     return;
 | |
|                 }
 | |
|                 let data = '';
 | |
|                 res.on('data', chunk => data += chunk);
 | |
|                 res.on('end', () => resolve(JSON.parse(data)));
 | |
|             }).on('error', reject);
 | |
|         });
 | |
|         console.log(`Se obtuvieron ${apiData.length} municipios desde la API.`);
 | |
| 
 | |
|         const apiNameMap = new Map(apiData.map(m => [normalizeString(m.nombre), m.id]));
 | |
| 
 | |
|         const geoJsonRaw = fs.readFileSync(GEOJSON_PATH, 'utf8');
 | |
|         const geoJsonData = JSON.parse(geoJsonRaw);
 | |
|         console.log(`Se leyeron ${geoJsonData.features.length} features desde el GeoJSON.`);
 | |
| 
 | |
|         const finalIdMap = {};
 | |
|         let matches = 0;
 | |
|         const notFound = [];
 | |
| 
 | |
|         // --- CAMBIO CLAVE: YA NO NECESITAMOS ALIAS MANUALES ---
 | |
| 
 | |
|         for (const feature of geoJsonData.features) {
 | |
|             const geoJsonId = feature.properties.cca;
 | |
|             const nombreGeoJson = normalizeString(feature.properties.nam);
 | |
| 
 | |
|             if (apiNameMap.has(nombreGeoJson)) {
 | |
|                 const apiId = apiNameMap.get(nombreGeoJson);
 | |
|                 finalIdMap[geoJsonId] = apiId;
 | |
|                 matches++;
 | |
|             } else {
 | |
|                 notFound.push(feature.properties.nam);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         console.log(`\n--- RESULTADOS ---`);
 | |
|         console.log(`Coincidencias encontradas: ${matches} de ${geoJsonData.features.length}`);
 | |
|         if (notFound.length > 0) {
 | |
|             console.warn(`\nNo se encontró coincidencia para los siguientes ${notFound.length} municipios del GeoJSON:`);
 | |
|             console.warn(notFound.join(', '));
 | |
|         }
 | |
| 
 | |
|         fs.writeFileSync(OUTPUT_PATH, JSON.stringify(finalIdMap, null, 2));
 | |
|         console.log(`\n¡Éxito! El mapa de IDs se ha guardado en: ${OUTPUT_PATH}`);
 | |
| 
 | |
|     } catch (error) {
 | |
|         console.error('\nOcurrió un error:', error.message);
 | |
|         if(error.code === 'ECONNREFUSED') {
 | |
|             console.error('Asegúrate de que tu servicio de API esté corriendo en la URL especificada.');
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| reconcile(); |