Actualizar server.js
This commit is contained in:
36
server.js
36
server.js
@@ -382,38 +382,27 @@ async function requirePlan(req, res, next, feature) {
|
|||||||
// ==========================================
|
// ==========================================
|
||||||
|
|
||||||
app.get("/public/portal/:token", async (req, res) => {
|
app.get("/public/portal/:token", async (req, res) => {
|
||||||
console.log("------------------------------------------");
|
|
||||||
console.log("👉 [PORTAL] 1. Entrando a ruta portal con token:", req.params.token, "y service:", req.query.service);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { token } = req.params;
|
const { token } = req.params;
|
||||||
const serviceId = req.query.service;
|
const serviceId = req.query.service;
|
||||||
|
|
||||||
console.log("👉 [PORTAL] 2. Buscando cliente en BD...");
|
// 1. Buscamos al cliente por su token
|
||||||
const qClient = await pool.query("SELECT * FROM clients WHERE portal_token = $1 LIMIT 1", [token]);
|
const qClient = await pool.query("SELECT * FROM clients WHERE portal_token = $1 LIMIT 1", [token]);
|
||||||
|
if (qClient.rowCount === 0) return res.status(404).json({ ok: false, error: "Enlace no válido" });
|
||||||
if (qClient.rowCount === 0) {
|
|
||||||
console.log("❌ [PORTAL] Cliente no encontrado");
|
|
||||||
return res.status(404).json({ ok: false, error: "Enlace no válido" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const client = qClient.rows[0];
|
const client = qClient.rows[0];
|
||||||
const ownerId = client.owner_id;
|
const ownerId = client.owner_id;
|
||||||
console.log(`✅ [PORTAL] Cliente encontrado: ${client.full_name} (Dueño: ${ownerId})`);
|
|
||||||
|
|
||||||
console.log("👉 [PORTAL] 3. Buscando datos de la empresa...");
|
// 2. Buscamos los datos de la empresa
|
||||||
const qConfig = await pool.query("SELECT full_name, company_logo FROM users WHERE id = $1", [ownerId]);
|
const qConfig = await pool.query("SELECT full_name, company_logo FROM users WHERE id = $1", [ownerId]);
|
||||||
const company = {
|
const company = {
|
||||||
name: qConfig.rows[0]?.full_name || "IntegraRepara",
|
name: qConfig.rows[0]?.full_name || "IntegraRepara",
|
||||||
logo: qConfig.rows[0]?.company_logo || null
|
logo: qConfig.rows[0]?.company_logo || null
|
||||||
};
|
};
|
||||||
console.log(`✅ [PORTAL] Empresa: ${company.name}`);
|
|
||||||
|
|
||||||
|
// 3. CONSULTA SEGURA (Con o sin ID)
|
||||||
let qServices;
|
let qServices;
|
||||||
console.log("👉 [PORTAL] 4. Lanzando consulta SQL de servicios...");
|
|
||||||
|
|
||||||
if (serviceId && !isNaN(parseInt(serviceId))) {
|
if (serviceId && !isNaN(parseInt(serviceId))) {
|
||||||
console.log("👉 [PORTAL] Tipo de búsqueda: POR ID EXACTO (" + serviceId + ")");
|
|
||||||
qServices = await pool.query(`
|
qServices = await pool.query(`
|
||||||
SELECT s.id, s.service_ref, s.is_urgent, s.raw_data, s.created_at,
|
SELECT s.id, s.service_ref, s.is_urgent, s.raw_data, s.created_at,
|
||||||
st.name as real_status_name, st.is_final as is_status_final,
|
st.name as real_status_name, st.is_final as is_status_final,
|
||||||
@@ -424,7 +413,6 @@ app.get("/public/portal/:token", async (req, res) => {
|
|||||||
WHERE s.id = $1 AND s.owner_id = $2 AND s.provider != 'SYSTEM_BLOCK'
|
WHERE s.id = $1 AND s.owner_id = $2 AND s.provider != 'SYSTEM_BLOCK'
|
||||||
`, [parseInt(serviceId), ownerId]);
|
`, [parseInt(serviceId), ownerId]);
|
||||||
} else {
|
} else {
|
||||||
console.log("👉 [PORTAL] Tipo de búsqueda: POR TELÉFONO");
|
|
||||||
let phoneMatch = String(client.phone || "").replace(/[^0-9]/g, "");
|
let phoneMatch = String(client.phone || "").replace(/[^0-9]/g, "");
|
||||||
if (phoneMatch.length > 9) phoneMatch = phoneMatch.slice(-9);
|
if (phoneMatch.length > 9) phoneMatch = phoneMatch.slice(-9);
|
||||||
if (phoneMatch.length < 6) phoneMatch = "TELEFONO_FALSO_123";
|
if (phoneMatch.length < 6) phoneMatch = "TELEFONO_FALSO_123";
|
||||||
@@ -441,8 +429,6 @@ app.get("/public/portal/:token", async (req, res) => {
|
|||||||
ORDER BY s.created_at DESC
|
ORDER BY s.created_at DESC
|
||||||
`, [ownerId, `%${phoneMatch}%`]);
|
`, [ownerId, `%${phoneMatch}%`]);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`✅ [PORTAL] Consulta OK. Encontrados: ${qServices.rowCount} servicios.`);
|
|
||||||
|
|
||||||
const formattedServices = qServices.rows.map(s => {
|
const formattedServices = qServices.rows.map(s => {
|
||||||
return {
|
return {
|
||||||
@@ -459,19 +445,11 @@ app.get("/public/portal/:token", async (req, res) => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("✅ [PORTAL] 5. Todo procesado. Enviando al navegador.");
|
|
||||||
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("🔥 [PORTAL] ERROR CRÍTICO CAPTURADO:");
|
console.error("🔥 ERROR EN PORTAL:", e.message);
|
||||||
console.error(e); // Esto imprimirá el error real en Coolify
|
res.status(500).json({ ok: false, error: e.message });
|
||||||
|
|
||||||
// Magia: Le enviamos el error al navegador para que tú lo puedas leer
|
|
||||||
res.status(500).json({
|
|
||||||
ok: false,
|
|
||||||
error_mensaje_real: e.message,
|
|
||||||
error_stack: e.stack
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user