Actualizar server.js

This commit is contained in:
2026-02-20 18:24:25 +00:00
parent de272a0ea7
commit 65618eab42

View File

@@ -439,31 +439,33 @@ app.post("/public/assignment/respond", async (req, res) => {
const { token, action } = req.body; const { token, action } = req.body;
await client.query('BEGIN'); await client.query('BEGIN');
// LEEMOS EXACTAMENTE IGUAL QUE EN LA RUTA GET, BLOQUEANDO ERRORES FANTASMA
const q = await client.query( const q = await client.query(
"SELECT *, CURRENT_TIMESTAMP as db_now FROM assignment_pings WHERE token = $1 FOR UPDATE", "SELECT *, CURRENT_TIMESTAMP as db_now FROM assignment_pings WHERE token = $1 FOR UPDATE",
[token] [token]
); );
if (q.rowCount === 0) throw new Error("Enlace no válido o inexistente"); if (q.rowCount === 0) throw new Error("Enlace no válido");
const ping = q.rows[0]; const ping = q.rows[0];
const isExpired = ping.status !== 'pending' || new Date(ping.expires_at) <= new Date(ping.db_now);
if (isExpired) throw new Error("El tiempo se agotó justo antes de aceptar.");
if (action === 'accept') { if (action === 'accept') {
await client.query("UPDATE assignment_pings SET status = 'accepted' WHERE id = $1", [ping.id]); await client.query("UPDATE assignment_pings SET status = 'accepted' WHERE id = $1", [ping.id]);
// AÑADIDO: Guardar ID de operario (Forzamos tipo INT para evitar errores de JSONB) // Obtenemos el ID del estado "Asignado" del sistema
const statusQ = await client.query(
"SELECT id FROM service_statuses WHERE owner_id = (SELECT owner_id FROM scraped_services WHERE id = $1) AND name = 'Asignado' LIMIT 1",
[ping.scraped_id]
);
const idAsignado = statusQ.rows[0]?.id;
// Actualizamos el servicio: Operario asignado y Estado "Asignado"
await client.query(` await client.query(`
UPDATE scraped_services UPDATE scraped_services
SET status = 'imported', SET status = 'imported',
automation_status = 'completed', automation_status = 'completed',
assigned_to = $1, assigned_to = $1,
raw_data = raw_data || jsonb_build_object('assigned_to', $1::int) raw_data = raw_data || jsonb_build_object('assigned_to', $1::int, 'status_operativo', $3::text)
WHERE id = $2 WHERE id = $2
`, [ping.user_id, ping.scraped_id]); `, [ping.user_id, ping.scraped_id, idAsignado]);
} else { } else {
await client.query("UPDATE assignment_pings SET status = 'rejected', expires_at = CURRENT_TIMESTAMP WHERE id = $1", [ping.id]); await client.query("UPDATE assignment_pings SET status = 'rejected', expires_at = CURRENT_TIMESTAMP WHERE id = $1", [ping.id]);
@@ -473,11 +475,8 @@ app.post("/public/assignment/respond", async (req, res) => {
res.json({ ok: true }); res.json({ ok: true });
} catch (e) { } catch (e) {
await client.query('ROLLBACK'); await client.query('ROLLBACK');
console.error("ERROR AL RESPONDER TURNO:", e.message); // Por si acaso hay otro error
res.status(400).json({ ok: false, error: e.message }); res.status(400).json({ ok: false, error: e.message });
} finally { } finally { client.release(); }
client.release();
}
}); });
// ========================================== // ==========================================