Actualizar server.js
This commit is contained in:
18
server.js
18
server.js
@@ -3094,11 +3094,12 @@ app.post("/webhook/evolution", async (req, res) => {
|
||||
const ownerId = instanceName.split("_")[1];
|
||||
const cleanPhone = telefonoCliente.slice(-9);
|
||||
|
||||
// 🔍 BUSCAMOS EL EXPEDIENTE ACTIVO MÁS RECIENTE (Ignorando finalizados/anulados)
|
||||
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,
|
||||
s.raw_data->>'scheduled_time' as hora_cita, -- 👈 NUEVO: EXTRAEMOS LA HORA
|
||||
s.raw_data->>'scheduled_time' as hora_cita,
|
||||
s.raw_data->>'Población' as poblacion,
|
||||
s.raw_data->>'appointment_status' as appointment_status,
|
||||
s.raw_data->>'requested_date' as cita_pendiente_fecha,
|
||||
@@ -3106,19 +3107,20 @@ app.post("/webhook/evolution", async (req, res) => {
|
||||
FROM scraped_services s
|
||||
LEFT JOIN users u ON s.assigned_to = u.id
|
||||
LEFT JOIN service_statuses st ON (s.raw_data->>'status_operativo')::text = st.id::text
|
||||
WHERE s.owner_id = $1 AND s.status != 'archived' AND s.raw_data::text ILIKE $2
|
||||
WHERE s.owner_id = $1
|
||||
AND s.status != 'archived'
|
||||
AND s.raw_data::text ILIKE $2
|
||||
-- 👇 MAGIA: Excluimos de la búsqueda los estados muertos
|
||||
AND (st.name IS NULL OR (st.name NOT ILIKE '%finalizado%' AND st.name NOT ILIKE '%anulado%' AND st.name NOT ILIKE '%desasignado%'))
|
||||
ORDER BY s.created_at DESC LIMIT 1
|
||||
`, [ownerId, `%${cleanPhone}%`]);
|
||||
|
||||
if (svcQ.rowCount > 0) {
|
||||
const service = svcQ.rows[0];
|
||||
// Si entra aquí, es 100% seguro que el expediente está vivo y la IA puede hablar.
|
||||
|
||||
|
||||
|
||||
// 🛑 NUEVO ESCUDO: Bloquear IA si el estado es finalizado, anulado o desasignado
|
||||
const nombreEstado = (service.status_name || "").toLowerCase();
|
||||
if (nombreEstado.includes('finalizado') || nombreEstado.includes('anulado') || nombreEstado.includes('desasignado')) {
|
||||
console.log(`🛑 [IA SILENCIADA] El cliente escribió, pero el exp #${service.service_ref} está ${service.status_name}.`);
|
||||
return;
|
||||
}
|
||||
candadosIA.add(service.id);
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user