Actualizar server.js

This commit is contained in:
2026-03-15 14:45:56 +00:00
parent 0419a0dc0f
commit e6c5dcefe2

View File

@@ -1856,48 +1856,52 @@ async function dispatchToBolsa(serviceId, guildId, cp, accountId, userId) {
} }
// ========================================== // ==========================================
// 📥 RECEPCIÓN DE SERVICIOS (DETECTOR + AUTO-BOLSA CORREGIDO) // 📥 RECEPCIÓN DE SERVICIOS (EL MOTOR DEFINITIVO)
// ========================================== // ==========================================
app.post("/providers/scraped", authMiddleware, async (req, res) => { app.post("/providers/scraped", authMiddleware, async (req, res) => {
try { try {
const { provider, services } = req.body; const { provider, services } = req.body;
if (!provider || !Array.isArray(services)) return res.status(400).json({ ok: false });
if (!provider || !Array.isArray(services)) {
return res.status(400).json({ ok: false, error: "Formato de datos inválido" });
}
// 1. Cargamos el interruptor (siendo flexibles con 1 o true)
const credsQ = await pool.query( const credsQ = await pool.query(
"SELECT auto_dispatch FROM provider_credentials WHERE owner_id = $1 AND provider = $2", "SELECT auto_dispatch FROM provider_credentials WHERE owner_id = $1 AND provider = $2",
[req.user.accountId, provider] [req.user.accountId, provider]
); );
const autoDispatchEnabled = credsQ.rowCount > 0 &&
// ✅ CORRECCIÓN: Aceptamos 1, '1', 't' o true como "Encendido" (credsQ.rows[0].auto_dispatch === true || credsQ.rows[0].auto_dispatch === 1 || credsQ.rows[0].auto_dispatch === '1');
let autoDispatchEnabled = false;
if (credsQ.rowCount > 0) { // 2. Cargamos los gremios y sus palabras clave para detectar el gremio si falta
const val = credsQ.rows[0].auto_dispatch; const allGuilds = await pool.query("SELECT id, name, ia_keywords FROM guilds WHERE owner_id = $1", [req.user.accountId]);
autoDispatchEnabled = (val === true || val === 1 || val === '1' || val === 't');
}
let count = 0; let count = 0;
for (const svc of services) { for (const svc of services) {
const ref = svc['service_ref'] || svc['SERVICIO'] || svc['Referencia'] || svc['Expediente'] ||
(svc.raw_data && (svc.raw_data['SERVICIO'] || svc.raw_data['Referencia']));
const ref = svc['service_ref'] if (!ref) continue;
|| svc['SERVICIO']
|| svc['Referencia']
|| svc['Expediente']
|| (svc.raw_data && (svc.raw_data['SERVICIO'] || svc.raw_data['Referencia']));
if (!ref) continue;
// DETECCIÓN DE URGENCIA
let esUrgente = false; let esUrgente = false;
const todoElTexto = JSON.stringify(svc).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/\n/g, " "); const textoLimpio = JSON.stringify(svc).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
if (textoLimpio.includes("urgencia") || textoLimpio.includes("urgente") || textoLimpio.includes("por atencion")) {
if (todoElTexto.includes("atencion presencial urgencias") || todoElTexto.includes("atencion de la urgencia") || todoElTexto.includes("por atencion") || todoElTexto.includes('"urgente":"si"') || todoElTexto.includes('"urgencia":"si"')) {
esUrgente = true; esUrgente = true;
} }
const guildId = svc.guild_id || svc['guild_id'] || (svc.raw_data && svc.raw_data.guild_id); // DETECCIÓN DE GREMIO (Si no viene, lo buscamos nosotros)
let guildId = svc.guild_id || svc['guild_id'] || (svc.raw_data && svc.raw_data.guild_id);
if (!guildId) {
for (const g of allGuilds.rows) {
const keywords = Array.isArray(g.ia_keywords) ? g.ia_keywords : [];
if (keywords.some(kw => textoLimpio.includes(kw.toLowerCase()))) {
guildId = g.id;
break;
}
}
}
// GUARDAR EN BD
const insertRes = await pool.query(` const insertRes = await pool.query(`
INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, is_urgent) INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, is_urgent)
VALUES ($1, $2, $3, $4, $5) VALUES ($1, $2, $3, $4, $5)
@@ -1911,25 +1915,20 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
const newSvcId = insertRes.rows[0].id; const newSvcId = insertRes.rows[0].id;
const autoStatus = insertRes.rows[0].automation_status; const autoStatus = insertRes.rows[0].automation_status;
// 🕵️ CHIVATO: Esto aparecerá en tu consola de Coolify // 📢 ¡CHIVATO DE CONSOLA! (Si no ves esto en Coolify, es que el código no ha subido)
console.log(`[DEBUG-RECEPCION] Ref: ${ref} | Urgente: ${esUrgente} | Gremio: ${guildId} | Auto_ON: ${autoDispatchEnabled}`); console.log(`[DETECTOR-PRO] Ref: ${ref} | Urgente: ${esUrgente} | Gremio: ${guildId} | Auto: ${autoDispatchEnabled} | Status: ${autoStatus}`);
// 🔥 LANZAMIENTO AUTOMÁTICO 🔥 // 🔥 LANZAMIENTO AUTOMÁTICO 🔥
// Si es urgente, tiene gremio, el botón está ON y el servicio no ha sido procesado aún...
if (esUrgente && guildId && autoDispatchEnabled && (autoStatus === 'manual' || autoStatus === 'pending')) { if (esUrgente && guildId && autoDispatchEnabled && (autoStatus === 'manual' || autoStatus === 'pending')) {
console.log(`⚡ [AUTO-DISPATCH] ¡Hillmer detectada! Lanzando a la bolsa...`); console.log(`⚡ [AUTO-DISPATCH] Lanzando a la bolsa: ${ref}`);
const cpMatch = todoElTexto.match(/\b\d{5}\b/); const cpMatch = textoLimpio.match(/\b\d{5}\b/);
const cpFinal = cpMatch ? cpMatch[0] : "00000"; dispatchToBolsa(newSvcId, guildId, cpMatch ? cpMatch[0] : "00000", req.user.accountId, req.user.sub).catch(console.error);
// Usamos await para asegurar que se ejecute la lógica de bolsa
await dispatchToBolsa(newSvcId, guildId, cpFinal, req.user.accountId, req.user.sub);
} }
count++; count++;
} }
res.json({ ok: true, inserted: count }); res.json({ ok: true, inserted: count });
} catch (error) { } catch (error) {
console.error("❌ Error recibiendo servicios:", error); console.error("❌ Error grave en recepción:", error);
res.status(500).json({ ok: false, error: error.message }); res.status(500).json({ ok: false, error: error.message });
} }
}); });