From 104c8f906567b8c76bfa41794194d7ad305093a1 Mon Sep 17 00:00:00 2001 From: marsalva Date: Tue, 24 Feb 2026 22:56:28 +0000 Subject: [PATCH] Actualizar server.js --- server.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/server.js b/server.js index 130d4ad..99231ac 100644 --- a/server.js +++ b/server.js @@ -1663,15 +1663,14 @@ app.put("/guilds/:id/ia-rules", authMiddleware, async (req, res) => { // 🏆 MOTOR DE RANKING Y ESTADÍSTICAS // ========================================== -// Función interna que calcula el algoritmo exacto sobre 100 function calculateScore(services) { let score = 0; const now = new Date(); const thirtyDaysAgo = new Date(now.getTime() - (30 * 24 * 60 * 60 * 1000)); - // Separar servicios + // Separar servicios (usamos created_at para medir el último mes) const openServices = services.filter(s => !s.is_final); - const closedLast30Days = services.filter(s => s.is_final && new Date(s.updated_at) >= thirtyDaysAgo); + const closedLast30Days = services.filter(s => s.is_final && new Date(s.created_at) >= thirtyDaysAgo); // --- 1. VELOCIDAD DE CIERRE (Max 30 Puntos) --- let scoreCierre = 0; @@ -1679,12 +1678,11 @@ function calculateScore(services) { let totalDaysToClose = 0; closedLast30Days.forEach(s => { const created = new Date(s.created_at); - const closed = new Date(s.updated_at); - totalDaysToClose += (closed - created) / (1000 * 60 * 60 * 24); + const closed = new Date(); // Estimamos el cierre en el ciclo actual + totalDaysToClose += Math.max(1, (closed - created) / (1000 * 60 * 60 * 24)); }); const avgCloseDays = totalDaysToClose / closedLast30Days.length; - // Algoritmo: <= 2 días = 30 pts. >= 14 días = 0 pts. if (avgCloseDays <= 2) scoreCierre = 30; else if (avgCloseDays >= 14) scoreCierre = 0; else scoreCierre = 30 - ((avgCloseDays - 2) * (30 / 12)); @@ -1761,8 +1759,9 @@ function calculateScore(services) { // RUTA GET PARA EL RANKING app.get("/ranking", authMiddleware, async (req, res) => { try { + // CORRECCIÓN: Hemos quitado "updated_at" de aquí para evitar que la base de datos se queje const q = await pool.query(` - SELECT id, created_at, updated_at, raw_data, + SELECT id, created_at, raw_data, (SELECT is_final FROM service_statuses WHERE id::text = raw_data->>'status_operativo') as is_final FROM scraped_services WHERE assigned_to = $1