From 65618eab42d6cd0a13c94b0821d381fe7af18d51 Mon Sep 17 00:00:00 2001 From: marsalva Date: Fri, 20 Feb 2026 18:24:25 +0000 Subject: [PATCH] Actualizar server.js --- server.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/server.js b/server.js index 8288d6b..d419f32 100644 --- a/server.js +++ b/server.js @@ -439,31 +439,33 @@ app.post("/public/assignment/respond", async (req, res) => { const { token, action } = req.body; await client.query('BEGIN'); - // LEEMOS EXACTAMENTE IGUAL QUE EN LA RUTA GET, BLOQUEANDO ERRORES FANTASMA const q = await client.query( "SELECT *, CURRENT_TIMESTAMP as db_now FROM assignment_pings WHERE token = $1 FOR UPDATE", [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 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') { 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(` UPDATE scraped_services SET status = 'imported', automation_status = 'completed', 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 - `, [ping.user_id, ping.scraped_id]); + `, [ping.user_id, ping.scraped_id, idAsignado]); } else { 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 }); } catch (e) { 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 }); - } finally { - client.release(); - } + } finally { client.release(); } }); // ==========================================