From c629370cee68e7fe98c11e6a393f9d41b05d8c49 Mon Sep 17 00:00:00 2001 From: marsalva Date: Thu, 26 Feb 2026 08:16:16 +0000 Subject: [PATCH] Actualizar server.js --- server.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/server.js b/server.js index 769f36f..f4aa66e 100644 --- a/server.js +++ b/server.js @@ -1850,8 +1850,9 @@ app.get("/public/portal/:token/location/:serviceId", async (req, res) => { // 🕒 EL RELOJ DEL SISTEMA (Ejecutar cada minuto) // ========================================== setInterval(async () => { + const client = await pool.connect(); // <-- Conectamos de forma segura try { - const expiredPings = await pool.query(` + const expiredPings = await client.query(` SELECT ap.id, ap.scraped_id, ap.user_id, s.owner_id, s.raw_data FROM assignment_pings ap JOIN scraped_services s ON ap.scraped_id = s.id @@ -1861,8 +1862,8 @@ setInterval(async () => { `); for (const ping of expiredPings.rows) { - await pool.query("UPDATE assignment_pings SET status = 'expired' WHERE id = $1", [ping.id]); - const nextWorkerQ = await pool.query(` + await client.query("UPDATE assignment_pings SET status = 'expired' WHERE id = $1", [ping.id]); + const nextWorkerQ = await client.query(` SELECT u.id, u.phone, u.full_name FROM users u JOIN user_guilds ug ON u.id = ug.user_id @@ -1874,16 +1875,20 @@ setInterval(async () => { if (nextWorkerQ.rowCount > 0) { const nextW = nextWorkerQ.rows[0]; const newToken = crypto.randomBytes(16).toString('hex'); - await pool.query(`INSERT INTO assignment_pings (scraped_id, user_id, token, expires_at) VALUES ($1, $2, $3, CURRENT_TIMESTAMP + INTERVAL '5 minutes')`, [ping.scraped_id, nextW.id, newToken]); + await client.query(`INSERT INTO assignment_pings (scraped_id, user_id, token, expires_at) VALUES ($1, $2, $3, CURRENT_TIMESTAMP + INTERVAL '5 minutes')`, [ping.scraped_id, nextW.id, newToken]); const mensaje = `🛠️ *SERVICIO DISPONIBLE*\nEl anterior compañero no respondió. Es tu turno:\n🔗 https://integrarepara.es/aceptar.html?t=${newToken}`; const instanceName = `cliente_${ping.owner_id}`; sendWhatsAppAuto(nextW.phone, mensaje, instanceName).catch(console.error); } else { - await pool.query("UPDATE scraped_services SET automation_status = 'failed' WHERE id = $1", [ping.scraped_id]); + await client.query("UPDATE scraped_services SET automation_status = 'failed' WHERE id = $1", [ping.scraped_id]); } } - } catch (e) { console.error("Reloj:", e); } + } catch (e) { + console.error("Reloj:", e); + } finally { + client.release(); // <-- Liberamos la conexión SIEMPRE + } }, 60000); const port = process.env.PORT || 3000;