Actualizar server.js
This commit is contained in:
29
server.js
29
server.js
@@ -3944,7 +3944,7 @@ app.post("/webhook/evolution", async (req, res) => {
|
|||||||
const ownerId = instanceName.split("_")[1];
|
const ownerId = instanceName.split("_")[1];
|
||||||
const cleanPhone = telefonoCliente.slice(-9);
|
const cleanPhone = telefonoCliente.slice(-9);
|
||||||
|
|
||||||
// 🔍 BUSCAMOS EL EXPEDIENTE ACTIVO MÁS RECIENTE
|
// 🔍 BUSCAMOS EL EXPEDIENTE ACTIVO MÁS RECIENTE
|
||||||
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,
|
st.name as status_name,
|
||||||
@@ -4032,33 +4032,6 @@ app.post("/webhook/evolution", async (req, res) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (candadosIA.has(service.id)) return;
|
|
||||||
candadosIA.add(service.id);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 🛑 COMPROBAR SI LA HEMOS PAUSADO CON EL SEMÁFORO ROJO
|
|
||||||
if (service.ia_paused === 'true' || service.ia_paused === true) {
|
|
||||||
console.log(`🤫 [IA MUTEADA] El cliente ha hablado, pero la IA está en semáforo rojo para ${service.service_ref}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 🛡️ VERIFICAR INTERVENCIÓN HUMANA Y APLICAR 2 HORAS DE SILENCIO (120 min)
|
|
||||||
const checkHumanQ = await pool.query(`
|
|
||||||
SELECT sender_role, created_at FROM service_communications
|
|
||||||
WHERE scraped_id = $1 ORDER BY created_at DESC LIMIT 1
|
|
||||||
`, [service.id]);
|
|
||||||
|
|
||||||
if (checkHumanQ.rowCount > 0) {
|
|
||||||
const lastMsg = checkHumanQ.rows[0];
|
|
||||||
const diffMinutos = (new Date() - new Date(lastMsg.created_at)) / (1000 * 60);
|
|
||||||
|
|
||||||
// 🛑 ESCUDO ACTIVADO
|
|
||||||
if (['admin', 'superadmin', 'operario'].includes(lastMsg.sender_role) && diffMinutos < 120) {
|
|
||||||
console.log(`🛡️ [ESCUDO IA] Silenciando a la IA porque un humano habló hace ${Math.round(diffMinutos)} minutos.`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 🧠 LLAMADA A LA IA
|
// 🧠 LLAMADA A LA IA
|
||||||
const respuestaIA = await procesarConIA(ownerId, mensajeTexto, {
|
const respuestaIA = await procesarConIA(ownerId, mensajeTexto, {
|
||||||
dbId: service.id,
|
dbId: service.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user