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() {
|
export async function GlobalSidebar() {
|
||||||
|
const { createAdminClient } = await import('@/lib/appwrite.server');
|
||||||
|
const client = await createAdminClient();
|
||||||
let groupedAds: Record<string, any[]> = {};
|
let groupedAds: Record<string, any[]> = {};
|
||||||
let settings = await fetchSettings();
|
let settings = await fetchSettings();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Traemos "Display Slider", "Evento", "Clasificado" (Obtener sus IDs)
|
const typesRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'publication_types', []);
|
||||||
const typesRes = await databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'publication_types', []);
|
|
||||||
const validCodes = ['DISPLAY_SLIDER', 'EVENTO', 'CLASIFICADO'];
|
const validCodes = ['DISPLAY_SLIDER', 'EVENTO', 'CLASIFICADO'];
|
||||||
const validTypeIds = typesRes.documents.filter(t => validCodes.includes(t.codigo)).map(t => t.$id);
|
const validTypeIds = typesRes.documents.filter(t => validCodes.includes(t.codigo)).map(t => t.$id);
|
||||||
|
|
||||||
if (validTypeIds.length > 0) {
|
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.equal('estado', 'Publicado'),
|
||||||
Query.orderAsc('fecha_finalizacion'), // Los que caducan más pronto primero
|
Query.orderAsc('fecha_finalizacion'),
|
||||||
Query.limit(50)
|
Query.limit(50)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const activePosts = postsRes.documents.filter(post => {
|
const activePosts = postsRes.documents.filter(post => {
|
||||||
// Filtramos por tipo y caducidad
|
if (!post.tipo_publicacion_id || !validTypeIds.includes(post.tipo_publicacion_id)) return false;
|
||||||
if (!post.tipo_publicacion_id || !validTypeIds.includes(post.tipo_publicacion_id.$id)) return false;
|
|
||||||
if (post.fecha_finalizacion && new Date(post.fecha_finalizacion) < now) return false;
|
if (post.fecha_finalizacion && new Date(post.fecha_finalizacion) < now) return false;
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Agrupar por eslogan del cliente / patrocinador
|
|
||||||
for (const post of activePosts) {
|
for (const post of activePosts) {
|
||||||
let slogan = 'Patrocinado';
|
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) {
|
if (post.cliente_id) {
|
||||||
const contracts = await databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'contracts', [
|
try {
|
||||||
Query.equal('cliente_id', post.cliente_id.$id),
|
const contracts = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'contracts', [
|
||||||
Query.equal('estado', 'Activo')
|
Query.equal('cliente_id', post.cliente_id),
|
||||||
]);
|
Query.equal('estado', 'Activo'),
|
||||||
if (contracts.documents.length > 0 && contracts.documents[0].eslogan) {
|
Query.limit(1)
|
||||||
slogan = contracts.documents[0].eslogan;
|
]);
|
||||||
} else {
|
if (contracts.documents.length > 0 && contracts.documents[0].eslogan) {
|
||||||
slogan = post.cliente_id.nombre_entidad || 'General';
|
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] = [];
|
if (!groupedAds[slogan]) groupedAds[slogan] = [];
|
||||||
@ -102,3 +102,29 @@ export async function GlobalSidebar() {
|
|||||||
|
|
||||||
return <ClientSidebars groupedAds={groupedAds} rotationSeconds={settings.rotation_seconds} />
|
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