feat: Google Drive agency sync with auto folder match
Connect marketing@ once, match Drive folders to tenants by name, list Drive in Activos, and mirror portal uploads to the linked folder. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -15,6 +15,11 @@ import {
|
||||
sanitizeFolder,
|
||||
serviceSupabase,
|
||||
} from '@/lib/assets';
|
||||
import {
|
||||
ensureChildFolder,
|
||||
getTenantDriveFolderId,
|
||||
uploadToDrive,
|
||||
} from '@/lib/google-drive';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
@@ -176,7 +181,47 @@ export async function POST(req: NextRequest) {
|
||||
continue;
|
||||
}
|
||||
const created = await ins.json();
|
||||
uploaded.push(Array.isArray(created) ? created[0] : created);
|
||||
const assetRow = Array.isArray(created) ? created[0] : created;
|
||||
|
||||
// Best-effort mirror to linked Google Drive folder
|
||||
try {
|
||||
const link = await getTenantDriveFolderId(ctx.tenantId!);
|
||||
if (link) {
|
||||
let parentId = link.folderId;
|
||||
if (folder) {
|
||||
const parts = folder.split('/').filter(Boolean);
|
||||
for (const part of parts) {
|
||||
parentId = await ensureChildFolder(parentId, part);
|
||||
}
|
||||
}
|
||||
const driveFile = await uploadToDrive({
|
||||
parentFolderId: parentId,
|
||||
fileName: safeName,
|
||||
mimeType: file.type || 'application/octet-stream',
|
||||
buffer: buf,
|
||||
});
|
||||
if (driveFile.id && assetRow?.id) {
|
||||
await fetch(
|
||||
`${supabaseUrl()}/rest/v1/asset_files?id=eq.${encodeURIComponent(assetRow.id)}`,
|
||||
{
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
...headers,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ drive_file_id: driveFile.id }),
|
||||
}
|
||||
);
|
||||
assetRow.drive_file_id = driveFile.id;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
errors.push(
|
||||
`${file.name}: portal ok, Drive: ${e instanceof Error ? e.message : 'error'}`
|
||||
);
|
||||
}
|
||||
|
||||
uploaded.push(assetRow);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
|
||||
Reference in New Issue
Block a user