Actualizar server.js

This commit is contained in:
2026-03-29 15:31:51 +00:00
parent c9e9083707
commit e0ae22163d

View File

@@ -4218,27 +4218,43 @@ app.post("/public/portal/:token/budget/:id/respond", async (req, res) => {
const { token, id } = req.params; const { token, id } = req.params;
const { action, signature } = req.body; 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]); 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 }); if (clientQ.rowCount === 0) return res.status(404).json({ ok: false });
const ownerId = clientQ.rows[0].owner_id; 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(()=>{}); 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'; const newStatus = action === 'accept' ? 'accepted' : 'rejected';
await pool.query( await pool.query(
"UPDATE budgets SET status = $1, signature = $2 WHERE id = $3 AND owner_id = $4", "UPDATE budgets SET status = $1, signature = $2 WHERE id = $3 AND owner_id = $4",
[newStatus, signature || null, id, ownerId] [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 // 💳 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) => { app.post("/public/portal/:token/budget/:id/checkout", async (req, res) => {
try { try {
const { token, id } = req.params; const { token, id } = req.params;