2026-01-16 05:22:00 +00:00
|
|
|
// src/app/dashboard/sync-actions.ts
|
2026-01-08 05:41:31 +00:00
|
|
|
'use server';
|
2026-01-16 05:22:00 +00:00
|
|
|
|
2026-01-08 05:41:31 +00:00
|
|
|
import { auth } from "@/auth";
|
|
|
|
|
import { revalidatePath } from "next/cache";
|
2026-01-16 05:22:00 +00:00
|
|
|
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
|
2026-01-08 05:41:31 +00:00
|
|
|
|
|
|
|
|
export async function syncOneDrive() {
|
|
|
|
|
const session = await auth();
|
|
|
|
|
if (!session?.user?.id) throw new Error("Unauthorized");
|
|
|
|
|
|
2026-01-11 13:41:54 +00:00
|
|
|
try {
|
2026-01-16 05:22:00 +00:00
|
|
|
// 1. Call Service to get cloud data (token refresh handled inside service)
|
|
|
|
|
const items = await getWebCalibreChildren(session.user.id);
|
2026-01-11 13:41:54 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2026-01-16 05:22:00 +00:00
|
|
|
for (const item of items) {
|
2026-01-11 13:41:54 +00:00
|
|
|
const isFolder = !!item.folder;
|
|
|
|
|
|
2026-01-16 05:22:00 +00:00
|
|
|
// Business Logic: Skip UUID storage folders
|
|
|
|
|
if (isFolder && uuidRegex.test(item.name)) continue;
|
2026-01-11 13:41:54 +00:00
|
|
|
|
2026-01-13 13:17:40 +00:00
|
|
|
const extension = isFolder ? 'FOLDER' : (item.name.split('.').pop()?.toUpperCase() || 'UNKNOWN');
|
2026-01-11 13:41:54 +00:00
|
|
|
|
2026-01-16 05:22:00 +00:00
|
|
|
// 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 },
|
2026-01-11 13:41:54 +00:00
|
|
|
});
|
2026-01-16 05:22:00 +00:00
|
|
|
|
2026-01-11 13:41:54 +00:00
|
|
|
syncedCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
revalidatePath('/dashboard');
|
|
|
|
|
return { success: true, count: syncedCount };
|
|
|
|
|
} catch (error: any) {
|
2026-01-16 05:22:00 +00:00
|
|
|
console.error("Sync Error:", error.message);
|
|
|
|
|
throw new Error("Failed to sync with OneDrive");
|
2026-01-08 05:41:31 +00:00
|
|
|
}
|
|
|
|
|
}
|