fix: ficha no finge sync al guardar IDs; normaliza URL de Drive

«Conectar» en la ficha solo guarda la referencia; last_sync queda
para syncs reales. Drive acepta URL completa → folder id.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
4Ventures
2026-09-24 12:22:41 +02:00
parent 5501f45a67
commit 81ba5df37d
2 changed files with 20 additions and 5 deletions

View File

@@ -129,13 +129,24 @@ export async function PATCH(req: NextRequest) {
};
if (status) patch.status = status;
if (externalAccountId !== undefined) {
patch.external_account_id = externalAccountId || null;
let id = externalAccountId || null;
// Drive: accept full folder URL → store folder id only
if (provider === 'google_drive' && id) {
const m = id.match(/\/folders\/([a-zA-Z0-9_-]+)/);
if (m) id = m[1];
else if (id.includes('http')) {
const q = id.match(/[?&]id=([a-zA-Z0-9_-]+)/);
if (q) id = q[1];
}
}
patch.external_account_id = id;
}
if (label !== undefined) patch.label = label || null;
if (errorMessage !== undefined) patch.error_message = errorMessage || null;
if (enabled !== undefined) patch.enabled = enabled;
// Manual «Conectar» only marks the reference — do not fake a data sync timestamp.
// Real syncs (Ads discover, Orgánico, Superhuman…) set last_sync_at themselves.
if (status === 'connected') {
patch.last_sync_at = new Date().toISOString();
patch.error_message = null;
}
if (status === 'disconnected') {