mongodb
Signed-off-by: Omar Sánchez Pizarro <omar.sanchez@pistacero.net>
This commit is contained in:
@@ -1,16 +1,50 @@
|
||||
import express from 'express';
|
||||
import { getNotifiedArticles } from '../services/redis.js';
|
||||
import { getNotifiedArticles } from '../services/mongodb.js';
|
||||
import { basicAuthMiddleware } from '../middlewares/auth.js';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Obtener artículos notificados
|
||||
router.get('/', async (req, res) => {
|
||||
router.delete('/', async (req, res) => {
|
||||
try {
|
||||
const articles = await getNotifiedArticles();
|
||||
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;
|
||||
|
||||
const sorted = articles.sort((a, b) => b.notifiedAt - a.notifiedAt);
|
||||
// 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({
|
||||
@@ -24,16 +58,34 @@ router.get('/', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Buscar artículos en Redis
|
||||
router.get('/search', async (req, res) => {
|
||||
// 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();
|
||||
const allArticles = await getNotifiedArticles(filter);
|
||||
|
||||
// Filtrar artículos que coincidan con la búsqueda
|
||||
const filtered = allArticles.filter(article => {
|
||||
@@ -61,11 +113,23 @@ router.get('/search', async (req, res) => {
|
||||
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) => b.notifiedAt - a.notifiedAt);
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user