Actualizar server.js
This commit is contained in:
55
server.js
55
server.js
@@ -1812,7 +1812,7 @@ app.post("/providers/credentials", authMiddleware, async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 📥 RECEPCIÓN DE SERVICIOS (EL DETECTOR DEFINITIVO POR DESCRIPCIÓN Y TEXTOS CORTADOS)
|
// 📥 RECEPCIÓN DE SERVICIOS (EMBUDO INTELIGENTE DEFINITIVO)
|
||||||
// ==========================================
|
// ==========================================
|
||||||
app.post("/providers/scraped", authMiddleware, async (req, res) => {
|
app.post("/providers/scraped", authMiddleware, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@@ -1832,7 +1832,7 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
|
|||||||
|
|
||||||
for (const svc of services) {
|
for (const svc of services) {
|
||||||
|
|
||||||
// 1. EXTRAER REFERENCIA (Soportando "SERVICIO" de HomeServe)
|
// 1. EXTRAER REFERENCIA A PRUEBA DE BOMBAS
|
||||||
const ref = svc['service_ref']
|
const ref = svc['service_ref']
|
||||||
|| svc['SERVICIO']
|
|| svc['SERVICIO']
|
||||||
|| svc['Referencia']
|
|| svc['Referencia']
|
||||||
@@ -1842,35 +1842,42 @@ 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']));
|
|| (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) {
|
if (!ref) {
|
||||||
console.log("⚠️ Se omitió un servicio por no encontrar referencia.");
|
console.log("⚠️ Se omitió un servicio por no encontrar el número de referencia.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 🔥 2. EL DETECTOR BRILLANTE (Busca la palabra cortada de la Bandeja de Entrada) 🔥
|
// 🔥 2. DETECTOR DEFINITIVO Y A PRUEBA DE BALAS 🔥
|
||||||
let esUrgente = false;
|
let esUrgente = false;
|
||||||
|
|
||||||
const camposABuscar = [
|
const checkUrgencia = (obj) => {
|
||||||
svc['ACTUALMENTE EN'],
|
for (const key in obj) {
|
||||||
svc['Estado'],
|
if (typeof obj[key] === 'object') continue;
|
||||||
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) {
|
const col = String(key).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
||||||
if (!texto) continue;
|
const val = String(obj[key]).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
||||||
const val = String(texto).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
|
||||||
|
|
||||||
// ¡AQUÍ ESTÁ LA MAGIA! Si la tabla lo corta y deja "por atencion", lo pilla.
|
// Caso 1: Multiasistencia (La columna se llama "urgencia" y el valor es "si")
|
||||||
if (val.includes('urgencia') || val.includes('urgente') || val.includes('por atencion')) {
|
if ((col.includes('urgent') || col.includes('urgencia')) && (val === 'si' || val === 'true')) {
|
||||||
esUrgente = true;
|
return true;
|
||||||
break;
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
@@ -1891,7 +1898,7 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
|
|||||||
|
|
||||||
// 4. AUTO-DESPACHO
|
// 4. 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} de ${provider} a la bolsa...`);
|
console.log(`⚡ [AUTO-DISPATCH] Lanzando urgencia ${ref} a la bolsa...`);
|
||||||
|
|
||||||
const todoElTexto = JSON.stringify(svc).toLowerCase();
|
const todoElTexto = JSON.stringify(svc).toLowerCase();
|
||||||
const cpMatch = todoElTexto.match(/\b\d{5}\b/);
|
const cpMatch = todoElTexto.match(/\b\d{5}\b/);
|
||||||
|
|||||||
Reference in New Issue
Block a user