no update fechas

Signed-off-by: Omar Sánchez Pizarro <omar.sanchez@pistacero.net>
This commit is contained in:
Omar Sánchez Pizarro
2026-01-20 12:29:22 +01:00
parent 63481b500d
commit a65bedf24d
5 changed files with 21 additions and 474 deletions

View File

@@ -15,9 +15,6 @@ let config = null;
// Duración de sesión en milisegundos (24 horas)
const SESSION_DURATION = 24 * 60 * 60 * 1000;
// TTL de artículos notificados en milisegundos (7 días)
const NOTIFIED_ARTICLE_TTL = 7 * 24 * 60 * 60 * 1000;
// Inicializar MongoDB si está configurado
export async function initMongoDB() {
try {
@@ -105,7 +102,6 @@ async function createIndexes() {
// Índices de compatibilidad con estructura antigua
await db.collection('articles').createIndex({ username: 1 });
await db.collection('articles').createIndex({ worker_name: 1 });
await db.collection('articles').createIndex({ notifiedAt: -1 });
console.log('✅ Índices de MongoDB creados');
} catch (error) {
@@ -704,107 +700,6 @@ export async function deleteUserSessions(username) {
}
// Funciones para artículos
export async function saveArticle(articleData) {
if (!db) {
throw new Error('MongoDB no está disponible');
}
try {
const articlesCollection = db.collection('articles');
const expiresAt = new Date(Date.now() + NOTIFIED_ARTICLE_TTL);
// Extraer datos del artículo (sin user_info)
const {
platform,
id,
username,
worker_name,
...articleFields
} = articleData;
// Buscar artículo existente
const existing = await articlesCollection.findOne({ platform, id });
// Preparar user_info para este usuario/worker
const userInfoEntry = {
username: username || null,
worker_name: worker_name || null,
notified: true,
notified_at: new Date(),
is_favorite: false,
};
if (existing) {
// Artículo existe, actualizar o añadir user_info
const existingUserInfo = existing.user_info || [];
// Buscar si ya existe un user_info para este usuario
const existingUserInfoIndex = existingUserInfo.findIndex(
ui => ui.username === username
);
if (existingUserInfoIndex >= 0) {
// Actualizar user_info existente pero mantener notified_at original
existingUserInfo[existingUserInfoIndex] = {
...existingUserInfo[existingUserInfoIndex],
worker_name: worker_name || existingUserInfo[existingUserInfoIndex].worker_name,
notified: true,
// NO actualizar notified_at, mantener el valor existente
};
} else {
// Añadir nuevo user_info
existingUserInfo.push(userInfoEntry);
}
// Solo actualizar precio si es diferente, no actualizar fechas de notificación
const existingPrice = existing.price;
const newPrice = articleFields.price;
if (existingPrice === newPrice) {
// Si el precio es el mismo, solo actualizar user_info
await articlesCollection.updateOne(
{ platform, id },
{
$set: {
user_info: existingUserInfo,
}
}
);
} else {
// Precio diferente, actualizar artículo completo
// Mantener updatedAt para saber cuándo cambió el precio
// pero NO actualizar notified_at (ya se mantiene arriba)
await articlesCollection.updateOne(
{ platform, id },
{
$set: {
...articleFields,
user_info: existingUserInfo,
expiresAt,
updatedAt: new Date(),
}
}
);
}
} else {
// Artículo nuevo, crear con user_info
await articlesCollection.insertOne({
platform,
id,
...articleFields,
user_info: [userInfoEntry],
expiresAt,
createdAt: new Date(),
updatedAt: new Date(),
});
}
return true;
} catch (error) {
console.error('Error guardando artículo:', error.message);
throw error;
}
}
export async function getArticle(platform, id) {
if (!db) {
return null;