Actualizar server.js
This commit is contained in:
17
server.js
17
server.js
@@ -1850,8 +1850,9 @@ app.get("/public/portal/:token/location/:serviceId", async (req, res) => {
|
|||||||
// 🕒 EL RELOJ DEL SISTEMA (Ejecutar cada minuto)
|
// 🕒 EL RELOJ DEL SISTEMA (Ejecutar cada minuto)
|
||||||
// ==========================================
|
// ==========================================
|
||||||
setInterval(async () => {
|
setInterval(async () => {
|
||||||
|
const client = await pool.connect(); // <-- Conectamos de forma segura
|
||||||
try {
|
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
|
SELECT ap.id, ap.scraped_id, ap.user_id, s.owner_id, s.raw_data
|
||||||
FROM assignment_pings ap
|
FROM assignment_pings ap
|
||||||
JOIN scraped_services s ON ap.scraped_id = s.id
|
JOIN scraped_services s ON ap.scraped_id = s.id
|
||||||
@@ -1861,8 +1862,8 @@ setInterval(async () => {
|
|||||||
`);
|
`);
|
||||||
|
|
||||||
for (const ping of expiredPings.rows) {
|
for (const ping of expiredPings.rows) {
|
||||||
await pool.query("UPDATE assignment_pings SET status = 'expired' WHERE id = $1", [ping.id]);
|
await client.query("UPDATE assignment_pings SET status = 'expired' WHERE id = $1", [ping.id]);
|
||||||
const nextWorkerQ = await pool.query(`
|
const nextWorkerQ = await client.query(`
|
||||||
SELECT u.id, u.phone, u.full_name
|
SELECT u.id, u.phone, u.full_name
|
||||||
FROM users u
|
FROM users u
|
||||||
JOIN user_guilds ug ON u.id = ug.user_id
|
JOIN user_guilds ug ON u.id = ug.user_id
|
||||||
@@ -1874,16 +1875,20 @@ setInterval(async () => {
|
|||||||
if (nextWorkerQ.rowCount > 0) {
|
if (nextWorkerQ.rowCount > 0) {
|
||||||
const nextW = nextWorkerQ.rows[0];
|
const nextW = nextWorkerQ.rows[0];
|
||||||
const newToken = crypto.randomBytes(16).toString('hex');
|
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 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}`;
|
const instanceName = `cliente_${ping.owner_id}`;
|
||||||
sendWhatsAppAuto(nextW.phone, mensaje, instanceName).catch(console.error);
|
sendWhatsAppAuto(nextW.phone, mensaje, instanceName).catch(console.error);
|
||||||
} else {
|
} 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);
|
}, 60000);
|
||||||
|
|
||||||
const port = process.env.PORT || 3000;
|
const port = process.env.PORT || 3000;
|
||||||
|
|||||||
Reference in New Issue
Block a user