diff --git a/src/components/AdWrappers.tsx b/src/components/AdWrappers.tsx index 2f3034a..a2f4544 100644 --- a/src/components/AdWrappers.tsx +++ b/src/components/AdWrappers.tsx @@ -1,12 +1,16 @@ -import { databases, APPWRITE_ALMEIRA_CONFIG, Query } from '@/lib/appwrite'; +import { APPWRITE_ALMEIRA_CONFIG, Query } from '@/lib/appwrite'; +import { createAdminClient } from '@/lib/appwrite.server'; import TopBanner from './TopBanner'; import ClientSidebars from './ClientSidebars'; async function fetchSettings() { try { - const res = await databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'settings', []); + const client = await createAdminClient(); + const res = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'settings', [Query.limit(1)]); if (res.documents.length > 0) return res.documents[0]; - } catch(e) {} + } catch(e) { + console.error("fetchSettings error:", e); + } return { rotation_seconds: 5, ticker_count: 10 }; } @@ -15,16 +19,15 @@ export async function GlobalTopBanner() { let settings = await fetchSettings(); try { - const typesRes = await databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'publication_types', [ + const client = await createAdminClient(); + const typesRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'publication_types', [ Query.equal('codigo', 'DISPLAY_BANNER') ]); if (typesRes.documents.length > 0) { const typeId = typesRes.documents[0].$id; - // Limitamos a los banners vigentes (fecha caducidad en el futuro o nula) - // Appwrite requiere Querys explĂ­citas. Traemos los activos y los ordenamos. - const postsRes = await databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'posts', [ + const postsRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'posts', [ Query.equal('tipo_publicacion_id', typeId), Query.equal('estado', 'Publicado'), Query.orderDesc('fecha_publicacion'), diff --git a/src/components/TopBanner.tsx b/src/components/TopBanner.tsx index d08ea83..2b0a2a8 100644 --- a/src/components/TopBanner.tsx +++ b/src/components/TopBanner.tsx @@ -27,7 +27,18 @@ export default function TopBanner({ initialBanners, rotationSeconds }: { initial const ENDPOINT = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT || 'https://appwrite.almeira.com.co/v1'; const PROJECT_ID = process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID || '69b84df5003bd8957c5c'; - const imgUrl = `${ENDPOINT}/storage/buckets/${activeBanner.imagen_destacada_id.bucket_id}/files/${activeBanner.imagen_destacada_id.file_id}/view?project=${PROJECT_ID}`; + let imgUrl = activeBanner.og_image_url || null; + + if (activeBanner.imagen_destacada_id) { + if (typeof activeBanner.imagen_destacada_id === 'object' && activeBanner.imagen_destacada_id.bucket_id) { + imgUrl = `${ENDPOINT}/storage/buckets/${activeBanner.imagen_destacada_id.bucket_id}/files/${activeBanner.imagen_destacada_id.file_id}/view?project=${PROJECT_ID}`; + } else if (typeof activeBanner.imagen_destacada_id === 'string') { + // Fallback to 'media' bucket if we only have the ID + imgUrl = `${ENDPOINT}/storage/buckets/media/files/${activeBanner.imagen_destacada_id}/view?project=${PROJECT_ID}`; + } + } + + if (!imgUrl) return null; const recordClickAndGo = () => { fetch('/api/metrics', {