Actualizar server.js

This commit is contained in:
2026-03-15 12:50:29 +00:00
parent f0715fc7f3
commit d80baf95b8

View File

@@ -1812,7 +1812,7 @@ app.post("/providers/credentials", authMiddleware, async (req, res) => {
}); });
// ========================================== // ==========================================
// 📥 RECEPCIÓN DE SERVICIOS (EMBUDO INTELIGENTE DEFINITIVO) // 📥 RECEPCIÓN DE SERVICIOS (EL DETECTOR DEFINITIVO Y MÁS LIMPIO)
// ========================================== // ==========================================
app.post("/providers/scraped", authMiddleware, async (req, res) => { app.post("/providers/scraped", authMiddleware, async (req, res) => {
try { try {
@@ -1846,43 +1846,30 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
continue; continue;
} }
// 🔥 2. DETECTOR DEFINITIVO Y A PRUEBA DE BALAS 🔥 // 🔥 2. DETECTOR DE URGENCIAS MODO DIOS 🔥
let esUrgente = false; let esUrgente = false;
const checkUrgencia = (obj) => { // Convertimos todo a un texto gigante, minúsculas, sin acentos ni símbolos raros
for (const key in obj) { const todoElTexto = JSON.stringify(svc)
if (typeof obj[key] === 'object') continue; .toLowerCase()
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.replace(/\n/g, " "); // Quitamos saltos de línea por si acaso
const col = String(key).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, ""); // 1. Detección HomeServe: Busca la frase clave de la Bandeja de Entrada o del Interior
const val = String(obj[key]).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, ""); if (todoElTexto.includes("atencion presencial urgencias") || todoElTexto.includes("atencion de la urgencia") || todoElTexto.includes("por atencion")) {
// Caso 1: Multiasistencia (La columna se llama "urgencia" y el valor es "si")
if ((col.includes('urgent') || col.includes('urgencia')) && (val === 'si' || val === 'true')) {
return true;
}
// Caso 2: HomeServe (Buscamos en Descripción, Estado, etc.)
// Para no caer en el falso positivo (Nivel de urgencia: Normal), ignoramos la caja si se llama "urgencia"
if (!col.includes('urgent') && !col.includes('urgencia')) {
// Si en la descripción o el estado pone urgente, urgencia o el texto cortado "por atencion"
if (val.includes('urgente') || val.includes('urgencia') || val.includes('por atencion')) {
return true;
}
}
}
return false;
};
// Comprobamos en la raíz y en los datos anidados
if (checkUrgencia(svc)) {
esUrgente = true;
} else if (svc.raw_data && checkUrgencia(svc.raw_data)) {
esUrgente = true; esUrgente = true;
} }
// 2. Detección Multiasistencia: Busca el combo "urgente":"si" literal en el JSON
if (todoElTexto.includes('"urgente":"si"') || todoElTexto.includes('"urgencia":"si"')) {
esUrgente = true;
}
// 3. Extracción de Gremio
const guildId = svc.guild_id || svc['guild_id'] || (svc.raw_data && svc.raw_data.guild_id); const guildId = svc.guild_id || svc['guild_id'] || (svc.raw_data && svc.raw_data.guild_id);
// 3. GUARDAMOS EN LA BD (CON MEMORIA ETERNA: OR EXCLUDED.is_urgent) // 4. GUARDAMOS EN LA BD (CON MEMORIA ETERNA: OR EXCLUDED.is_urgent)
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)
@@ -1896,11 +1883,10 @@ 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;
// 4. AUTO-DESPACHO // 5. AUTO-DESPACHO
if (esUrgente && guildId && autoDispatchEnabled && (autoStatus === 'manual' || autoStatus === 'pending')) { if (esUrgente && guildId && autoDispatchEnabled && (autoStatus === 'manual' || autoStatus === 'pending')) {
console.log(`⚡ [AUTO-DISPATCH] Lanzando urgencia ${ref} a la bolsa...`); console.log(`⚡ [AUTO-DISPATCH] Lanzando urgencia ${ref} a la bolsa...`);
const todoElTexto = JSON.stringify(svc).toLowerCase();
const cpMatch = todoElTexto.match(/\b\d{5}\b/); const cpMatch = todoElTexto.match(/\b\d{5}\b/);
const cpFinal = cpMatch ? cpMatch[0] : "00000"; const cpFinal = cpMatch ? cpMatch[0] : "00000";