Actualizar server.js
This commit is contained in:
47
server.js
47
server.js
@@ -4208,6 +4208,53 @@ app.post("/webhook/evolution", async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ==========================================
|
||||||
|
// ✍️ 5. RUTA PARA ACEPTAR/RECHAZAR Y FIRMAR PRESUPUESTOS
|
||||||
|
// ==========================================
|
||||||
|
app.post("/public/portal/:token/budget/:id/respond", async (req, res) => {
|
||||||
|
try {
|
||||||
|
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]
|
||||||
|
);
|
||||||
|
|
||||||
|
// 4. Avisar a la OFICINA (Admin) por WhatsApp del resultado
|
||||||
|
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}.`;
|
||||||
|
|
||||||
|
// Lo enviamos a tu número usando la instancia de tu empresa
|
||||||
|
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 });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 🕒 EL RELOJ DEL SISTEMA (Ejecutar cada minuto)
|
// 🕒 EL RELOJ DEL SISTEMA (Ejecutar cada minuto)
|
||||||
// ==========================================
|
// ==========================================
|
||||||
|
|||||||
Reference in New Issue
Block a user