fix(sat-sync): fallback to central FIEL for despacho tenants without per-contribuyente FIEL
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user