Actualizar server.js
This commit is contained in:
17
server.js
17
server.js
@@ -653,9 +653,24 @@ async function triggerWhatsAppEvent(ownerId, serviceId, eventType) {
|
||||
const phone = raw["Teléfono"] || raw["TELEFONO"] || "";
|
||||
if (!phone) return false; // Sin teléfono = No enviado
|
||||
|
||||
// 4. Buscamos el token del portal cliente (o lo creamos si no existe)
|
||||
const phoneClean = phone.replace('+34', '').trim();
|
||||
let token = "ERROR";
|
||||
const clientQ = await pool.query("SELECT portal_token FROM clients WHERE phone LIKE $1 AND owner_id=$2 LIMIT 1", [`%${phoneClean}%`, ownerId]);
|
||||
const token = clientQ.rowCount > 0 ? clientQ.rows[0].portal_token : "ERROR";
|
||||
|
||||
if (clientQ.rowCount > 0) {
|
||||
token = clientQ.rows[0].portal_token;
|
||||
} else {
|
||||
// El cliente no existe en la agenda aún. Lo creamos al vuelo.
|
||||
const cName = raw["Nombre Cliente"] || raw["CLIENTE"] || "Asegurado";
|
||||
const cAddr = raw["Dirección"] || raw["DOMICILIO"] || "";
|
||||
const insertC = await pool.query(
|
||||
"INSERT INTO clients (owner_id, full_name, phone, addresses) VALUES ($1, $2, $3, $4) RETURNING portal_token",
|
||||
[ownerId, cName, phone, JSON.stringify([cAddr])]
|
||||
);
|
||||
token = insertC.rows[0].portal_token;
|
||||
}
|
||||
|
||||
const linkMagico = `https://portal.integrarepara.es/?token=${token}&service=${serviceId}`;
|
||||
|
||||
let fechaLimpia = raw["scheduled_date"] || "la fecha acordada";
|
||||
|
||||
Reference in New Issue
Block a user