Actualizar server.js
This commit is contained in:
61
server.js
61
server.js
@@ -1348,6 +1348,67 @@ app.put("/services/set-appointment/:id", authMiddleware, async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ==========================================
|
||||||
|
// 📞 RUTA PARA CLIENTE NO LOCALIZADO
|
||||||
|
// ==========================================
|
||||||
|
app.post("/services/not-found/:id", authMiddleware, async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
const current = await pool.query('SELECT raw_data, provider, owner_id FROM scraped_services WHERE id=$1 AND owner_id=$2', [id, req.user.accountId]);
|
||||||
|
|
||||||
|
if (current.rowCount === 0) return res.status(404).json({ok: false});
|
||||||
|
|
||||||
|
const raw = current.rows[0].raw_data || {};
|
||||||
|
|
||||||
|
// Sumamos 1 al contador de llamadas
|
||||||
|
const currentCalls = parseInt(raw.called_times || 0) + 1;
|
||||||
|
raw.called_times = currentCalls;
|
||||||
|
|
||||||
|
// Guardamos en la base de datos
|
||||||
|
await pool.query("UPDATE scraped_services SET raw_data=$1 WHERE id=$2", [JSON.stringify(raw), id]);
|
||||||
|
|
||||||
|
// Intentamos enviar el WhatsApp usando la plantilla
|
||||||
|
const phone = raw["Teléfono"] || raw["TELEFONOS"] || raw["TELEFONO"] || "";
|
||||||
|
if (phone) {
|
||||||
|
// Buscamos si existe la plantilla (tendrás que crearla en el panel de admin luego)
|
||||||
|
const tplQ = await pool.query("SELECT content FROM message_templates WHERE owner_id=$1 AND type='not_found'", [req.user.accountId]);
|
||||||
|
let text = tplQ.rowCount > 0 && tplQ.rows[0].content
|
||||||
|
? tplQ.rows[0].content
|
||||||
|
: `Hola {{NOMBRE}}, soy el técnico de {{COMPANIA}}. He intentado contactar contigo para agendar tu reparación (Exp. {{REFERENCIA}}), pero no ha sido posible. Por favor, pulsa aquí para elegir tu cita: {{ENLACE}}`;
|
||||||
|
|
||||||
|
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}%`, req.user.accountId]);
|
||||||
|
|
||||||
|
if (clientQ.rowCount > 0) {
|
||||||
|
token = clientQ.rows[0].portal_token;
|
||||||
|
} else {
|
||||||
|
const newToken = crypto.randomBytes(6).toString('hex');
|
||||||
|
const insertC = await pool.query(
|
||||||
|
"INSERT INTO clients (owner_id, full_name, phone, addresses, portal_token) VALUES ($1, $2, $3, '[]', $4) RETURNING portal_token",
|
||||||
|
[req.user.accountId, raw["Nombre Cliente"] || "Cliente", phone, newToken]
|
||||||
|
);
|
||||||
|
token = insertC.rows[0].portal_token;
|
||||||
|
}
|
||||||
|
|
||||||
|
const linkMagico = `https://portal.integrarepara.es/?token=${token}&service=${id}`;
|
||||||
|
|
||||||
|
text = text.replace(/{{NOMBRE}}/g, raw["Nombre Cliente"] || raw["CLIENTE"] || "Cliente");
|
||||||
|
text = text.replace(/{{COMPANIA}}/g, raw["Compañía"] || raw["COMPAÑIA"] || "su Aseguradora");
|
||||||
|
text = text.replace(/{{REFERENCIA}}/g, current.rows[0].service_ref || id);
|
||||||
|
text = text.replace(/{{ENLACE}}/g, linkMagico);
|
||||||
|
|
||||||
|
const userQ = await pool.query("SELECT wa_settings FROM users WHERE id=$1", [req.user.accountId]);
|
||||||
|
const settings = userQ.rows[0]?.wa_settings || {};
|
||||||
|
const useDelay = settings.wa_delay_enabled !== false;
|
||||||
|
|
||||||
|
sendWhatsAppAuto(phone, text, `cliente_${req.user.accountId}`, useDelay).catch(console.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({ ok: true, called_times: currentCalls });
|
||||||
|
} catch (e) { res.status(500).json({ ok: false }); }
|
||||||
|
});
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 📝 RUTAS DE PLANTILLAS DE MENSAJES
|
// 📝 RUTAS DE PLANTILLAS DE MENSAJES
|
||||||
// ==========================================
|
// ==========================================
|
||||||
|
|||||||
Reference in New Issue
Block a user