feat(sat): reporte diario de errores SAT por proxy

- Agrega tabla sat_proxy_errors con proxy_usado, error_code, stage, message.
- sat-client.service.ts expone proxyInfo usado en cada conexión SAT.
- sat.service.ts registra errores de bloqueo/devolución del SAT en sat_proxy_errors
  y guarda proxyUsed en sat_sync_jobs.
- Nuevo job sat-proxy-report.job.ts: cron 8 AM CDMX, envía email a ADMIN_EMAIL
  con errores de las últimas 24h agrupados por proxy/error_code.
- Template de email sat-proxy-report.ts y método sendSatProxyReport.
- Registra el cron en src/index.ts.
- Actualiza docs/SAT-SYNC-IMPLEMENTATION.md.
This commit is contained in:
Horux Dev
2026-08-03 14:27:19 +00:00
parent 3de0014e80
commit 099f34c903
57 changed files with 4554 additions and 7 deletions

View File

@@ -0,0 +1,78 @@
import { startSync } from '../src/services/sat/sat.service.js';
import { prisma } from '../src/config/database.js';
const POLL_INTERVAL_MS = 60_000;
interface TestCase {
name: string;
tenantId: string;
contribuyenteId: string;
rfc: string;
}
const tests: TestCase[] = [
{
name: 'Horux 360',
tenantId: 'c52c2f5d-b1ae-45c6-8cc8-b11c9611618a',
contribuyenteId: '4a1d6014-f705-424b-b185-7740be6a80c6',
rfc: 'TORC9611214CA',
},
{
name: 'C&L (falló antes)',
tenantId: '49b60455-c501-4ca2-b4bc-36ea7f2951a2',
contribuyenteId: '36b43d67-3b92-4307-95a6-c8e4053e0140',
rfc: 'GCC080208478',
},
];
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function waitForJob(jobId: string): Promise<{ status: string; error: string | null; found: number; inserted: number; updated: number; progress: number }> {
while (true) {
const job = await prisma.satSyncJob.findUnique({ where: { id: jobId } });
if (!job) throw new Error(`Job ${jobId} no encontrado`);
if (job.status === 'completed' || job.status === 'failed' || job.status === 'pending') {
return {
status: job.status,
error: job.errorMessage,
found: job.cfdisFound || 0,
inserted: job.cfdisInserted || 0,
updated: job.cfdisUpdated || 0,
progress: job.progressPercent || 0,
};
}
console.log(`[WAIT] Job ${jobId} status=${job.status}, progress=${job.progressPercent}%. Esperando 60s...`);
await sleep(POLL_INTERVAL_MS);
}
}
async function runTest(test: TestCase): Promise<void> {
console.log(`\n[TEST] === ${test.name} | ${test.rfc} ===`);
try {
const jobId = await startSync(test.tenantId, 'daily', undefined, undefined, test.contribuyenteId);
console.log(`[TEST] Job iniciado: ${jobId}`);
const result = await waitForJob(jobId);
console.log(`[TEST] ${test.rfc} finalizado: status=${result.status}, found=${result.found}, inserted=${result.inserted}, updated=${result.updated}, progress=${result.progress}%`);
if (result.error) {
console.error(`[TEST] Error en ${test.rfc}: ${result.error}`);
}
} catch (error: any) {
console.error(`[TEST] Error lanzando sync para ${test.rfc}:`, error.message || error);
}
}
async function main() {
console.log('[TEST] Iniciando prueba RFC vs IP');
for (const test of tests) {
await runTest(test);
}
console.log('\n[TEST] Prueba finalizada');
}
main().catch(async (err) => {
console.error('[TEST] Error fatal:', err);
await prisma.$disconnect().catch(() => {});
process.exit(1);
});