Add article facets endpoint and integrate into frontend

This commit is contained in:
Omar Sánchez Pizarro
2026-01-20 18:35:35 +01:00
parent 346dcc3dc0
commit 16ec8dc2fa
4 changed files with 126 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
import express from 'express';
import { getNotifiedArticles } from '../services/mongodb.js';
import { getNotifiedArticles, getArticleFacets } from '../services/mongodb.js';
import { basicAuthMiddleware } from '../middlewares/auth.js';
const router = express.Router();
@@ -43,6 +43,24 @@ router.get('/', basicAuthMiddleware, async (req, res) => {
}
});
// Obtener facets (valores únicos) para filtros (requiere autenticación obligatoria)
router.get('/facets', basicAuthMiddleware, async (req, res) => {
try {
// Obtener usuario autenticado (requerido)
const user = req.user;
const isAdmin = user.role === 'admin';
// Si no es admin, solo mostrar facets de sus propios artículos
const usernameFilter = isAdmin ? null : user.username;
const facets = await getArticleFacets(usernameFilter);
res.json(facets);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Buscar artículos en MongoDB (requiere autenticación obligatoria)
router.get('/search', basicAuthMiddleware, async (req, res) => {
try {

View File

@@ -331,6 +331,78 @@ export async function getNotifiedArticles(filter = {}) {
}
}
export async function getArticleFacets(usernameFilter = null) {
if (!db) {
return {
platforms: [],
usernames: [],
workers: []
};
}
try {
const articlesCollection = db.collection('articles');
// Construir query base
const query = {};
if (usernameFilter) {
// Si hay filtro de username, solo buscar artículos de ese usuario
query['user_info.username'] = usernameFilter;
}
// Obtener todos los artículos que coincidan con el filtro
const articles = await articlesCollection.find(query).toArray();
// Extraer valores únicos
const platformsSet = new Set();
const usernamesSet = new Set();
const workersSet = new Set();
for (const article of articles) {
// Plataforma (campo directo del artículo)
if (article.platform) {
platformsSet.add(article.platform);
}
// Username y worker_name (pueden estar en user_info o en campos directos para compatibilidad)
const userInfoList = article.user_info || [];
if (userInfoList.length > 0) {
// Estructura nueva: usar user_info
for (const userInfo of userInfoList) {
if (userInfo.username) {
usernamesSet.add(userInfo.username);
}
if (userInfo.worker_name) {
workersSet.add(userInfo.worker_name);
}
}
} else {
// Estructura antigua: usar campos directos
if (article.username) {
usernamesSet.add(article.username);
}
if (article.worker_name) {
workersSet.add(article.worker_name);
}
}
}
return {
platforms: Array.from(platformsSet).sort(),
usernames: Array.from(usernamesSet).sort(),
workers: Array.from(workersSet).sort()
};
} catch (error) {
console.error('Error obteniendo facets de artículos:', error.message);
return {
platforms: [],
usernames: [],
workers: []
};
}
}
export async function getFavorites(username = null) {
if (!db) {
return [];