Actualizar server.js

This commit is contained in:
2026-02-15 22:54:09 +00:00
parent b27f6e3339
commit a5f400b96e

View File

@@ -343,18 +343,42 @@ app.post("/public/assignment/respond", async (req, res) => {
try { try {
const { token, action } = req.body; const { token, action } = req.body;
await client.query('BEGIN'); 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"); if (q.rowCount === 0) throw new Error("Acción caducada");
const ping = q.rows[0]; const ping = q.rows[0];
if (action === 'accept') { 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 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 { } else {
await client.query("UPDATE assignment_pings SET status = 'rejected', expires_at = NOW() WHERE id = $1", [ping.id]); await client.query("UPDATE assignment_pings SET status = 'rejected', expires_at = NOW() WHERE id = $1", [ping.id]);
} }
await client.query('COMMIT'); await client.query('COMMIT');
res.json({ ok: true }); 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();
}
}); });
// ========================================== // ==========================================