From 07a5115cf57fc43a58f109c3dbea9a1de34af682 Mon Sep 17 00:00:00 2001 From: marsalva Date: Sun, 29 Mar 2026 09:20:44 +0000 Subject: [PATCH] Actualizar server.js --- server.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 6664e37..d9cab69 100644 --- a/server.js +++ b/server.js @@ -1330,7 +1330,32 @@ app.get("/public/portal/:token", async (req, res) => { }; }); - res.json({ ok: true, client: { name: client.full_name }, company, services: formattedServices }); + // 💰 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);