Actualizar server.js

This commit is contained in:
2026-03-28 21:09:25 +00:00
parent 3fe6fc5bc9
commit 503e1da7f2

View File

@@ -4136,21 +4136,39 @@ setInterval(async () => {
for (const ping of expiredPings.rows) {
await client.query("UPDATE assignment_pings SET status = 'expired' WHERE id = $1", [ping.id]);
// 🛑 FIX: Extraer Gremio y CP del raw_data para buscar al SIGUIENTE operario CORRECTO
const raw = ping.raw_data || {};
const guildId = raw.guild_id || null;
// Lógica para extraer CP exactamente igual que al inicio
const todoElTexto = JSON.stringify(raw).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
const cpMatch = todoElTexto.match(/\b\d{5}\b/);
const cpFinal = cpMatch ? cpMatch[0] : "00000";
// Si por algún motivo no hay gremio guardado, abortamos la bolsa para este servicio
if (!guildId) {
await client.query("UPDATE scraped_services SET automation_status = 'failed' WHERE id = $1", [ping.scraped_id]);
continue;
}
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
WHERE u.owner_id = $1 AND u.status = 'active'
AND ug.guild_id = $3 AND u.zones::jsonb @> $4::jsonb
AND u.id NOT IN (SELECT user_id FROM assignment_pings WHERE scraped_id = $2)
LIMIT 1
`, [ping.owner_id, ping.scraped_id]);
`, [ping.owner_id, ping.scraped_id, guildId, JSON.stringify([{ cps: cpFinal }])]);
if (nextWorkerQ.rowCount > 0) {
const nextW = nextWorkerQ.rows[0];
const newToken = crypto.randomBytes(16).toString('hex');
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}`;
// 🛑 FIX: URL corregida a web.integrarepara.es
const mensaje = `🛠️ *SERVICIO DISPONIBLE*\nEl anterior compañero no respondió. Es tu turno:\n🔗 https://web.integrarepara.es/aceptar.html?t=${newToken}`;
const instanceName = `cliente_${ping.owner_id}`;
sendWhatsAppAuto(nextW.phone, mensaje, instanceName).catch(console.error);
} else {