Anadir variable {{PORTAL}} (enlace magico al portal del cliente) a los recordatorios automaticos

This commit is contained in:
2026-08-23 16:24:27 +00:00
parent f3e53d4898
commit ac48fe0965

View File

@@ -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) {