diff --git a/server.js b/server.js index 323200a..c8c4a74 100644 --- a/server.js +++ b/server.js @@ -1812,7 +1812,7 @@ app.post("/providers/credentials", authMiddleware, async (req, res) => { }); // ========================================== -// 📥 RECEPCIÓN DE SERVICIOS (EMBUDO INTELIGENTE CON AUTO-DESPACHO Y TODOTERRENO FINAL) +// 📥 RECEPCIÓN DE SERVICIOS (EMBUDO INTELIGENTE FINAL Y DEFINITIVO) // ========================================== app.post("/providers/scraped", authMiddleware, async (req, res) => { try { @@ -1833,15 +1833,20 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => { for (const svc of services) { - // 2. Extraer la referencia (revisamos la raíz y el raw_data por si viene anidado) - const ref = svc['Referencia'] || svc['Nº Siniestro'] || svc['Expediente'] || svc['service_ref'] || (svc.raw_data && svc.raw_data['Referencia']); - if (!ref) continue; + // 2. EXTRAER REFERENCIA (¡AQUÍ ESTABA EL FALLO! HomeServe lo llama "SERVICIO") + const ref = svc['service_ref'] + || svc['SERVICIO'] + || svc['Referencia'] + || svc['Nº Siniestro'] + || svc['Expediente'] + || (svc.raw_data && (svc.raw_data['SERVICIO'] || svc.raw_data['Referencia'] || svc.raw_data['Nº Siniestro'] || svc.raw_data['Expediente'])); + + if (!ref) continue; // Si no hay referencia, lo salta. ¡Por eso no actualizaba! // 🔥 3. DETECTOR DE URGENCIAS ANTI-FALLOS (APLASTAMIENTO TOTAL) 🔥 let esUrgente = false; - // Convertimos TODO el expediente (con todas sus subcapas) a un texto gigante, - // en minúsculas y sin acentos para que no se escape nada. + // Convertimos TODO el expediente (esté donde esté el dato) a un texto gigante const todoElTexto = JSON.stringify(svc).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, ""); if ( @@ -1852,10 +1857,10 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => { esUrgente = true; } - // 🕵️ 4. EXTRAER GREMIO (Ya viene en el objeto enviado) + // 🕵️ 4. EXTRAER GREMIO const guildId = svc.guild_id || svc['guild_id'] || (svc.raw_data && svc.raw_data.guild_id); - // 5. Guardamos en la Base de Datos + // 5. GUARDAMOS EN LA BASE DE DATOS const insertRes = await pool.query(` INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, is_urgent) VALUES ($1, $2, $3, $4, $5) @@ -1879,16 +1884,9 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => { const port = process.env.PORT || 3000; fetch(`http://127.0.0.1:${port}/providers/automate/${newSvcId}`, { method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': req.headers.authorization - }, - body: JSON.stringify({ - guild_id: guildId, - cp: cpFinal, - useDelay: false - }) - }).catch(e => console.error("Error en auto-despacho fetch:", e.message)); + headers: { 'Content-Type': 'application/json', 'Authorization': req.headers.authorization }, + body: JSON.stringify({ guild_id: guildId, cp: cpFinal, useDelay: false }) + }).catch(e => console.error("Error en auto-despacho:", e.message)); } count++;