146 lines
4.8 KiB
JavaScript
146 lines
4.8 KiB
JavaScript
import express from 'express';
|
|
import { getNotifiedArticles } from '../services/mongodb.js';
|
|
import { basicAuthMiddleware } from '../middlewares/auth.js';
|
|
|
|
const router = express.Router();
|
|
|
|
router.delete('/', async (req, res) => {
|
|
try {
|
|
const count = await clearAllArticles();
|
|
res.json({ success: true, message: `Todos los artículos eliminados: ${count} artículos borrados`, count });
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// Obtener artículos notificados (requiere autenticación obligatoria)
|
|
router.get('/', basicAuthMiddleware, async (req, res) => {
|
|
try {
|
|
// Obtener usuario autenticado (requerido)
|
|
const user = req.user;
|
|
const isAdmin = user.role === 'admin';
|
|
|
|
// Construir filtro
|
|
const filter = {};
|
|
|
|
// Si no es admin, solo mostrar sus artículos
|
|
if (!isAdmin) {
|
|
filter.username = user.username;
|
|
} else if (req.query.username) {
|
|
// Admin puede filtrar por username
|
|
filter.username = req.query.username;
|
|
}
|
|
|
|
if (req.query.worker_name) filter.worker_name = req.query.worker_name;
|
|
if (req.query.platform) filter.platform = req.query.platform;
|
|
|
|
const articles = await getNotifiedArticles(filter);
|
|
|
|
const limit = parseInt(req.query.limit) || 100;
|
|
const offset = parseInt(req.query.offset) || 0;
|
|
|
|
// Los artículos ya vienen ordenados de MongoDB, pero asegurémonos
|
|
const sorted = articles.sort((a, b) => {
|
|
const aTime = typeof a.notifiedAt === 'number' ? a.notifiedAt : new Date(a.notifiedAt).getTime();
|
|
const bTime = typeof b.notifiedAt === 'number' ? b.notifiedAt : new Date(b.notifiedAt).getTime();
|
|
return bTime - aTime;
|
|
});
|
|
const paginated = sorted.slice(offset, offset + limit);
|
|
|
|
res.json({
|
|
articles: paginated,
|
|
total: articles.length,
|
|
limit,
|
|
offset,
|
|
});
|
|
} 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 {
|
|
const query = req.query.q || '';
|
|
if (!query.trim()) {
|
|
return res.json({ articles: [], total: 0 });
|
|
}
|
|
|
|
// Obtener usuario autenticado (requerido)
|
|
const user = req.user;
|
|
const isAdmin = user.role === 'admin';
|
|
|
|
// Construir filtro adicional si se proporciona
|
|
const filter = {};
|
|
|
|
// Si no es admin, solo buscar sus artículos
|
|
if (!isAdmin) {
|
|
filter.username = user.username;
|
|
} else if (req.query.username) {
|
|
// Admin puede filtrar por username
|
|
filter.username = req.query.username;
|
|
}
|
|
|
|
if (req.query.worker_name) filter.worker_name = req.query.worker_name;
|
|
if (req.query.platform) filter.platform = req.query.platform;
|
|
|
|
const searchTerm = query.toLowerCase().trim();
|
|
const allArticles = await getNotifiedArticles(filter);
|
|
|
|
// Filtrar artículos que coincidan con la búsqueda
|
|
const filtered = allArticles.filter(article => {
|
|
// Buscar en título
|
|
const title = (article.title || '').toLowerCase();
|
|
if (title.includes(searchTerm)) return true;
|
|
|
|
// Buscar en descripción
|
|
const description = (article.description || '').toLowerCase();
|
|
if (description.includes(searchTerm)) return true;
|
|
|
|
// Buscar en localidad
|
|
const location = (article.location || '').toLowerCase();
|
|
if (location.includes(searchTerm)) return true;
|
|
|
|
// Buscar en precio (como número o texto)
|
|
const price = String(article.price || '').toLowerCase();
|
|
if (price.includes(searchTerm)) return true;
|
|
|
|
// Buscar en plataforma
|
|
const platform = (article.platform || '').toLowerCase();
|
|
if (platform.includes(searchTerm)) return true;
|
|
|
|
// Buscar en ID
|
|
const id = String(article.id || '').toLowerCase();
|
|
if (id.includes(searchTerm)) return true;
|
|
|
|
// Buscar en username
|
|
const username = (article.username || '').toLowerCase();
|
|
if (username.includes(searchTerm)) return true;
|
|
|
|
// Buscar en worker_name
|
|
const worker_name = (article.worker_name || '').toLowerCase();
|
|
if (worker_name.includes(searchTerm)) return true;
|
|
|
|
return false;
|
|
});
|
|
|
|
// Ordenar por fecha de notificación (más recientes primero)
|
|
const sorted = filtered.sort((a, b) => {
|
|
const aTime = typeof a.notifiedAt === 'number' ? a.notifiedAt : new Date(a.notifiedAt).getTime();
|
|
const bTime = typeof b.notifiedAt === 'number' ? b.notifiedAt : new Date(b.notifiedAt).getTime();
|
|
return bTime - aTime;
|
|
});
|
|
|
|
res.json({
|
|
articles: sorted,
|
|
total: sorted.length,
|
|
query: query,
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
export default router;
|
|
|