Actualizar server.js

This commit is contained in:
2026-02-14 22:40:35 +00:00
parent 7f8a469cef
commit b95589335a

View File

@@ -249,7 +249,7 @@ async function autoUpdateDB() {
}
// HELPERS
function normalizePhone(phone) { let p = String(phone || "").trim().replace(/\s+/g, "").replace(/-/g, ""); if (!p) return ""; if (!p.startsWith("+") && /^[6789]\d{8}$/.test(p)) return "+34" + p; return p; }
function normalizePhone(phone) { let p = String(phone || "").trim().replace(/\s+/g, "").replace(/-/g, ""); if (!p) return ""; if (!p.startsWith("+") && /^[6789]\d{8}/.test(p)) return "+34" + p; return p; }
function signToken(user) { const accountId = user.owner_id || user.id; return jwt.sign({ sub: user.id, email: user.email, phone: user.phone, role: user.role || 'operario', accountId }, JWT_SECRET, { expiresIn: "30d" }); }
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)); }
@@ -436,6 +436,49 @@ app.post("/providers/import/:id", authMiddleware, async (req, res) => {
}
});
// NUEVA RUTA PARA ACTUALIZAR BORRADOR DEL SCRAPING
app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
const { id } = req.params;
const {
name, phone, address, cp, description,
guild_id, assigned_to, internal_notes, client_notes, is_urgent
} = req.body;
try {
const current = await pool.query(
'SELECT raw_data FROM scraped_services WHERE id = $1 AND owner_id = $2',
[id, req.user.accountId]
);
if (current.rows.length === 0) {
return res.status(404).json({ error: 'Expediente no encontrado' });
}
const updatedRawData = {
...current.rows[0].raw_data,
"Nombre Cliente": name,
"Teléfono": phone,
"Dirección": address,
"Código Postal": cp,
"Descripción": description,
"Urgente": is_urgent ? "Sí" : "No"
};
await pool.query(
`UPDATE scraped_services
SET raw_data = $1,
status = 'pending'
WHERE id = $2 AND owner_id = $3`,
[JSON.stringify(updatedRawData), id, req.user.accountId]
);
res.json({ ok: true, message: 'Borrador actualizado correctamente' });
} catch (error) {
console.error('Error al actualizar borrador:', error);
res.status(500).json({ error: 'Error interno del servidor' });
}
});
// ==========================================
// 🗺️ MAPEADOR (VERSIÓN LIMPIA: SOLO EL ÚLTIMO SERVICIO)
// ==========================================