diff --git a/server.js b/server.js index d47eed3..ea7a105 100644 --- a/server.js +++ b/server.js @@ -343,18 +343,42 @@ app.post("/public/assignment/respond", async (req, res) => { try { const { token, action } = req.body; await client.query('BEGIN'); - const q = await client.query("SELECT * FROM assignment_pings WHERE token = $1 AND status = 'pending' AND expires_at > NOW()", [token]); + + // 1. Buscamos el ping para saber qué operario y qué servicio es + const q = await client.query( + "SELECT * FROM assignment_pings WHERE token = $1 AND status = 'pending' AND expires_at > NOW()", + [token] + ); if (q.rowCount === 0) throw new Error("Acción caducada"); + const ping = q.rows[0]; + if (action === 'accept') { + // Marcamos el ping como aceptado await client.query("UPDATE assignment_pings SET status = 'accepted' WHERE id = $1", [ping.id]); - await client.query("UPDATE scraped_services SET status = 'imported', automation_status = 'completed' WHERE id = $1", [ping.scraped_id]); + + // ACCIÓN CLAVE: Actualizamos el servicio con el operario asignado + // Guardamos el user_id en el JSON raw_data para que el panel lo lea correctamente + await client.query(` + UPDATE scraped_services + SET status = 'imported', + automation_status = 'completed', + raw_data = raw_data || jsonb_build_object('assigned_to', $1) + WHERE id = $2 + `, [ping.user_id, ping.scraped_id]); + } else { await client.query("UPDATE assignment_pings SET status = 'rejected', expires_at = NOW() WHERE id = $1", [ping.id]); } + await client.query('COMMIT'); res.json({ ok: true }); - } catch (e) { await client.query('ROLLBACK'); res.status(400).json({ ok: false }); } finally { client.release(); } + } catch (e) { + await client.query('ROLLBACK'); + res.status(400).json({ ok: false }); + } finally { + client.release(); + } }); // ==========================================