import 'dotenv/config'; import { tenantDb } from '../src/config/database.js'; import { getDecryptedFielContribuyente } from '../src/services/contribuyente-fiel.service.js'; import { createSatService, querySat } from '../src/services/sat/sat-client.service.js'; // Discrimina la causa del "Error no controlado": ¿hora del día, largo del rango // o que el rango termine "ahora"? 3 solicitudes de UNA FIEL, una a la vez. const TENANT_ID = '81116985-03cd-4843-97ba-05e8be9917c6'; // auza const DB = 'horux_auza640701ti9'; const CONTRIBUYENTE_ID = 'bb921e1d-ed49-4139-bb6f-bca28980050f'; async function probe(label: string, fn: () => Promise) { try { const r = await fn(); console.log(`[${label}] success=${r.success} statusCode=${r.statusCode ?? '-'} message="${r.message}" requestId=${r.requestId ?? '-'}`); } catch (e: any) { console.log(`[${label}] EXCEPTION: ${e?.message || e}`); } } async function main() { const pool = await tenantDb.getPool(TENANT_ID, DB); const fiel = await getDecryptedFielContribuyente(pool, CONTRIBUYENTE_ID); if (!fiel) { console.log('Sin FIEL'); return; } const service = createSatService({ cerContent: fiel.cerContent, keyContent: fiel.keyContent, password: fiel.password }); const now = new Date(); console.log(`now = ${now.toISOString()}`); const d = (daysBack: number, endOfDay = false) => { const x = new Date(now.getTime() - daysBack * 86400000); if (endOfDay) x.setUTCHours(23, 59, 59, 0); return x; }; // A: réplica exacta de la sonda exitosa de mediodía (1 día, terminando ayer) await probe('A 1d ayer (replica mediodia)', () => querySat(service, d(2), d(1), 'emitidos', 'cfdi')); // B: réplica del daily que falla (7 días, terminando AHORA) await probe('B 7d terminando ahora (replica daily)', () => querySat(service, d(7), now, 'emitidos', 'cfdi')); // C: 7 días pero terminando ayer 23:59 (aisla "termina ahora" vs "7 días") await probe('C 7d terminando ayer', () => querySat(service, d(8, false), d(1, true), 'emitidos', 'cfdi')); } main().then(() => process.exit(0)).catch((e) => { console.error(e); process.exit(1); });