Actualizar server.js

This commit is contained in:
2026-02-24 13:15:39 +00:00
parent 0c699d0e31
commit df660b7059

View File

@@ -778,6 +778,40 @@ app.get("/public/portal/:token/slots", async (req, res) => {
} catch (e) { console.error("Error Slots:", e); res.status(500).json({ ok: false }); } } catch (e) { console.error("Error Slots:", e); res.status(500).json({ ok: false }); }
}); });
// --- RUTA PARA GUARDAR LA CITA SOLICITADA POR EL CLIENTE ---
app.post("/public/portal/:token/book", async (req, res) => {
try {
const { token } = req.params;
const { serviceId, date, time } = req.body;
if (!serviceId || !date || !time) return res.status(400).json({ ok: false, error: "Faltan datos" });
// Verificamos quién es el dueño del portal usando el token
const clientQ = await pool.query("SELECT id, owner_id FROM clients WHERE portal_token = $1", [token]);
if (clientQ.rowCount === 0) return res.status(404).json({ ok: false, error: "Token inválido" });
const ownerId = clientQ.rows[0].owner_id;
// Recuperamos los datos crudos del servicio
const serviceQ = await pool.query("SELECT raw_data FROM scraped_services WHERE id=$1 AND owner_id=$2", [serviceId, ownerId]);
if (serviceQ.rowCount === 0) return res.status(404).json({ ok: false, error: "Servicio no encontrado" });
const raw = serviceQ.rows[0].raw_data || {};
// Grabamos la solicitud en el jsonb para que el admin la vea en agenda.html
raw.requested_date = date;
raw.requested_time = time;
raw.appointment_status = 'pending';
await pool.query("UPDATE scraped_services SET raw_data = $1 WHERE id = $2", [JSON.stringify(raw), serviceId]);
res.json({ ok: true });
} catch (e) {
console.error("Error agendando cita (book):", e);
res.status(500).json({ ok: false, error: "Error interno" });
}
});
// 3. OBTENER SOLICITUDES PARA EL PANEL DEL ADMIN // 3. OBTENER SOLICITUDES PARA EL PANEL DEL ADMIN
app.get("/agenda/requests", authMiddleware, async (req, res) => { app.get("/agenda/requests", authMiddleware, async (req, res) => {
try { try {