From df660b70595cbf50ec7ed04e4ef00026bbbfd4a6 Mon Sep 17 00:00:00 2001 From: marsalva Date: Tue, 24 Feb 2026 13:15:39 +0000 Subject: [PATCH] Actualizar server.js --- server.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/server.js b/server.js index 1bf127a..96d35da 100644 --- a/server.js +++ b/server.js @@ -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 }); } }); + +// --- 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 app.get("/agenda/requests", authMiddleware, async (req, res) => { try {