Actualizar server.js

This commit is contained in:
2026-03-29 09:20:44 +00:00
parent 503e1da7f2
commit 07a5115cf5

View File

@@ -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) { } catch (e) {
console.error("🔥 ERROR EN PORTAL:", e.message); console.error("🔥 ERROR EN PORTAL:", e.message);