diff --git a/server.js b/server.js index c8eafe7..b7df35e 100644 --- a/server.js +++ b/server.js @@ -500,7 +500,7 @@ async function ensureInstance(instanceName) { // ========================================== -// 🔗 PORTAL PÚBLICO DEL CLIENTE (BLINDADO) +// 🔗 PORTAL PÚBLICO DEL CLIENTE (BLINDADO SQL) // ========================================== app.get("/public/portal/:token", async (req, res) => { try { @@ -513,7 +513,7 @@ app.get("/public/portal/:token", async (req, res) => { // Asegurar IDs y Teléfono para la búsqueda const ownerId = client.owner_id || client.account_id; - const clientPhone = client.phone ? `%${client.phone}%` : '%000000000%'; // Fallback por si no tiene teléfono + const clientPhone = client.phone ? `%${client.phone.replace('+34', '').trim()}%` : '%000000000%'; // 2. Obtener la Configuración de la Empresa const qConfig = await pool.query("SELECT * FROM config WHERE account_id::text = $1::text", [ownerId]); @@ -522,7 +522,7 @@ app.get("/public/portal/:token", async (req, res) => { company = qConfig.rows[0].portal_settings; } - // 3. Buscar TODOS los servicios del cliente buscando su teléfono en el JSON + // 3. Buscar TODOS los servicios del cliente (SQL Protegido contra NULLs) const qServices = await pool.query(` SELECT s.id, s.service_ref, s.is_urgent, s.raw_data, @@ -536,10 +536,10 @@ app.get("/public/portal/:token", async (req, res) => { AND s.provider != 'SYSTEM_BLOCK' AND s.raw_data IS NOT NULL AND ( - s.raw_data->>'Teléfono' LIKE $2 OR - s.raw_data->>'TELEFONOS' LIKE $2 OR - s.raw_data->>'TELEFONO' LIKE $2 OR - s.raw_data->>'Teléfono 1' LIKE $2 + COALESCE(s.raw_data->>'Teléfono', '') LIKE $2 OR + COALESCE(s.raw_data->>'TELEFONOS', '') LIKE $2 OR + COALESCE(s.raw_data->>'TELEFONO', '') LIKE $2 OR + COALESCE(s.raw_data->>'Teléfono 1', '') LIKE $2 ) ORDER BY st.is_final ASC NULLS FIRST, @@ -556,13 +556,13 @@ app.get("/public/portal/:token", async (req, res) => { scheduled_date: s.raw_data?.scheduled_date, scheduled_time: s.raw_data?.scheduled_time, assigned_worker: s.assigned_worker || 'Pendiente', - raw_data: s.raw_data // Necesario para la barra de ETA + raw_data: s.raw_data })); res.json({ ok: true, client, company, services: formattedServices }); } catch (e) { - console.error("🔥 Error 500 en Portal Cliente:", e.message); + console.error("🔥 Error 500 en Portal Cliente:", e); res.status(500).json({ ok: false, error: "Error interno del servidor" }); } });