fix: guardar OAuth Google con upsert real (PATCH vacío no insertaba)

El callback creía haber guardado el token y fallaba al emparejar Drive
con «no conectado». Upsert on_conflict=provider + match no bloquea OAuth.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
4Ventures
2026-09-24 12:17:46 +02:00
parent 5d04a79de7
commit 5501f45a67
3 changed files with 59 additions and 46 deletions

View File

@@ -56,27 +56,31 @@ export async function GET(req: NextRequest) {
email,
});
// Auto-match
const headers = serviceHeaders();
// Auto-match folders (non-fatal: tokens already saved)
let linked = 0;
let ambiguous = 0;
if (headers) {
const tRes = await fetch(
`${supabaseUrl()}/rest/v1/tenants?select=id,name,slug&order=name.asc`,
{ headers, cache: 'no-store' }
);
if (tRes.ok) {
const tenants = (await tRes.json()) as {
id: string;
name: string;
slug: string;
}[];
const folders = await listDriveFolders();
const matches = await matchFoldersToTenants(tenants, folders);
const applied = await applyMatches(matches);
linked = applied.linked;
ambiguous = matches.filter((m) => m.status === 'ambiguous').length;
try {
const headers = serviceHeaders();
if (headers) {
const tRes = await fetch(
`${supabaseUrl()}/rest/v1/tenants?select=id,name,slug&order=name.asc`,
{ headers, cache: 'no-store' }
);
if (tRes.ok) {
const tenants = (await tRes.json()) as {
id: string;
name: string;
slug: string;
}[];
const folders = await listDriveFolders();
const matches = await matchFoldersToTenants(tenants, folders);
const applied = await applyMatches(matches);
linked = applied.linked;
ambiguous = matches.filter((m) => m.status === 'ambiguous').length;
}
}
} catch {
// match can fail without undoing OAuth
}
const res = NextResponse.redirect(