fix(sat-sync): fallback to central FIEL for despacho tenants without per-contribuyente FIEL

This commit is contained in:
Horux Dev
2026-04-30 04:22:11 +00:00
parent dd8484a800
commit 746d00bb66

View File

@@ -86,7 +86,26 @@ async function syncTenant(tenantId: string): Promise<void> {
`);
if (contribuyentes.length === 0) {
console.log(`[SAT Cron] Tenant ${tenantId} (despacho) sin contribuyentes con FIEL, omitiendo`);
// Fallback: algunos despachos tienen FIEL a nivel de tenant (legacy) en lugar de per-contribuyente
const fielCentral = await prisma.fielCredential.findUnique({
where: { tenantId },
select: { isActive: true, validUntil: true },
});
if (fielCentral?.isActive && new Date() < fielCentral.validUntil) {
console.log(`[SAT Cron] Tenant ${tenantId} (despacho) sin FIEL per-contribuyente, usando FIEL central como fallback`);
const status = await getSyncStatus(tenantId);
if (status.hasActiveSync) {
console.log(`[SAT Cron] Tenant ${tenantId} ya tiene sync activo, omitiendo`);
return;
}
const needsInitial = await needsInitialSync(tenantId);
const syncType = needsInitial ? 'initial' : 'daily';
console.log(`[SAT Cron] Iniciando sync ${syncType} para tenant ${tenantId} (fallback FIEL central)`);
const jobId = await startSync(tenantId, syncType);
console.log(`[SAT Cron] Job ${jobId} iniciado para tenant ${tenantId}`);
} else {
console.log(`[SAT Cron] Tenant ${tenantId} (despacho) sin contribuyentes con FIEL ni FIEL central, omitiendo`);
}
return;
}