/** * Exporta todos los XMLs existentes de ROEM691011EZ4 al filesystem */ import { config } from 'dotenv'; import { resolve } from 'path'; config({ path: resolve(process.cwd(), '.env') }); import { Pool } from 'pg'; import { mkdirSync, writeFileSync } from 'fs'; import { join } from 'path'; const BASE_PATH = '/var/horux/xml/ROEM691011EZ4'; const DB_URL = process.env.DATABASE_URL!.replace(/\/[^/?]+(\?.*)?$/, '/horux_roem691011ez4$1'); async function main() { const pool = new Pool({ connectionString: DB_URL }); const { rows } = await pool.query( `SELECT uuid_fiscal, fecha_emision, xml_original FROM cfdis WHERE xml_original IS NOT NULL` ); console.log(`Exportando ${rows.length} XMLs...`); let count = 0; for (const row of rows) { const fecha = new Date(row.fecha_emision); const year = fecha.getFullYear().toString(); const month = (fecha.getMonth() + 1).toString().padStart(2, '0'); const dir = join(BASE_PATH, year, month); mkdirSync(dir, { recursive: true }); writeFileSync(join(dir, `${row.uuid_fiscal}.xml`), row.xml_original, 'utf-8'); count++; } console.log(`${count} XMLs exportados a ${BASE_PATH}`); await pool.end(); } main().catch(console.error);