diff --git a/server.js b/server.js index 12759a0..986bcd2 100644 --- a/server.js +++ b/server.js @@ -331,16 +331,47 @@ async function ensureInstance(instanceName) { app.get("/public/assignment/:token", async (req, res) => { try { const { token } = req.params; + + // MODO DEBUG: Traemos el registro exista o no, y le pedimos a la BD su hora exacta (db_now) const q = await pool.query(` - SELECT ap.*, s.raw_data, u.full_name as worker_name - FROM assignment_pings ap - JOIN scraped_services s ON ap.scraped_id = s.id - JOIN users u ON ap.user_id = u.id - WHERE ap.token = $1 AND ap.status = 'pending' AND ap.expires_at > CURRENT_TIMESTAMP + SELECT ap.*, s.raw_data, u.full_name as worker_name, CURRENT_TIMESTAMP as db_now + FROM assignment_pings ap + JOIN scraped_services s ON ap.scraped_id = s.id + JOIN users u ON ap.user_id = u.id + WHERE ap.token = $1 `, [token]); - if (q.rowCount === 0) return res.status(404).json({ ok: false, error: "Enlace caducado" }); - res.json({ ok: true, service: q.rows[0].raw_data, worker: q.rows[0].worker_name }); - } catch (e) { res.status(500).json({ ok: false }); } + + if (q.rowCount === 0) { + return res.status(404).json({ ok: false, error: "El enlace no existe en la base de datos." }); + } + + const data = q.rows[0]; + + // Hacemos la comprobación manualmente para decidir si mandamos error o éxito + const isExpired = data.status !== 'pending' || new Date(data.expires_at) <= new Date(data.db_now); + + if (isExpired) { + return res.status(404).json({ + ok: false, + error: "Este enlace ha caducado o el servicio ya ha sido asignado.", + debug: { + estado_en_bd: data.status, + hora_limite_bd: data.expires_at, + hora_actual_bd: data.db_now + } + }); + } + + res.json({ + ok: true, + service: data.raw_data, + worker: data.worker_name, + debug: { + hora_limite_bd: data.expires_at, + hora_actual_bd: data.db_now + } + }); + } catch (e) { res.status(500).json({ ok: false, error: e.message }); } }); app.post("/public/assignment/respond", async (req, res) => {