From 901930dcf6d149600cd7a162ca05875f31eecfe9 Mon Sep 17 00:00:00 2001 From: marsalva Date: Sun, 1 Mar 2026 21:59:38 +0000 Subject: [PATCH] Actualizar server.js --- server.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/server.js b/server.js index a8cd2bd..db6c10d 100644 --- a/server.js +++ b/server.js @@ -342,6 +342,24 @@ function signToken(user) { const accountId = user.owner_id || user.id; return jw function authMiddleware(req, res, next) { const h = req.headers.authorization || ""; const token = h.startsWith("Bearer ") ? h.slice(7) : ""; if (!token) return res.status(401).json({ ok: false, error: "No token" }); try { req.user = jwt.verify(token, JWT_SECRET); next(); } catch { return res.status(401).json({ ok: false, error: "Token inválido" }); } } function genCode6() { return String(Math.floor(100000 + Math.random() * 900000)); } + +// ========================================== +// 🕵️ ROBOT NOTARIO (TRAZABILIDAD TOTAL) +// ========================================== +async function registrarMovimiento(serviceId, userId, action, details) { + try { + let userName = "Sistema Robot"; + if (userId) { + const u = await pool.query("SELECT full_name FROM users WHERE id=$1", [userId]); + if (u.rowCount > 0) userName = u.rows[0].full_name; + } + await pool.query( + "INSERT INTO scraped_service_logs (scraped_id, user_name, action, details) VALUES ($1, $2, $3, $4)", + [serviceId, userName, action, details || ""] + ); + } catch (e) { console.error("Error Robot Notario:", e); } +} + // 🛡️ MIDDLEWARE DE PLANES async function requirePlan(req, res, next, feature) { try { @@ -1434,6 +1452,12 @@ app.put("/services/set-appointment/:id", authMiddleware, async (req, res) => { await pool.query('UPDATE scraped_services SET raw_data = $1, assigned_to = $2 WHERE id = $3 AND owner_id = $4', [JSON.stringify(updatedRawData), finalAssignedTo, id, req.user.accountId] ); + + // --- INICIO TRAZABILIDAD --- + let logDetalle = `Estado modificado a: ${stName.toUpperCase() || 'MODIFICADO'}.`; + if (newDate) logDetalle += ` Cita para el ${newDate} a las ${newTime}.`; + await registrarMovimiento(id, req.user.sub, "Actualización desde App", logDetalle); + // --- FIN TRAZABILIDAD --- res.json({ ok: true }); } catch (e) {