// src/app/dashboard/sync-actions.ts 'use server'; import { auth } from "@/auth"; import { revalidatePath } from "next/cache"; import { upsertFileNode } from "@/data-access/file-nodes"; import { getWebCalibreChildren } from "@/services/onedrive"; // src/app/dashboard/sync-actions.ts //import { upsertFileNode } from "@/data-access/file-nodes"; // Change this from /services/onedrive export async function syncOneDrive() { const session = await auth(); if (!session?.user?.id) throw new Error("Unauthorized"); try { // 1. Call Service to get cloud data (token refresh handled inside service) const items = await getWebCalibreChildren(session.user.id); let syncedCount = 0; const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; for (const item of items) { const isFolder = !!item.folder; // Business Logic: Skip UUID storage folders if (isFolder && uuidRegex.test(item.name)) continue; const extension = isFolder ? 'FOLDER' : (item.name.split('.').pop()?.toUpperCase() || 'UNKNOWN'); // 2. Call DAL to save to database await upsertFileNode(item.id, { name: item.name, size: BigInt(item.size || 0), isFolder: isFolder, path: item.parentReference?.path + '/' + item.name, ownerId: session.user.id, metadata: { type: extension, mimeType: item.file?.mimeType || null }, }); syncedCount++; } revalidatePath('/dashboard'); return { success: true, count: syncedCount }; } catch (error: any) { console.error("Sync Error:", error.message); throw new Error("Failed to sync with OneDrive"); } }