From 8e83dd22768d8512adb89b0b7e9f109ba16b5de4 Mon Sep 17 00:00:00 2001 From: Horux Dev Date: Thu, 30 Apr 2026 03:02:19 +0000 Subject: [PATCH] feat(sat-sync): expand incremental sync to all plans except Custom and Mi empresa - Daily sync already covers all active tenants with FIEL (no plan filter) - Incremental sync now runs for all plans EXCEPT: - 'custom' - 'mi_empresa' - 'mi_empresa_plus' - Renamed getEnterpriseTenantsWithFiel -> getIncrementalTenantsWithFiel - Updated logs to reflect new eligibility --- apps/api/src/jobs/sat-sync.job.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/apps/api/src/jobs/sat-sync.job.ts b/apps/api/src/jobs/sat-sync.job.ts index 489b524..0a9bfac 100644 --- a/apps/api/src/jobs/sat-sync.job.ts +++ b/apps/api/src/jobs/sat-sync.job.ts @@ -122,16 +122,25 @@ async function runSyncJob(): Promise { } /** - * Obtiene los tenants Enterprise activos con FIEL configurada. + * Planes excluidos del incremental (Custom y Mi empresa no reciben sync frecuente). */ -async function getEnterpriseTenantsWithFiel(): Promise { +const INCREMENTAL_EXCLUDED_PLANS = new Set(['custom', 'mi_empresa', 'mi_empresa_plus']); + +/** + * Obtiene los tenants activos con FIEL configurada que son elegibles para sync incremental. + * Incluye todos los planes EXCEPTO Custom y Mi empresa / Mi empresa plus. + */ +async function getIncrementalTenantsWithFiel(): Promise { const tenants = await prisma.tenant.findMany({ - where: { active: true, plan: 'enterprise' }, - select: { id: true }, + where: { active: true }, + select: { id: true, plan: true }, }); const result: string[] = []; for (const tenant of tenants) { + if (INCREMENTAL_EXCLUDED_PLANS.has(tenant.plan || '')) { + continue; + } if (await hasFielConfigured(tenant.id)) { result.push(tenant.id); } @@ -170,7 +179,8 @@ async function incrementalSyncTenant(tenantId: string): Promise { } /** - * Ejecuta el job incremental de 6 horas para todos los tenants Enterprise. + * Ejecuta el job incremental de 6 horas para todos los tenants elegibles. + * Elegibles = todos los planes EXCEPTO Custom y Mi empresa. */ async function runIncrementalSyncJob(): Promise { if (isIncrementalRunning) { @@ -179,11 +189,11 @@ async function runIncrementalSyncJob(): Promise { } isIncrementalRunning = true; - console.log('[SAT Cron Inc] Iniciando ciclo incremental Enterprise'); + console.log('[SAT Cron Inc] Iniciando ciclo incremental'); try { - const tenantIds = await getEnterpriseTenantsWithFiel(); - console.log(`[SAT Cron Inc] ${tenantIds.length} tenants Enterprise con FIEL`); + const tenantIds = await getIncrementalTenantsWithFiel(); + console.log(`[SAT Cron Inc] ${tenantIds.length} tenants elegibles con FIEL`); if (tenantIds.length === 0) return; @@ -480,7 +490,7 @@ export function startSatSyncJob(): void { console.log(`[SAT Cron] Retry programado cada hora`); console.log(`[Opinion Cron] Programado para: ${OPINION_CRON_SCHEDULE} (America/Mexico_City)`); console.log(`[CSF Cron] Programado para: ${CSF_CRON_SCHEDULE} (America/Mexico_City)`); - console.log(`[SAT Cron Inc] Incremental Enterprise programado para: ${INCREMENTAL_CRON_SCHEDULE} (America/Mexico_City)`); + console.log(`[SAT Cron Inc] Incremental programado para: ${INCREMENTAL_CRON_SCHEDULE} (America/Mexico_City)`); console.log(`[Subscription Cron] Lifecycle programado para: ${SUBSCRIPTION_LIFECYCLE_CRON} (America/Mexico_City)`); console.log(`[SAT Watchdog] Programado para: ${WATCHDOG_CRON_SCHEDULE} (America/Mexico_City)`); }