Mega push vol 4
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import type { SupabaseClient } from '@supabase/supabase-js';
|
||||
import type { Database, Document } from '$lib/supabase/types';
|
||||
import { createLogger } from '$lib/utils/logger';
|
||||
|
||||
export interface DocumentWithChildren extends Document {
|
||||
children?: DocumentWithChildren[];
|
||||
}
|
||||
const log = createLogger('api.documents');
|
||||
|
||||
export async function fetchDocuments(
|
||||
supabase: SupabaseClient<Database>,
|
||||
@@ -16,7 +15,11 @@ export async function fetchDocuments(
|
||||
.order('type', { ascending: false }) // folders first
|
||||
.order('name');
|
||||
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
log.error('fetchDocuments failed', { error, data: { orgId } });
|
||||
throw error;
|
||||
}
|
||||
log.debug('fetchDocuments ok', { data: { count: data?.length ?? 0 } });
|
||||
return data ?? [];
|
||||
}
|
||||
|
||||
@@ -41,7 +44,11 @@ export async function createDocument(
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
log.error('createDocument failed', { error, data: { orgId, name, type, parentId } });
|
||||
throw error;
|
||||
}
|
||||
log.info('createDocument ok', { data: { id: data.id, name, type } });
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -57,7 +64,10 @@ export async function updateDocument(
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
log.error('updateDocument failed', { error, data: { id, updates } });
|
||||
throw error;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -66,7 +76,10 @@ export async function deleteDocument(
|
||||
id: string
|
||||
): Promise<void> {
|
||||
const { error } = await supabase.from('documents').delete().eq('id', id);
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
log.error('deleteDocument failed', { error, data: { id } });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function moveDocument(
|
||||
@@ -79,30 +92,12 @@ export async function moveDocument(
|
||||
.update({ parent_id: newParentId, updated_at: new Date().toISOString() })
|
||||
.eq('id', id);
|
||||
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
log.error('moveDocument failed', { error, data: { id, newParentId } });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export function buildDocumentTree(documents: Document[]): DocumentWithChildren[] {
|
||||
const map = new Map<string, DocumentWithChildren>();
|
||||
const roots: DocumentWithChildren[] = [];
|
||||
|
||||
// First pass: create map
|
||||
documents.forEach((doc) => {
|
||||
map.set(doc.id, { ...doc, children: [] });
|
||||
});
|
||||
|
||||
// Second pass: build tree
|
||||
documents.forEach((doc) => {
|
||||
const node = map.get(doc.id)!;
|
||||
if (doc.parent_id && map.has(doc.parent_id)) {
|
||||
map.get(doc.parent_id)!.children!.push(node);
|
||||
} else {
|
||||
roots.push(node);
|
||||
}
|
||||
});
|
||||
|
||||
return roots;
|
||||
}
|
||||
|
||||
export function subscribeToDocuments(
|
||||
supabase: SupabaseClient<Database>,
|
||||
|
||||
Reference in New Issue
Block a user