Actualizar server.js

This commit is contained in:
2026-03-15 12:24:47 +00:00
parent dc0bb0bfdb
commit e704960fe0

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 POR DESCRIPCIÓN)
// ==========================================
app.post("/providers/scraped", authMiddleware, async (req, res) => {
try {
@@ -1832,7 +1832,7 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
for (const svc of services) {
// 1. EXTRAER REFERENCIA A PRUEBA DE BOMBAS (Incluye "SERVICIO" de HomeServe)
// 1. EXTRAER REFERENCIA (Soportando "SERVICIO" de HomeServe)
const ref = svc['service_ref']
|| svc['SERVICIO']
|| svc['Referencia']
@@ -1842,31 +1842,34 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
|| (svc.raw_data && (svc.raw_data['SERVICIO'] || svc.raw_data['Referencia'] || svc.raw_data['Nº Siniestro'] || svc.raw_data['Expediente'] || svc.raw_data['expediente']));
if (!ref) {
console.log("⚠️ Se omitió un servicio por no encontrar el número de referencia.");
console.log("⚠️ Se omitió un servicio por no encontrar referencia.");
continue;
}
// 2. DETECTOR DE URGENCIAS (APLASTAMIENTO TOTAL - CERO FALSOS POSITIVOS)
// 🔥 2. EL DETECTOR BRILLANTE (Solo busca en Descripción y Estados) 🔥
let esUrgente = false;
// Juntamos todos los valores del objeto en un solo string
const textoPlano = JSON.stringify(svc).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
// Caso HomeServe: frases mágicas exactas (cero falsos positivos)
if (
textoPlano.includes('atencion presencial urgencias') ||
textoPlano.includes('atencion de la urgencia')
) {
esUrgente = true;
} else {
// Caso Multiasistencia u otros: si la columna se llama Urgencia/Urgent y el valor es "si" o "true"
for (const key in svc) {
const col = String(key).toLowerCase();
const val = String(svc[key]).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
if ((col.includes('urgent') || col === 'urgencia') && (val === 'si' || val === 'true' || val === 'si')) {
esUrgente = true;
break;
}
const camposABuscar = [
svc['ACTUALMENTE EN'],
svc['Estado'],
svc['ESTADO'],
svc['Descripción'],
svc['DESCRIPCION'],
svc['Urgente'],
svc['URGENTE'],
svc.raw_data ? svc.raw_data['Descripción'] : null,
svc.raw_data ? svc.raw_data['DESCRIPCION'] : null,
svc.raw_data ? svc.raw_data['ACTUALMENTE EN'] : null
];
for (const texto of camposABuscar) {
if (!texto) continue;
// Lo pasamos a minúsculas y le quitamos los acentos
const val = String(texto).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
if (val.includes('urgencia') || val.includes('urgente')) {
esUrgente = true;
break; // Si lo encuentra, dejamos de buscar
}
}
@@ -1890,7 +1893,8 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
if (esUrgente && guildId && autoDispatchEnabled && (autoStatus === 'manual' || autoStatus === 'pending')) {
console.log(`⚡ [AUTO-DISPATCH] Lanzando urgencia ${ref} de ${provider} a la bolsa...`);
const cpMatch = textoPlano.match(/\b\d{5}\b/);
const todoElTexto = JSON.stringify(svc).toLowerCase();
const cpMatch = todoElTexto.match(/\b\d{5}\b/);
const cpFinal = cpMatch ? cpMatch[0] : "00000";
const port = process.env.PORT || 3000;