Actualizar server.js
This commit is contained in:
26
server.js
26
server.js
@@ -384,7 +384,7 @@ async function requirePlan(req, res, next, feature) {
|
||||
app.get("/public/portal/:token", async (req, res) => {
|
||||
try {
|
||||
const { token } = req.params;
|
||||
const serviceIdParam = req.query.service; // Extraemos el ID si lo mandan
|
||||
const serviceIdParam = req.query.service;
|
||||
|
||||
// 1. Buscamos al cliente por su token
|
||||
const qClient = await pool.query("SELECT * FROM clients WHERE portal_token = $1 LIMIT 1", [token]);
|
||||
@@ -393,18 +393,21 @@ app.get("/public/portal/:token", async (req, res) => {
|
||||
const client = qClient.rows[0];
|
||||
const ownerId = client.owner_id;
|
||||
|
||||
// 2. Buscamos los datos de la empresa para personalizar el portal
|
||||
// 2. Buscamos los datos de la empresa
|
||||
const qConfig = await pool.query("SELECT full_name, company_logo FROM users WHERE id = $1", [ownerId]);
|
||||
const company = {
|
||||
name: qConfig.rows[0]?.full_name || "IntegraRepara",
|
||||
logo: qConfig.rows[0]?.company_logo || null
|
||||
};
|
||||
|
||||
// 3. OBTENER SERVICIOS (Lógica blindada anti-crashes)
|
||||
// 3. TELÉFONO SEGURO
|
||||
let phoneMatch = String(client.phone || "").replace(/\D/g, "").slice(-9);
|
||||
if (phoneMatch.length < 8) phoneMatch = "NO_VALIDO_123";
|
||||
|
||||
// 4. CONSTRUIMOS LA BÚSQUEDA (Doble vía para evitar colapsos)
|
||||
let qServices;
|
||||
|
||||
// Si nos pasan un ID de servicio en la URL, sacamos ESE servicio exacto
|
||||
if (serviceIdParam && serviceIdParam !== "undefined" && serviceIdParam !== "null") {
|
||||
if (serviceIdParam && !isNaN(parseInt(serviceIdParam))) {
|
||||
// Si nos envían el ID por URL, buscamos ESE servicio exacto
|
||||
qServices = await pool.query(`
|
||||
SELECT s.id, s.service_ref, s.is_urgent, s.raw_data, s.created_at, s.client_id,
|
||||
st.name as real_status_name, st.is_final as is_status_final,
|
||||
@@ -414,12 +417,8 @@ app.get("/public/portal/:token", async (req, res) => {
|
||||
LEFT JOIN service_statuses st ON st.id::text = (s.raw_data->>'status_operativo')::text
|
||||
WHERE s.id = $1 AND s.owner_id = $2
|
||||
`, [parseInt(serviceIdParam), ownerId]);
|
||||
}
|
||||
// Si no hay ID en la URL, sacamos todos los del cliente usando su teléfono
|
||||
else {
|
||||
let phoneMatch = String(client.phone || "").replace(/\D/g, "").slice(-9);
|
||||
if (phoneMatch.length < 8) phoneMatch = "NO_VALIDO_123";
|
||||
|
||||
} else {
|
||||
// Si no hay ID, buscamos por el teléfono
|
||||
qServices = await pool.query(`
|
||||
SELECT s.id, s.service_ref, s.is_urgent, s.raw_data, s.created_at, s.client_id,
|
||||
st.name as real_status_name, st.is_final as is_status_final,
|
||||
@@ -457,7 +456,8 @@ app.get("/public/portal/:token", async (req, res) => {
|
||||
|
||||
} catch (e) {
|
||||
console.error("🔥 ERROR EN PORTAL:", e.message);
|
||||
res.status(500).json({ ok: false, error: "Error interno" });
|
||||
// AHORA EL SERVIDOR LE CHIVARÁ AL NAVEGADOR EL MOTIVO EXACTO DEL ERROR
|
||||
res.status(500).json({ ok: false, error: e.message, debug_stack: e.stack });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user