Actualizar server.js

This commit is contained in:
2026-03-08 11:46:19 +00:00
parent bd70efd8fb
commit 648bf42026

View File

@@ -3096,7 +3096,9 @@ app.post("/webhook/evolution", async (req, res) => {
const svcQ = await pool.query(` const svcQ = await pool.query(`
SELECT s.id, s.service_ref, s.assigned_to, u.full_name as worker_name, s.is_urgent, 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->>'Población' as poblacion,
s.raw_data->>'appointment_status' as appointment_status, s.raw_data->>'appointment_status' as appointment_status,
s.raw_data->>'requested_date' as cita_pendiente_fecha, s.raw_data->>'requested_date' as cita_pendiente_fecha,
@@ -3111,12 +3113,10 @@ app.post("/webhook/evolution", async (req, res) => {
if (svcQ.rowCount > 0) { if (svcQ.rowCount > 0) {
const service = svcQ.rows[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)) { if (candadosIA.has(service.id)) {
console.log(`⏳ [Bloqueo] Ignorando mensaje rápido concurrente para exp #${service.service_ref}`);
return; return;
} }
// Cerramos el candado
candadosIA.add(service.id); candadosIA.add(service.id);
try { try {
@@ -3129,12 +3129,10 @@ app.post("/webhook/evolution", async (req, res) => {
if (checkHumanQ.rowCount > 0) { if (checkHumanQ.rowCount > 0) {
const lastMsg = checkHumanQ.rows[0]; const lastMsg = checkHumanQ.rows[0];
const diffMinutos = (new Date() - new Date(lastMsg.created_at)) / (1000 * 60); const diffMinutos = (new Date() - new Date(lastMsg.created_at)) / (1000 * 60);
if (['admin', 'superadmin', 'operario'].includes(lastMsg.sender_role) && diffMinutos < 120) { if (['admin', 'superadmin', 'operario'].includes(lastMsg.sender_role) && diffMinutos < 120) return;
return; // IA Silenciada
}
} }
// 🧠 LLAMADA A LA IA // 🧠 LLAMADA A LA IA (Añadimos la hora aquí)
const respuestaIA = await procesarConIA(ownerId, mensajeTexto, { const respuestaIA = await procesarConIA(ownerId, mensajeTexto, {
dbId: service.id, dbId: service.id,
ref: service.service_ref, ref: service.service_ref,
@@ -3142,6 +3140,7 @@ app.post("/webhook/evolution", async (req, res) => {
operario: service.worker_name, operario: service.worker_name,
worker_id: service.assigned_to, worker_id: service.assigned_to,
cita: service.cita, cita: service.cita,
hora_cita: service.hora_cita, // 👈 NUEVO: SE LO PASAMOS AL CEREBRO
poblacion: service.poblacion || "", poblacion: service.poblacion || "",
is_urgent: service.is_urgent, is_urgent: service.is_urgent,
appointment_status: service.appointment_status, appointment_status: service.appointment_status,