From f59e079f4e0d4a6dc427a828143bd3d0c87463b1 Mon Sep 17 00:00:00 2001 From: marsalva Date: Fri, 20 Feb 2026 21:51:19 +0000 Subject: [PATCH] Actualizar server.js --- server.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/server.js b/server.js index cbc2edc..b57eda3 100644 --- a/server.js +++ b/server.js @@ -635,13 +635,15 @@ async function triggerWhatsAppEvent(ownerId, serviceId, eventType) { if (!settings[checkSwitch]) return; // Si el botón está apagado, salimos // 2. Buscamos qué plantilla corresponde a este evento - const tplTypeMap = { - 'wa_evt_welcome': 'welcome', - 'wa_evt_date': 'appointment', // Plantilla: Cita Creada - 'wa_evt_update': 'update', // Plantilla: Modificación de Servicio - 'wa_evt_onway': 'on_way', - 'wa_evt_survey': 'survey' - }; + const tplTypeMap = { + 'wa_evt_welcome': 'welcome', + 'wa_evt_assigned': 'assigned', // <--- NUEVO: Asignado a operario + 'wa_evt_date': 'appointment', + 'wa_evt_update': 'update', + 'wa_evt_onway': 'on_way', + 'wa_evt_survey': 'survey' +}; + const tplQ = await pool.query("SELECT content FROM message_templates WHERE owner_id=$1 AND type=$2", [ownerId, tplTypeMap[eventType]]); if (tplQ.rowCount === 0 || !tplQ.rows[0].content) return; let text = tplQ.rows[0].content; @@ -964,6 +966,17 @@ app.put("/services/set-appointment/:id", authMiddleware, async (req, res) => { const statusQ = await pool.query("SELECT name FROM service_statuses WHERE id=$1", [status_operativo]); const stName = statusQ.rows[0]?.name.toLowerCase() || ""; + // ... dentro de la lógica del Motor en set-appointment ... +const stName = statusQ.rows[0]?.name.toLowerCase() || ""; + +if (stName.includes('asignado')) { + // Si el estado contiene "asignado", disparamos el nuevo evento + triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_assigned'); +} else if (stName.includes('citado') && newDate !== "") { + // ... resto de lógica que ya tenemos ... + + + // REGLA ESTRICTA: ¿Es estado Citado y tiene fecha? if (stName.includes('citado') && newDate !== "") { if (oldDate === "") {