feat: Orgánico siempre al día (auto-sync al abrir + cron diario)

El botón Sync 28d pasa a «Actualizar ahora»; la vista refresca sola
si faltan datos y hay endpoint /api/cron/organic-sync.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
4Ventures
2026-09-24 12:10:02 +02:00
parent aded4c3514
commit d137b5796a
7 changed files with 267 additions and 88 deletions

View File

@@ -70,8 +70,8 @@ export default function GoogleOrganicPanel() {
body: JSON.stringify({ action: 'match', apply }),
});
const data = await res.json();
setBusy(null);
if (!res.ok) {
setBusy(null);
setErr(data.error || 'Discover falló');
return;
}
@@ -80,9 +80,14 @@ export default function GoogleOrganicPanel() {
setGscCount(data.gscCount || 0);
setMsg(
apply
? `Emparejado · GA4 ${data.applied?.ga4 || 0} · GSC ${data.applied?.gsc || 0} (de ${data.ga4Count} props / ${data.gscCount} sites)`
? `Emparejado · GA4 ${data.applied?.ga4 || 0} · GSC ${data.applied?.gsc || 0}. Actualizando datos…`
: `Vistas ${data.ga4Count} props GA4 · ${data.gscCount} sites GSC (sin escribir)`
);
if (apply) {
await syncAll();
} else {
setBusy(null);
}
}
async function syncAll() {
@@ -96,7 +101,7 @@ export default function GoogleOrganicPanel() {
const data = await res.json();
setBusy(null);
if (!res.ok) {
setErr(data.error || 'Sync falló');
setErr(data.error || 'Actualización falló');
return;
}
const wrote = (data.results || []).reduce(
@@ -104,7 +109,7 @@ export default function GoogleOrganicPanel() {
0
);
setMsg(
`Sync orgánico: ${data.synced || 0} clientes · ${wrote} días escritos en organic_daily`
`Datos al día · ${data.synced || 0} clientes · ${wrote} días en Orgánico (se actualiza solo al abrir la vista y cada noche)`
);
}
@@ -129,7 +134,8 @@ export default function GoogleOrganicPanel() {
Usa el mismo OAuth de{' '}
<strong className="text-gray-800">marketing@4ventures.es</strong>{' '}
(Drive + Analytics + GSC). Empareja propiedades/sites a fichas y
sincroniza a Orgánico.
sincroniza a Orgánico. Los datos se mantienen al día solos (al abrir
la vista + cron diario).
</p>
</div>
</div>
@@ -164,7 +170,7 @@ export default function GoogleOrganicPanel() {
) : (
<RefreshCw className="w-3.5 h-3.5" />
)}
Sync 28d → Orgánico
Actualizar ahora
</button>
</div>
</div>

View File

@@ -135,6 +135,11 @@ export default function OrganicoDashboard({
setIntegrations(data.integrations || []);
setWorkspace(data.workspace || {});
setWarning(data.warning || null);
if (data.autoSynced?.daysWritten) {
setMsg(
`Datos GA4/GSC actualizados automáticamente (${data.autoSynced.daysWritten} días).`
);
}
setLoading(false);
}, []);