diff --git a/src/lib/filebrowserFolders.js b/src/lib/filebrowserFolders.js index 8dab5ea..f1d108a 100644 --- a/src/lib/filebrowserFolders.js +++ b/src/lib/filebrowserFolders.js @@ -50,7 +50,7 @@ export async function createTaskFolder(companyName, projectName, taskTitle) { `/Clients/${co}/Projects/${proj}/00 Project Files/`, `${base}/`, `${base}/Request Info/`, - `${base}/Old Book/`, + `${base}/Old Books/`, ]; for (const dir of mkdirs) { await fbq('mkdir', dir); diff --git a/supabase/functions/fbq-backfill/index.ts b/supabase/functions/fbq-backfill/index.ts index 8b03a2b..12771a0 100644 --- a/supabase/functions/fbq-backfill/index.ts +++ b/supabase/functions/fbq-backfill/index.ts @@ -32,6 +32,45 @@ function mime(name: string) { return m[e] ?? 'application/octet-stream'; } +async function listDir(path: string): Promise<{ name: string; type: string }[] | null> { + const r = await fetch(`${FBQ_URL}/api/resources?source=${SOURCE}&path=${encodeURIComponent(path)}`, { headers: fbqH }); + if (!r.ok) return null; + const data = await r.json().catch(() => null); + return [...(data?.folders ?? []), ...(data?.files ?? [])]; +} + +async function renameDir(fromPath: string, toPath: string) { + await fetch(`${FBQ_URL}/api/resources?source=${SOURCE}`, { + method: 'PATCH', + headers: { ...fbqH, 'Content-Type': 'application/json' }, + body: JSON.stringify({ action: 'rename', items: [{ fromSource: SOURCE, fromPath, toSource: SOURCE, toPath }], overwrite: false }), + }); +} + +async function deleteDir(path: string) { + await fetch(`${FBQ_URL}/api/resources?source=${SOURCE}&path=${encodeURIComponent(path.endsWith('/') ? path : path + '/')}`, { + method: 'DELETE', headers: fbqH, + }); +} + +async function migrateOldBook(base: string) { + const src = `${base}/Old Book`; + const dst = `${base}/Old Books`; + const srcItems = await listDir(src); + if (srcItems === null) return; // Old Book doesn't exist + const dstItems = await listDir(dst); + if (dstItems === null) { + // Old Books doesn't exist — simple rename + await renameDir(src, dst); + } else { + // Both exist — move each item from Old Book into Old Books, then delete Old Book + for (const item of srcItems) { + await renameDir(`${src}/${item.name}`, `${dst}/${item.name}`); + } + await deleteDir(src); + } +} + async function chunkAll(items: T[], fn: (item: T) => Promise, size = 8) { for (let i = 0; i < items.length; i += size) { await Promise.all(items.slice(i, i + size).map(fn)); @@ -90,7 +129,8 @@ Deno.serve(async () => { ]); const taskIsNew = await mkdir(base); - await Promise.all([mkdir(`${base}/Request Info`), mkdir(`${base}/Old Book`)]); + await Promise.all([mkdir(`${base}/Request Info`), mkdir(`${base}/Old Books`)]); + await migrateOldBook(base); if (taskIsNew) { newTasks.push({ task, base, co, pr }); created++; } else skipped++;