no update fechas
Signed-off-by: Omar Sánchez Pizarro <omar.sanchez@pistacero.net>
This commit is contained in:
@@ -39,13 +39,7 @@ router.get('/', basicAuthMiddleware, async (req, res) => {
|
||||
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);
|
||||
const paginated = articles.slice(offset, offset + limit);
|
||||
|
||||
res.json({
|
||||
articles: paginated,
|
||||
@@ -124,16 +118,9 @@ router.get('/search', basicAuthMiddleware, async (req, res) => {
|
||||
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,
|
||||
articles: filtered,
|
||||
total: filtered.length,
|
||||
query: query,
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
@@ -281,11 +281,21 @@ export async function getNotifiedArticles(filter = {}) {
|
||||
};
|
||||
|
||||
// Si hay un user_info específico, usar sus datos
|
||||
const fallbackTime =
|
||||
article.createdAt?.getTime?.() ||
|
||||
(typeof article.createdAt === 'number' ? article.createdAt : null) ||
|
||||
article.modified_at?.getTime?.() ||
|
||||
null;
|
||||
|
||||
if (relevantUserInfo) {
|
||||
result.username = relevantUserInfo.username;
|
||||
result.worker_name = relevantUserInfo.worker_name;
|
||||
result.is_favorite = relevantUserInfo.is_favorite || false;
|
||||
result.notifiedAt = relevantUserInfo.notified_at?.getTime() || Date.now();
|
||||
// NO usar Date.now() como fallback, para no mover artículos antiguos
|
||||
result.notifiedAt =
|
||||
relevantUserInfo.notified_at?.getTime?.() ||
|
||||
(typeof relevantUserInfo.notified_at === 'number' ? relevantUserInfo.notified_at : null) ||
|
||||
fallbackTime;
|
||||
} else {
|
||||
// Sin filtro específico, mostrar el primer user_info o datos generales
|
||||
const firstUserInfo = (article.user_info || [])[0];
|
||||
@@ -293,13 +303,19 @@ export async function getNotifiedArticles(filter = {}) {
|
||||
result.username = firstUserInfo.username;
|
||||
result.worker_name = firstUserInfo.worker_name;
|
||||
result.is_favorite = firstUserInfo.is_favorite || false;
|
||||
result.notifiedAt = firstUserInfo.notified_at?.getTime() || Date.now();
|
||||
result.notifiedAt =
|
||||
firstUserInfo.notified_at?.getTime?.() ||
|
||||
(typeof firstUserInfo.notified_at === 'number' ? firstUserInfo.notified_at : null) ||
|
||||
fallbackTime;
|
||||
} else {
|
||||
// Compatibilidad con estructura antigua
|
||||
result.username = article.username;
|
||||
result.worker_name = article.worker_name;
|
||||
result.is_favorite = article.is_favorite || false;
|
||||
result.notifiedAt = article.notifiedAt?.getTime() || Date.now();
|
||||
result.notifiedAt =
|
||||
article.notifiedAt?.getTime?.() ||
|
||||
(typeof article.notifiedAt === 'number' ? article.notifiedAt : null) ||
|
||||
fallbackTime;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -331,7 +347,7 @@ export async function getFavorites(username = null) {
|
||||
|
||||
const articles = await articlesCollection
|
||||
.find(query)
|
||||
.sort({ 'user_info.notified_at': -1, createdAt: -1 })
|
||||
.sort({ createdAt: -1, modified_at: -1 })
|
||||
.toArray();
|
||||
|
||||
// Filtrar y transformar para devolver solo los favoritos relevantes
|
||||
@@ -351,7 +367,14 @@ export async function getFavorites(username = null) {
|
||||
username: userInfo.username,
|
||||
worker_name: userInfo.worker_name,
|
||||
is_favorite: true,
|
||||
notifiedAt: userInfo.notified_at?.getTime() || Date.now(),
|
||||
// NOTA: no usar Date.now() como fallback, para no mover favoritos antiguos
|
||||
notifiedAt:
|
||||
userInfo.notified_at?.getTime?.() ||
|
||||
(typeof userInfo.notified_at === 'number' ? userInfo.notified_at : null) ||
|
||||
article.createdAt?.getTime?.() ||
|
||||
(typeof article.createdAt === 'number' ? article.createdAt : null) ||
|
||||
article.modified_at?.getTime?.() ||
|
||||
null,
|
||||
expiresAt: article.expiresAt?.getTime() || null,
|
||||
});
|
||||
}
|
||||
@@ -365,7 +388,14 @@ export async function getFavorites(username = null) {
|
||||
username: userInfo.username,
|
||||
worker_name: userInfo.worker_name,
|
||||
is_favorite: true,
|
||||
notifiedAt: userInfo.notified_at?.getTime() || Date.now(),
|
||||
// NOTA: no usar Date.now() como fallback, para no mover favoritos antiguos
|
||||
notifiedAt:
|
||||
userInfo.notified_at?.getTime?.() ||
|
||||
(typeof userInfo.notified_at === 'number' ? userInfo.notified_at : null) ||
|
||||
article.createdAt?.getTime?.() ||
|
||||
(typeof article.createdAt === 'number' ? article.createdAt : null) ||
|
||||
article.modified_at?.getTime?.() ||
|
||||
null,
|
||||
expiresAt: article.expiresAt?.getTime() || null,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user