From 648bf42026716bc16bbfd6d4b8dac7d15c7786e6 Mon Sep 17 00:00:00 2001 From: marsalva Date: Sun, 8 Mar 2026 11:46:19 +0000 Subject: [PATCH] Actualizar server.js --- server.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/server.js b/server.js index b9b675d..d0a2fc4 100644 --- a/server.js +++ b/server.js @@ -3096,7 +3096,9 @@ app.post("/webhook/evolution", async (req, res) => { const svcQ = await pool.query(` SELECT s.id, s.service_ref, s.assigned_to, u.full_name as worker_name, s.is_urgent, - st.name as status_name, s.raw_data->>'scheduled_date' as cita, + st.name as status_name, + s.raw_data->>'scheduled_date' as cita, + s.raw_data->>'scheduled_time' as hora_cita, -- 👈 NUEVO: EXTRAEMOS LA HORA s.raw_data->>'Población' as poblacion, s.raw_data->>'appointment_status' as appointment_status, s.raw_data->>'requested_date' as cita_pendiente_fecha, @@ -3111,12 +3113,10 @@ app.post("/webhook/evolution", async (req, res) => { if (svcQ.rowCount > 0) { const service = svcQ.rows[0]; - // 🛑 SEMÁFORO ANTI-METRALLETA: Si ya estamos procesando este aviso, abortamos. + // 🛑 SEMÁFORO ANTI-METRALLETA if (candadosIA.has(service.id)) { - console.log(`⏳ [Bloqueo] Ignorando mensaje rápido concurrente para exp #${service.service_ref}`); return; } - // Cerramos el candado candadosIA.add(service.id); try { @@ -3129,12 +3129,10 @@ app.post("/webhook/evolution", async (req, res) => { if (checkHumanQ.rowCount > 0) { const lastMsg = checkHumanQ.rows[0]; const diffMinutos = (new Date() - new Date(lastMsg.created_at)) / (1000 * 60); - if (['admin', 'superadmin', 'operario'].includes(lastMsg.sender_role) && diffMinutos < 120) { - return; // IA Silenciada - } + if (['admin', 'superadmin', 'operario'].includes(lastMsg.sender_role) && diffMinutos < 120) return; } - // 🧠 LLAMADA A LA IA + // 🧠 LLAMADA A LA IA (Añadimos la hora aquí) const respuestaIA = await procesarConIA(ownerId, mensajeTexto, { dbId: service.id, ref: service.service_ref, @@ -3142,6 +3140,7 @@ app.post("/webhook/evolution", async (req, res) => { operario: service.worker_name, worker_id: service.assigned_to, cita: service.cita, + hora_cita: service.hora_cita, // 👈 NUEVO: SE LO PASAMOS AL CEREBRO poblacion: service.poblacion || "", is_urgent: service.is_urgent, appointment_status: service.appointment_status,