Fix: Conditional sidebar and full-width News Feed
This commit is contained in:
parent
c76ed914b1
commit
a0dcd01b9b
@ -48,46 +48,46 @@ export async function GlobalTopBanner() {
|
||||
}
|
||||
|
||||
export async function GlobalSidebar() {
|
||||
const { createAdminClient } = await import('@/lib/appwrite.server');
|
||||
const client = await createAdminClient();
|
||||
let groupedAds: Record<string, any[]> = {};
|
||||
let settings = await fetchSettings();
|
||||
|
||||
try {
|
||||
// Traemos "Display Slider", "Evento", "Clasificado" (Obtener sus IDs)
|
||||
const typesRes = await databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'publication_types', []);
|
||||
const typesRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'publication_types', []);
|
||||
const validCodes = ['DISPLAY_SLIDER', 'EVENTO', 'CLASIFICADO'];
|
||||
const validTypeIds = typesRes.documents.filter(t => validCodes.includes(t.codigo)).map(t => t.$id);
|
||||
|
||||
if (validTypeIds.length > 0) {
|
||||
const postsRes = await databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'posts', [
|
||||
const postsRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'posts', [
|
||||
Query.equal('estado', 'Publicado'),
|
||||
Query.orderAsc('fecha_finalizacion'), // Los que caducan más pronto primero
|
||||
Query.orderAsc('fecha_finalizacion'),
|
||||
Query.limit(50)
|
||||
]);
|
||||
|
||||
const now = new Date();
|
||||
const activePosts = postsRes.documents.filter(post => {
|
||||
// Filtramos por tipo y caducidad
|
||||
if (!post.tipo_publicacion_id || !validTypeIds.includes(post.tipo_publicacion_id.$id)) return false;
|
||||
if (!post.tipo_publicacion_id || !validTypeIds.includes(post.tipo_publicacion_id)) return false;
|
||||
if (post.fecha_finalizacion && new Date(post.fecha_finalizacion) < now) return false;
|
||||
return true;
|
||||
});
|
||||
|
||||
// Agrupar por eslogan del cliente / patrocinador
|
||||
for (const post of activePosts) {
|
||||
let slogan = 'Patrocinado';
|
||||
// El post tiene cliente. El cliente tiene contratos activos.
|
||||
// Como no queremos hacer nested queries destructivas, tomaremos el cliente_id
|
||||
// y buscaremos sus contratos.
|
||||
if (post.cliente_id) {
|
||||
const contracts = await databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'contracts', [
|
||||
Query.equal('cliente_id', post.cliente_id.$id),
|
||||
Query.equal('estado', 'Activo')
|
||||
]);
|
||||
if (contracts.documents.length > 0 && contracts.documents[0].eslogan) {
|
||||
slogan = contracts.documents[0].eslogan;
|
||||
} else {
|
||||
slogan = post.cliente_id.nombre_entidad || 'General';
|
||||
}
|
||||
try {
|
||||
const contracts = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'contracts', [
|
||||
Query.equal('cliente_id', post.cliente_id),
|
||||
Query.equal('estado', 'Activo'),
|
||||
Query.limit(1)
|
||||
]);
|
||||
if (contracts.documents.length > 0 && contracts.documents[0].eslogan) {
|
||||
slogan = contracts.documents[0].eslogan;
|
||||
} else {
|
||||
// Fallback to client name if we can fetch it or if it's in the object
|
||||
slogan = 'Patrocinado';
|
||||
}
|
||||
} catch(e) { slogan = 'Patrocinado'; }
|
||||
}
|
||||
|
||||
if (!groupedAds[slogan]) groupedAds[slogan] = [];
|
||||
@ -102,3 +102,29 @@ export async function GlobalSidebar() {
|
||||
|
||||
return <ClientSidebars groupedAds={groupedAds} rotationSeconds={settings.rotation_seconds} />
|
||||
}
|
||||
|
||||
export async function hasSidebarContent(): Promise<boolean> {
|
||||
try {
|
||||
const { createAdminClient } = await import('@/lib/appwrite.server');
|
||||
const client = await createAdminClient();
|
||||
const typesRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'publication_types', []);
|
||||
const validCodes = ['DISPLAY_SLIDER', 'EVENTO', 'CLASIFICADO'];
|
||||
const validTypeIds = typesRes.documents.filter(t => validCodes.includes(t.codigo)).map(t => t.$id);
|
||||
|
||||
if (validTypeIds.length === 0) return false;
|
||||
|
||||
const postsRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'posts', [
|
||||
Query.equal('estado', 'Publicado'),
|
||||
Query.limit(20)
|
||||
]);
|
||||
|
||||
const now = new Date();
|
||||
return postsRes.documents.some(post => {
|
||||
if (!post.tipo_publicacion_id || !validTypeIds.includes(post.tipo_publicacion_id)) return false;
|
||||
if (post.fecha_finalizacion && new Date(post.fecha_finalizacion) < now) return false;
|
||||
return true;
|
||||
});
|
||||
} catch(e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user