Actualizar server.js
This commit is contained in:
63
server.js
63
server.js
@@ -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) => {
|
||||
try {
|
||||
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(
|
||||
"SELECT auto_dispatch FROM provider_credentials WHERE owner_id = $1 AND provider = $2",
|
||||
[req.user.accountId, provider]
|
||||
);
|
||||
const autoDispatchEnabled = credsQ.rowCount > 0 &&
|
||||
(credsQ.rows[0].auto_dispatch === true || credsQ.rows[0].auto_dispatch === 1 || credsQ.rows[0].auto_dispatch === '1');
|
||||
|
||||
// ✅ CORRECCIÓN: Aceptamos 1, '1', 't' o true como "Encendido"
|
||||
let autoDispatchEnabled = false;
|
||||
if (credsQ.rowCount > 0) {
|
||||
const val = credsQ.rows[0].auto_dispatch;
|
||||
autoDispatchEnabled = (val === true || val === 1 || val === '1' || val === 't');
|
||||
}
|
||||
// 2. Cargamos los gremios y sus palabras clave para detectar el gremio si falta
|
||||
const allGuilds = await pool.query("SELECT id, name, ia_keywords FROM guilds WHERE owner_id = $1", [req.user.accountId]);
|
||||
|
||||
let count = 0;
|
||||
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'] || 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;
|
||||
const todoElTexto = JSON.stringify(svc).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/\n/g, " ");
|
||||
|
||||
if (todoElTexto.includes("atencion presencial urgencias") || todoElTexto.includes("atencion de la urgencia") || todoElTexto.includes("por atencion") || todoElTexto.includes('"urgente":"si"') || todoElTexto.includes('"urgencia":"si"')) {
|
||||
const textoLimpio = JSON.stringify(svc).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
||||
if (textoLimpio.includes("urgencia") || textoLimpio.includes("urgente") || textoLimpio.includes("por atencion")) {
|
||||
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(`
|
||||
INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, is_urgent)
|
||||
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 autoStatus = insertRes.rows[0].automation_status;
|
||||
|
||||
// 🕵️ CHIVATO: Esto aparecerá en tu consola de Coolify
|
||||
console.log(`[DEBUG-RECEPCION] Ref: ${ref} | Urgente: ${esUrgente} | Gremio: ${guildId} | Auto_ON: ${autoDispatchEnabled}`);
|
||||
// 📢 ¡CHIVATO DE CONSOLA! (Si no ves esto en Coolify, es que el código no ha subido)
|
||||
console.log(`[DETECTOR-PRO] Ref: ${ref} | Urgente: ${esUrgente} | Gremio: ${guildId} | Auto: ${autoDispatchEnabled} | Status: ${autoStatus}`);
|
||||
|
||||
// 🔥 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')) {
|
||||
console.log(`⚡ [AUTO-DISPATCH] ¡Hillmer detectada! Lanzando a la bolsa...`);
|
||||
const cpMatch = todoElTexto.match(/\b\d{5}\b/);
|
||||
const cpFinal = cpMatch ? cpMatch[0] : "00000";
|
||||
|
||||
// Usamos await para asegurar que se ejecute la lógica de bolsa
|
||||
await dispatchToBolsa(newSvcId, guildId, cpFinal, req.user.accountId, req.user.sub);
|
||||
console.log(`⚡ [AUTO-DISPATCH] Lanzando a la bolsa: ${ref}`);
|
||||
const cpMatch = textoLimpio.match(/\b\d{5}\b/);
|
||||
dispatchToBolsa(newSvcId, guildId, cpMatch ? cpMatch[0] : "00000", req.user.accountId, req.user.sub).catch(console.error);
|
||||
}
|
||||
|
||||
count++;
|
||||
}
|
||||
res.json({ ok: true, inserted: count });
|
||||
} 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 });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user