From ac48fe0965afa14683d9482eb5ac0b6660ac40eb Mon Sep 17 00:00:00 2001 From: marsalva Date: Sun, 23 Aug 2026 16:24:27 +0000 Subject: [PATCH] Anadir variable {{PORTAL}} (enlace magico al portal del cliente) a los recordatorios automaticos --- server.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index 0314ca2..e13549e 100644 --- a/server.js +++ b/server.js @@ -6034,6 +6034,9 @@ async function enviarRecordatorioAutomatizacion(client, svc, tipo, testMode) { if (tplQ.rowCount === 0 || !tplQ.rows[0].content) return; let text = tplQ.rows[0].content; + const rawPhone = raw["Teléfono"] || raw["TELEFONO"] || raw["TELEFONOS"] || ""; + const cleanPhone = extractValidPhone(rawPhone); + const nombre = raw["Nombre Cliente"] || raw["CLIENTE"] || raw["Nombre"] || "Cliente"; const direccion = raw["Dirección"] || raw["DOMICILIO"] || ""; text = text.replace(/{{NOMBRE}}/g, nombre); @@ -6042,8 +6045,27 @@ async function enviarRecordatorioAutomatizacion(client, svc, tipo, testMode) { text = text.replace(/{{HORA}}/g, raw["scheduled_time"] || ""); text = text.replace(/{{REFERENCIA}}/g, svc.service_ref || raw["Referencia"] || raw["Nº Siniestro"] || String(svc.id)); - const rawPhone = raw["Teléfono"] || raw["TELEFONO"] || raw["TELEFONOS"] || ""; - const cleanPhone = extractValidPhone(rawPhone); + // 🔗 {{PORTAL}}: enlace mágico al portal del cliente (siempre el real, aunque esté en modo prueba) + if (text.includes('{{PORTAL}}')) { + let portalToken = null; + if (cleanPhone.length >= 9) { + const clientQ = await client.query( + "SELECT portal_token FROM clients WHERE phone LIKE $1 AND owner_id=$2 LIMIT 1", + [`%${cleanPhone}%`, svc.owner_id] + ); + if (clientQ.rowCount > 0) { + portalToken = clientQ.rows[0].portal_token; + } else { + const insertC = await client.query( + "INSERT INTO clients (owner_id, full_name, phone, addresses) VALUES ($1, $2, $3, $4) RETURNING portal_token", + [svc.owner_id, nombre, "34" + cleanPhone, JSON.stringify([direccion])] + ); + portalToken = insertC.rows[0].portal_token; + } + } + const linkPortal = portalToken ? `https://portal.integrarepara.es/?token=${portalToken}&service=${svc.id}` : ""; + text = text.replace(/{{PORTAL}}/g, linkPortal); + } let finalPhone; if (testMode) {