Actualizar server.js

This commit is contained in:
2026-03-04 08:31:16 +00:00
parent 2bf09791f7
commit c52bab56e5

View File

@@ -402,10 +402,12 @@ app.get("/public/portal/:token", async (req, res) => {
logo: userData.company_logo || null logo: userData.company_logo || null
}; };
// 3. Obtenemos los servicios. // 3. TELÉFONO SEGURO E ID SEGURO (Esto evita el Error 500 y el fallo de enlace)
// AHORA BUSCAMOS POR client_id. Si el client_id es nulo (expedientes viejos), buscamos por teléfono en el JSON. let cleanPhoneToMatch = String(client.phone || "").replace(/\D/g, "").slice(-9);
const cleanPhoneToMatch = String(client.phone || "").replace(/\D/g, "").slice(-9); if (cleanPhoneToMatch.length < 8) cleanPhoneToMatch = "NO_VALIDO_123"; // Seguridad
const requestedServiceId = req.query.service || null; // <-- Capturamos el ID del enlace si existe
// Si hay ID en el enlace lo usamos, si no, mandamos un "0" para que no explote la DB
const requestedServiceId = req.query.service ? String(req.query.service) : "0";
const qServices = await pool.query(` const qServices = await pool.query(`
SELECT SELECT
@@ -420,7 +422,7 @@ app.get("/public/portal/:token", async (req, res) => {
WHERE s.owner_id = $1 WHERE s.owner_id = $1
AND s.provider != 'SYSTEM_BLOCK' AND s.provider != 'SYSTEM_BLOCK'
AND ( AND (
s.id::text = $4 -- <-- LA MAGIA: Forzamos a que traiga el expediente del enlace s.id::text = $4
OR s.client_id = $2 OR s.client_id = $2
OR (s.client_id IS NULL AND REPLACE(s.raw_data->>'Teléfono', ' ', '') LIKE $3) OR (s.client_id IS NULL AND REPLACE(s.raw_data->>'Teléfono', ' ', '') LIKE $3)
OR (s.client_id IS NULL AND REPLACE(s.raw_data->>'TELEFONO', ' ', '') LIKE $3) OR (s.client_id IS NULL AND REPLACE(s.raw_data->>'TELEFONO', ' ', '') LIKE $3)
@@ -444,11 +446,10 @@ app.get("/public/portal/:token", async (req, res) => {
}; };
}); });
// Incluso si no hay servicios, devolvemos el portal vacío para que no dé error 404
res.json({ ok: true, client: { name: client.full_name }, company, services: formattedServices }); res.json({ ok: true, client: { name: client.full_name }, company, services: formattedServices });
} catch (e) { } catch (e) {
console.error("🔥 ERROR EN PORTAL:", e.message); console.error("🔥 ERROR EN PORTAL:", e);
res.status(500).json({ ok: false, error: "Error interno" }); res.status(500).json({ ok: false, error: "Error interno" });
} }
}); });