From e0ae22163dea0b9af98b226e264d222f2a995859 Mon Sep 17 00:00:00 2001 From: marsalva Date: Sun, 29 Mar 2026 15:31:51 +0000 Subject: [PATCH] Actualizar server.js --- server.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/server.js b/server.js index 5bd7e3f..593360f 100644 --- a/server.js +++ b/server.js @@ -4218,27 +4218,43 @@ app.post("/public/portal/:token/budget/:id/respond", async (req, res) => { const { token, id } = req.params; const { action, signature } = req.body; - // 1. Identificar cliente const clientQ = await pool.query("SELECT owner_id, full_name, phone FROM clients WHERE portal_token = $1", [token]); if (clientQ.rowCount === 0) return res.status(404).json({ ok: false }); - const ownerId = clientQ.rows[0].owner_id; - // 2. Parche dinámico: Añadir columna de firma en la BBDD si no existe await pool.query(`ALTER TABLE budgets ADD COLUMN IF NOT EXISTS signature TEXT;`).catch(()=>{}); - // 3. Guardar estado y firma const newStatus = action === 'accept' ? 'accepted' : 'rejected'; await pool.query( "UPDATE budgets SET status = $1, signature = $2 WHERE id = $3 AND owner_id = $4", [newStatus, signature || null, id, ownerId] ); - // ========================================== + const ownerData = await pool.query("SELECT phone FROM users WHERE id = $1", [ownerId]); + const bQ = await pool.query("SELECT client_name, total FROM budgets WHERE id = $1", [id]); + + if (ownerData.rowCount > 0 && bQ.rowCount > 0) { + const adminPhone = ownerData.rows[0].phone; + const bInfo = bQ.rows[0]; + + const msgWa = action === 'accept' + ? `🟢 *PRESUPUESTO ACEPTADO*\nEl cliente ${bInfo.client_name} ha ACEPTADO y firmado el presupuesto PRE-${id} por ${bInfo.total}€.` + : `🔴 *PRESUPUESTO RECHAZADO*\nEl cliente ${bInfo.client_name} ha RECHAZADO el presupuesto PRE-${id}.`; + + sendWhatsAppAuto(adminPhone, msgWa, `cliente_${ownerId}`, false).catch(console.error); + } + + res.json({ ok: true }); + } catch (e) { + console.error("Error firmando presupuesto:", e); + res.status(500).json({ ok: false }); + } +}); + +// ========================================== // 💳 6. MOTOR DE PAGOS (STRIPE) PARA PRESUPUESTOS Y FACTURAS // ========================================== -// A) CREAR SESIÓN DE PAGO (Cuando el cliente pulsa "Pagar") app.post("/public/portal/:token/budget/:id/checkout", async (req, res) => { try { const { token, id } = req.params;