77 lines
2.8 KiB
TypeScript
77 lines
2.8 KiB
TypeScript
import { prisma, tenantDb } from '../src/config/database.js';
|
|
|
|
const TENANT_RFC = 'DESPACHO_MO3NI6U8_B9VGG';
|
|
|
|
async function main() {
|
|
const tenant = await prisma.tenant.findFirst({
|
|
where: { rfc: TENANT_RFC },
|
|
select: { id: true, databaseName: true },
|
|
});
|
|
if (!tenant) {
|
|
console.log('Tenant no encontrado');
|
|
return;
|
|
}
|
|
const pool = await tenantDb.getPool(tenant.id, tenant.databaseName);
|
|
|
|
console.log(`\n=== Tenant ${TENANT_RFC} ===\n`);
|
|
|
|
// 1) CFDIs emitidos via Facturapi (cualquier emisor) últimos 7 días
|
|
console.log(`>> CFDIs con source='facturapi' o facturapi_id no nulo, últimos 7 días:`);
|
|
const { rows: recientes } = await pool.query(
|
|
`SELECT uuid, rfc_emisor, rfc_receptor, nombre_receptor, tipo_comprobante, metodo_pago,
|
|
total, total_mxn, status, fecha_emision, source, facturapi_id
|
|
FROM cfdis
|
|
WHERE (source = 'facturapi' OR facturapi_id IS NOT NULL)
|
|
AND fecha_emision >= NOW() - interval '7 days'
|
|
ORDER BY fecha_emision DESC
|
|
LIMIT 20`,
|
|
);
|
|
if (recientes.length === 0) console.log(' (ninguno)');
|
|
for (const r of recientes) {
|
|
const emisor = r.rfc_emisor || '<NULL>';
|
|
const receptor = r.rfc_receptor || '<NULL>';
|
|
console.log(` ${r.uuid}`);
|
|
console.log(` EMISOR=${emisor} RECEPTOR=${receptor} (${r.nombre_receptor})`);
|
|
console.log(` tipo=${r.tipo_comprobante}/${r.metodo_pago} total=${r.total} status=${r.status} source=${r.source}`);
|
|
console.log(` fecha_emision=${r.fecha_emision?.toISOString?.() || r.fecha_emision}`);
|
|
console.log(` facturapi_id=${r.facturapi_id}`);
|
|
}
|
|
|
|
// 2) CFDIs totales en últimas 2 horas (cualquier emisor, cualquier source)
|
|
console.log(`\n>> CFDIs insertados en últimas 2 horas (cualquier source):`);
|
|
const { rows: ultimas } = await pool.query(
|
|
`SELECT uuid, rfc_emisor, rfc_receptor, tipo_comprobante, total,
|
|
status, fecha_emision, source, facturapi_id
|
|
FROM cfdis
|
|
WHERE fecha_emision >= NOW() - interval '2 hours'
|
|
ORDER BY fecha_emision DESC
|
|
LIMIT 20`,
|
|
);
|
|
if (ultimas.length === 0) console.log(' (ninguno)');
|
|
for (const r of ultimas) {
|
|
console.log(` ${r.uuid} | ${r.rfc_emisor} → ${r.rfc_receptor}`);
|
|
console.log(` tipo=${r.tipo_comprobante} total=${r.total} status=${r.status} source=${r.source}`);
|
|
console.log(` facturapi_id=${r.facturapi_id || 'null'}`);
|
|
}
|
|
|
|
// 3) Distribución de source en toda la BD
|
|
console.log(`\n>> Distribución de 'source' en cfdis:`);
|
|
const { rows: dist } = await pool.query(
|
|
`SELECT source, COUNT(*)::int AS cnt
|
|
FROM cfdis
|
|
GROUP BY source
|
|
ORDER BY cnt DESC`,
|
|
);
|
|
for (const r of dist) {
|
|
console.log(` source=${r.source || 'NULL'} → ${r.cnt}`);
|
|
}
|
|
|
|
await prisma.$disconnect();
|
|
}
|
|
|
|
main().catch(async e => {
|
|
console.error(e);
|
|
await prisma.$disconnect().catch(() => {});
|
|
process.exit(1);
|
|
});
|