From 743963c93a91895204cba6c670c7867b564cc451 Mon Sep 17 00:00:00 2001 From: marsalva Date: Sun, 29 Mar 2026 10:24:21 +0000 Subject: [PATCH] Actualizar server.js --- server.js | 85 ++++++------------------------------------------------- 1 file changed, 9 insertions(+), 76 deletions(-) diff --git a/server.js b/server.js index fca2d35..9cb797e 100644 --- a/server.js +++ b/server.js @@ -1278,12 +1278,12 @@ 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 Y SUS DATOS FISCALES (Para el PDF) + // 2. 馃洃 FIX CR脥TICO: PEDIMOS BILLING_SETTINGS A LA BBDD const qConfig = await pool.query("SELECT full_name, company_logo, billing_settings FROM users WHERE id = $1", [ownerId]); const company = { name: qConfig.rows[0]?.full_name || "IntegraRepara", logo: qConfig.rows[0]?.company_logo || null, - billing_settings: qConfig.rows[0]?.billing_settings || null + billing_settings: qConfig.rows[0]?.billing_settings || null // AQU脥 VIAJA EL IBAN Y LAS OBSERVACIONES }; // 3. CONSULTA SEGURA (Con o sin ID) @@ -1331,7 +1331,7 @@ app.get("/public/portal/:token", async (req, res) => { }; }); - // 馃挵 4. BUSCAR PRESUPUESTOS CON TODOS LOS DETALLES PARA EL PDF + // 4. BUSCAR PRESUPUESTOS CON SUS ART脥CULOS let cleanPhoneSearch = String(client.phone || "").replace(/[^0-9]/g, ""); if (cleanPhoneSearch.length > 9) cleanPhoneSearch = cleanPhoneSearch.slice(-9); @@ -1351,7 +1351,6 @@ app.get("/public/portal/:token", async (req, res) => { title: "Presupuesto de Reparaci贸n", amount: parseFloat(b.total).toFixed(2), created_at: b.created_at, - // Datos puros para el PDF: items: b.items, subtotal: b.subtotal, tax: b.tax, @@ -1361,80 +1360,14 @@ app.get("/public/portal/:token", async (req, res) => { })); } - res.json({ ok: true, client: { name: client.full_name }, company, services: formattedServices, quotes: formattedQuotes }); - - // 3. CONSULTA SEGURA (Con o sin ID) - let qServices; - if (serviceId && !isNaN(parseInt(serviceId))) { - qServices = await pool.query(` - 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, - u.full_name as worker_name, u.phone as worker_phone - FROM scraped_services s - LEFT JOIN users u ON u.id = s.assigned_to - LEFT JOIN service_statuses st ON st.id::text = (s.raw_data->>'status_operativo')::text - WHERE s.id = $1 AND s.owner_id = $2 AND s.provider != 'SYSTEM_BLOCK' - `, [parseInt(serviceId), ownerId]); - } else { - let phoneMatch = String(client.phone || "").replace(/[^0-9]/g, ""); - if (phoneMatch.length > 9) phoneMatch = phoneMatch.slice(-9); - if (phoneMatch.length < 6) phoneMatch = "TELEFONO_FALSO_123"; - - qServices = await pool.query(` - 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, - u.full_name as worker_name, u.phone as worker_phone - FROM scraped_services s - LEFT JOIN users u ON u.id = s.assigned_to - LEFT JOIN service_statuses st ON st.id::text = (s.raw_data->>'status_operativo')::text - WHERE s.owner_id = $1 AND s.provider != 'SYSTEM_BLOCK' - AND s.raw_data::text ILIKE $2 - ORDER BY s.created_at DESC - `, [ownerId, `%${phoneMatch}%`]); - } - - const formattedServices = qServices.rows.map(s => { - return { - id: s.id, - title: s.is_urgent ? `馃毃 URGENTE: #${s.service_ref}` : `Expediente #${s.service_ref}`, - description: s.raw_data?.["Descripci贸n"] || s.raw_data?.["DESCRIPCION"] || "Aviso de reparaci贸n", - status_name: s.real_status_name || "En gesti贸n", - is_final: s.is_status_final || false, - scheduled_date: s.raw_data?.scheduled_date || "", - scheduled_time: s.raw_data?.scheduled_time || "", - assigned_worker: s.worker_name || null, - worker_phone: s.worker_phone || null, - raw_data: s.raw_data - }; + res.json({ + ok: true, + client: { name: client.full_name }, + company, + services: formattedServices, + quotes: formattedQuotes }); - // 馃挵 4. A脩ADIDO SAAS: BUSCAR PRESUPUESTOS DEL CLIENTE - // Comparamos el tel茅fono del cliente quit谩ndole los prefijos para asegurar que caza - let cleanPhoneSearch = String(client.phone || "").replace(/[^0-9]/g, ""); - if (cleanPhoneSearch.length > 9) cleanPhoneSearch = cleanPhoneSearch.slice(-9); - - let formattedQuotes = []; - if (cleanPhoneSearch.length >= 9) { - const qBudgets = await pool.query(` - SELECT id, client_name, items, subtotal, tax, total, status, created_at - FROM budgets - WHERE owner_id = $1 - AND client_phone LIKE $2 - ORDER BY created_at DESC - `, [ownerId, `%${cleanPhoneSearch}%`]); - - formattedQuotes = qBudgets.rows.map(b => ({ - id: b.id, - quote_ref: `PRE-${b.id}`, // Generamos la referencia visual - title: "Presupuesto de Reparaci贸n", // Le damos un t铆tulo gen茅rico agradable - amount: parseFloat(b.total).toFixed(2), - created_at: b.created_at - })); - } - - // 馃殌 METEMOS LAS "QUOTES" (PRESUPUESTOS) EN EL PAQUETE FINAL - res.json({ ok: true, client: { name: client.full_name }, company, services: formattedServices, quotes: formattedQuotes }); - } catch (e) { console.error("馃敟 ERROR EN PORTAL:", e.message); res.status(500).json({ ok: false, error: e.message });