From e23e6aa5c15b0ae6ef72e8d9c8ad86ae7f6d03e4 Mon Sep 17 00:00:00 2001 From: marsalva Date: Sun, 8 Mar 2026 22:49:31 +0000 Subject: [PATCH] Actualizar server.js --- server.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/server.js b/server.js index 1942727..444f2b4 100644 --- a/server.js +++ b/server.js @@ -1819,10 +1819,16 @@ app.put("/services/set-appointment/:id", authMiddleware, async (req, res) => { // 🚀 RESPONDEMOS AL NAVEGADOR INMEDIATAMENTE (La ventana se cierra al instante) res.json({ ok: true }); - // 2. MAGIA: TAREAS EN SEGUNDO PLANO (El WhatsApp tarda lo que tenga que tardar, sin bloquear) + // 2. MAGIA: TAREAS EN SEGUNDO PLANO (El WhatsApp tarda lo que tenga que tardar, sin bloquear) (async () => { try { - if (stName.includes('asignado')) { + // 👇 Detectamos cambios en la fecha y el estado para no hacer disparos falsos + const statusChanged = status_operativo !== rawActual.status_operativo; + const oldDate = rawActual.scheduled_date || ""; + const dateChanged = newDate !== "" && newDate !== oldDate; + + if (statusChanged && stName.includes('asignado')) { + triggerHomeServeRobot(req.user.accountId, id, 'assign').catch(console.error); const waEnviadoExito = await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_assigned'); if (waEnviadoExito) { const estadoEsperando = await pool.query("SELECT id FROM service_statuses WHERE owner_id=$1 AND name='Esperando al Cliente' LIMIT 1", [req.user.accountId]); @@ -1850,12 +1856,16 @@ app.put("/services/set-appointment/:id", authMiddleware, async (req, res) => { updatedRawData.assigned_to_name = null; await pool.query('UPDATE scraped_services SET raw_data = $1, assigned_to = null WHERE id = $2 AND owner_id = $3', [JSON.stringify(updatedRawData), id, req.user.accountId]); } - else if (stName.includes('citado') && newDate !== "" && date !== undefined) { + + // 🟢 NUEVO DISPARADOR ROBOT CITA (Se lanza si cambia estado O si cambia fecha) + else if ((statusChanged && stName.includes('citado') && newDate !== "") || (dateChanged && stName.includes('citado'))) { + triggerHomeServeRobot(req.user.accountId, id, 'date').catch(console.error); - const oldDate = rawActual.scheduled_date || ""; + if (oldDate === "") await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_date'); else if (oldDate !== newDate) await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_update'); - } else if (stName.includes('camino')) { + } + else if (stName.includes('camino')) { await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_onway'); } else if (stName.includes('finalizado') || stName.includes('terminado')) { if (!skip_survey) {