diff --git a/src/app/actions/entity.ts b/src/app/actions/entity.ts index 5b7394d..8d388e1 100644 --- a/src/app/actions/entity.ts +++ b/src/app/actions/entity.ts @@ -26,7 +26,11 @@ export async function getEntityConfig() { [Query.limit(1)] ); return response.documents[0] || null; - } catch (error) { + } catch (error: any) { + // Silently handle 404 (collection not found) + if (error?.code === 404) { + return null; + } console.error('Error fetching entity config:', error); return null; } @@ -40,6 +44,11 @@ export async function updateEntityConfig(data: any) { const databases = await getAdminClient(); const existing = await getEntityConfig(); + if (!data || Object.keys(data).length === 0) { + console.warn('updateEntityConfig: Data is empty, skipping update.'); + return existing; + } + if (existing) { const result = await databases.updateDocument( DATABASE_ID, diff --git a/src/app/dashboard/[entity]/page.tsx b/src/app/dashboard/[entity]/page.tsx index cc68cce..8b83b82 100644 --- a/src/app/dashboard/[entity]/page.tsx +++ b/src/app/dashboard/[entity]/page.tsx @@ -40,7 +40,9 @@ async function PostsPage({ collectionId, [Query.orderDesc('$createdAt'), Query.limit(100)] ); - posts = res.documents as unknown as Post[]; + + // Deep-clone/sanitize to avoid "Only plain objects..." Next.js error + posts = JSON.parse(JSON.stringify(res.documents)) as Post[]; // Pre-load relation data for the drawer/form const fields = SCHEMA_MAP[entity] || []; @@ -63,6 +65,9 @@ async function PostsPage({ } }) ); + + // Sanitize relationData too + relationData = JSON.parse(JSON.stringify(relationData)); } catch (error) { console.error('PostsPage fetch error:', error); } @@ -142,7 +147,7 @@ export default async function EntityPage({ ); } - const user = await getLoggedInUser(); + const user = JSON.parse(JSON.stringify(await getLoggedInUser())); const prefs = await getUserPreferences(); const hiddenColumns = prefs.hiddenColumns?.[entity] || []; @@ -183,7 +188,8 @@ export default async function EntityPage({ collectionId, queries ); - documents = response.documents as Array>; + // Deep-clone/sanitize documents to avoid "Only plain objects..." Next.js error + documents = JSON.parse(JSON.stringify(response.documents)); total = response.total; } catch (error) { console.error(`Error fetching from ${collectionId}:`, error); diff --git a/src/components/NewsTicker.tsx b/src/components/NewsTicker.tsx index 311f5de..607f6e3 100644 --- a/src/components/NewsTicker.tsx +++ b/src/components/NewsTicker.tsx @@ -72,23 +72,35 @@ export default async function NewsTicker() { // 3. Fetch Approved WhatsApp Messages try { - const waRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'whatsapp_messages', [ - Query.equal('status', 'approved'), - Query.orderDesc('timestamp'), - Query.limit(5) - ]); + let waRes; + try { + waRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'whatsapp_messages', [ + Query.equal('status', 'approved'), + Query.orderDesc('timestamp'), + Query.limit(5) + ]); + } catch (queryError: any) { + // If 'status' attribute is missing, try fetching without it but still limited + console.warn("WhatsApp 'status' query failed, trying fallback fetch:", queryError.message); + waRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'whatsapp_messages', [ + Query.orderDesc('$createdAt'), + Query.limit(5) + ]); + } - waRes.documents.forEach((msg: any) => { - tickerItems.push({ - type: 'whatsapp', - id: msg.$id, - titulo: `${msg.sender_name}: ${msg.content}`, - slug: null, - imagenUrl: msg.avatar || null + if (waRes && waRes.documents) { + waRes.documents.forEach((msg: any) => { + tickerItems.push({ + type: 'whatsapp', + id: msg.$id, + titulo: `${msg.sender_name || 'Usuario'}: ${msg.content || ''}`, + slug: null, + imagenUrl: msg.avatar || null + }); }); - }); + } } catch (waError) { - console.warn("Skipping WhatsApp messages in ticker:", waError); + console.warn("Skipping WhatsApp messages in ticker (Collection or Schema missing):", waError); } } catch (e) {