Arregla corte de descripcion en Multiasistencia
This commit is contained in:
621
robot.js
621
robot.js
@@ -1,303 +1,179 @@
|
|||||||
import { chromium } from 'playwright';
|
import { chromium } from 'playwright';
|
||||||
import pg from 'pg';
|
import pg from 'pg';
|
||||||
|
|
||||||
|
// En Coolify las variables se inyectan solas
|
||||||
const { DATABASE_URL } = process.env;
|
const { DATABASE_URL } = process.env;
|
||||||
if (!DATABASE_URL) { console.error("❌ Error: No hay DATABASE_URL."); process.exit(1); }
|
|
||||||
|
if (!DATABASE_URL) {
|
||||||
|
console.error("❌ Error: No hay DATABASE_URL definida.");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
const pool = new pg.Pool({ connectionString: DATABASE_URL, ssl: false });
|
const pool = new pg.Pool({ connectionString: DATABASE_URL, ssl: false });
|
||||||
const HEADLESS = true;
|
|
||||||
|
|
||||||
// ==========================================
|
// CONFIGURACIÓN
|
||||||
// 🧠 MOTOR DE CLASIFICACIÓN DE GREMIOS PRO (AHORA DINÁMICO)
|
const HEADLESS = true; // En servidor SIEMPRE true
|
||||||
// ==========================================
|
|
||||||
|
|
||||||
function normalizarTexto(texto) {
|
|
||||||
if (!texto) return "";
|
|
||||||
return texto.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 🛠️ NUEVO: El "Gira-Nombres" EXCLUSIVO para HomeServe
|
|
||||||
function arreglarNombre(nombreRaw) {
|
|
||||||
if (!nombreRaw) return "";
|
|
||||||
let nombreLimpio = nombreRaw.trim();
|
|
||||||
// Si detectamos que tiene una coma...
|
|
||||||
if (nombreLimpio.includes(',')) {
|
|
||||||
let partes = nombreLimpio.split(',');
|
|
||||||
let apellidos = partes[0].trim();
|
|
||||||
let nombre = partes[1].trim();
|
|
||||||
// Le damos la vuelta y quitamos espacios sobrantes
|
|
||||||
nombreLimpio = `${nombre} ${apellidos}`.replace(/\s+/g, ' ');
|
|
||||||
}
|
|
||||||
return nombreLimpio;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 🛡️ DESCODIFICADOR UNIVERSAL PARA HOMESERVE (Arregla los +, Ó, Ñ, etc.)
|
|
||||||
function arreglarCodificacion(texto) {
|
|
||||||
if (!texto) return "";
|
|
||||||
let limpio = texto.replace(/\+/g, ' '); // Primero cambiamos los + por espacios
|
|
||||||
try {
|
|
||||||
// Truco clásico de Javascript para transformar ISO a UTF-8
|
|
||||||
limpio = decodeURIComponent(escape(limpio));
|
|
||||||
} catch(e) {
|
|
||||||
// Si no se puede, lo dejamos con los espacios arreglados
|
|
||||||
}
|
|
||||||
return limpio.trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
// NUEVO: La función ahora lee las keywords directamente del JSON de la base de datos
|
|
||||||
function clasificarGremio(descripcion, gremiosActivosDB) {
|
|
||||||
if (!descripcion || gremiosActivosDB.length === 0) return null;
|
|
||||||
|
|
||||||
const descNormalizada = normalizarTexto(descripcion);
|
|
||||||
|
|
||||||
for (const gremio of gremiosActivosDB) {
|
|
||||||
// Obtenemos las palabras clave guardadas desde el panel de configuración
|
|
||||||
const keywords = Array.isArray(gremio.ia_keywords) ? gremio.ia_keywords : [];
|
|
||||||
|
|
||||||
if (keywords.length === 0) continue; // Si no tiene reglas, pasamos al siguiente
|
|
||||||
|
|
||||||
// Comprobamos si alguna palabra clave coincide con la descripción
|
|
||||||
const coincide = keywords.some(kw => descNormalizada.includes(normalizarTexto(kw)));
|
|
||||||
|
|
||||||
if (coincide) {
|
|
||||||
console.log(` 🧠 Gremio detectado automáticamente: ${gremio.name} (ID: ${gremio.id})`);
|
|
||||||
return gremio.id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Función auxiliar para reintentar la navegación si hay fallo de red
|
|
||||||
async function gotoWithRetry(page, url, retries = 3) {
|
|
||||||
for (let i = 0; i < retries; i++) {
|
|
||||||
try {
|
|
||||||
// Usamos domcontentloaded para que no se quede colgado esperando imágenes o scripts pesados
|
|
||||||
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 45000 });
|
|
||||||
return; // Si funciona, salimos de la función
|
|
||||||
} catch (e) {
|
|
||||||
if (i === retries - 1) throw e; // Si es el último intento, lanza el error
|
|
||||||
console.log(`⚠️ Fallo de red detectado al ir a ${url}. Reintentando (${i + 1}/${retries})...`);
|
|
||||||
await page.waitForTimeout(3000); // Esperamos 3 segundos antes de volver a intentar
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log("🤖 ROBOT MODO: CIRUJANO + ASPIRADORA + CLASIFICADOR PRO");
|
console.log("🤖 INICIANDO ROBOT UNIFICADO (MEJORADO)...");
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const client = await pool.connect();
|
const client = await pool.connect();
|
||||||
try {
|
try {
|
||||||
// 1. CARGAMOS LOS GREMIOS ACTUALES (AHORA INCLUYE LAS PALABRAS CLAVE)
|
// 1. Obtener credenciales activas
|
||||||
const gremiosRes = await client.query("SELECT id, name, ia_keywords FROM guilds");
|
|
||||||
const gremiosDB = gremiosRes.rows;
|
|
||||||
|
|
||||||
await client.query(`
|
|
||||||
DO $$ BEGIN
|
|
||||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='scraped_services' AND column_name='is_urgent') THEN
|
|
||||||
ALTER TABLE scraped_services ADD COLUMN is_urgent BOOLEAN DEFAULT FALSE;
|
|
||||||
END IF;
|
|
||||||
-- AÑADIDO: Aseguramos que exista la columna ia_keywords en los gremios
|
|
||||||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='guilds' AND column_name='ia_keywords') THEN
|
|
||||||
ALTER TABLE guilds ADD COLUMN ia_keywords JSONB DEFAULT '[]';
|
|
||||||
END IF;
|
|
||||||
END $$;
|
|
||||||
`);
|
|
||||||
|
|
||||||
const res = await client.query("SELECT * FROM provider_credentials WHERE status = 'active'");
|
const res = await client.query("SELECT * FROM provider_credentials WHERE status = 'active'");
|
||||||
for (const cred of res.rows) {
|
const credentials = res.rows;
|
||||||
let password = Buffer.from(cred.password_hash, 'base64').toString('utf-8');
|
|
||||||
console.log(`\n🔄 Procesando ${cred.provider.toUpperCase()}...`);
|
console.log(`📋 Procesando ${credentials.length} cuentas de proveedores...`);
|
||||||
|
|
||||||
if (cred.provider === 'multiasistencia') {
|
for (const cred of credentials) {
|
||||||
await runMultiasistencia(cred.owner_id, cred.username, password, gremiosDB);
|
let password = "";
|
||||||
} else if (cred.provider === 'homeserve') {
|
try {
|
||||||
await runHomeserve(cred.owner_id, cred.username, password, gremiosDB);
|
password = Buffer.from(cred.password_hash, 'base64').toString('utf-8');
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`Error decodificando pass para usuario ${cred.owner_id}`);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`\n🔄 Sincronizando ${cred.provider.toUpperCase()} para usuario ID ${cred.owner_id}...`);
|
||||||
|
|
||||||
|
if (cred.provider === 'multiasistencia') {
|
||||||
|
await runMultiasistencia(cred.owner_id, cred.username, password);
|
||||||
|
} else if (cred.provider === 'homeserve') {
|
||||||
|
await runHomeserve(cred.owner_id, cred.username, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualizar timestamp
|
||||||
await client.query("UPDATE provider_credentials SET last_sync = NOW() WHERE id = $1", [cred.id]);
|
await client.query("UPDATE provider_credentials SET last_sync = NOW() WHERE id = $1", [cred.id]);
|
||||||
}
|
}
|
||||||
} catch (e) { console.error("❌ Error ciclo:", e.message); }
|
|
||||||
finally { client.release(); }
|
} catch (e) {
|
||||||
|
console.error("❌ Error en ciclo principal:", e);
|
||||||
|
} finally {
|
||||||
|
client.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ESPERA DE 15 MINUTOS
|
||||||
console.log("\n💤 Durmiendo 15 minutos...");
|
console.log("\n💤 Durmiendo 15 minutos...");
|
||||||
await new Promise(r => setTimeout(r, 15 * 60 * 1000));
|
await new Promise(r => setTimeout(r, 15 * 60 * 1000));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 🏥 MULTIASISTENCIA (CON REINTENTOS ANTI-CAÍDAS)
|
// 🛠️ ROBOT MULTIASISTENCIA (CORREGIDO)
|
||||||
// ==========================================
|
// ==========================================
|
||||||
async function runMultiasistencia(ownerId, user, pass, gremiosDB) {
|
async function runMultiasistencia(ownerId, user, pass) {
|
||||||
const browser = await chromium.launch({
|
const browser = await chromium.launch({ headless: HEADLESS, args: ['--no-sandbox'] });
|
||||||
headless: HEADLESS,
|
|
||||||
args: [
|
|
||||||
'--no-sandbox',
|
|
||||||
'--disable-setuid-sandbox',
|
|
||||||
'--disable-dev-shm-usage',
|
|
||||||
'--disable-gpu'
|
|
||||||
]
|
|
||||||
});
|
|
||||||
const context = await browser.newContext();
|
const context = await browser.newContext();
|
||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// INICIO DE SESIÓN CON REINTENTOS
|
console.log("🌍 [Multi] Conectando...");
|
||||||
await gotoWithRetry(page, 'https://web.multiasistencia.com/w3multi/acceso.php');
|
await page.goto('https://web.multiasistencia.com/w3multi/acceso.php', { timeout: 60000 });
|
||||||
|
|
||||||
await page.fill('input[name="usuario"]', user);
|
|
||||||
await page.fill('input[type="password"]', pass);
|
|
||||||
await page.click('input[type="submit"]');
|
|
||||||
await page.waitForTimeout(4000);
|
|
||||||
|
|
||||||
// ENTRAR AL BUZÓN CON REINTENTOS
|
|
||||||
await gotoWithRetry(page, 'https://web.multiasistencia.com/w3multi/frepasos_new.php?refresh=1');
|
|
||||||
await page.waitForTimeout(3000);
|
|
||||||
|
|
||||||
console.log("🔄 [Multi] Forzando recarga segura mediante script interno...");
|
// Login
|
||||||
try {
|
const userInput = await page.$('input[name="usuario"]') || await page.$('input[type="text"]');
|
||||||
await page.evaluate(() => {
|
if(userInput) {
|
||||||
if (typeof refrescar === 'function') refrescar();
|
await userInput.fill(user);
|
||||||
});
|
await page.fill('input[type="password"]', pass);
|
||||||
await page.waitForLoadState('networkidle');
|
await page.click('input[type="submit"]');
|
||||||
await page.waitForTimeout(3000);
|
await page.waitForTimeout(4000);
|
||||||
} catch (e) {}
|
|
||||||
|
|
||||||
// --- BUCLE DE PAGINACIÓN ---
|
|
||||||
let todosExpedientes = new Set();
|
|
||||||
let paginaActual = 1;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
console.log(`📄 [Multi] Escaneando página ${paginaActual}...`);
|
|
||||||
|
|
||||||
const expedientesPagina = await page.evaluate(() => {
|
|
||||||
const links = Array.from(document.querySelectorAll('a[href*="reparacion="]'));
|
|
||||||
return links.map(a => a.href.match(/reparacion=(\d+)/)?.[1]).filter(Boolean);
|
|
||||||
});
|
|
||||||
|
|
||||||
expedientesPagina.forEach(ref => todosExpedientes.add(ref));
|
|
||||||
|
|
||||||
// 🛑 NUEVO: Buscamos el botón de siguiente
|
|
||||||
const hasNextPage = await page.evaluate(() => {
|
|
||||||
const links = Array.from(document.querySelectorAll('a.lnkheader'));
|
|
||||||
return links.some(a => a.innerText.trim() === 'Página siguiente');
|
|
||||||
});
|
|
||||||
|
|
||||||
if (hasNextPage) {
|
|
||||||
// 🛑 NUEVO: Pulsamos usando el motor de Playwright, simulando click humano
|
|
||||||
await page.locator('a.lnkheader:has-text("Página siguiente")').first().click();
|
|
||||||
await page.waitForTimeout(3000);
|
|
||||||
paginaActual++;
|
|
||||||
if(paginaActual > 15) {
|
|
||||||
console.log("⚠️ [Multi] Límite de 15 páginas alcanzado por seguridad.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log("🛑 [Multi] No hay más páginas.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const expedientes = Array.from(todosExpedientes);
|
// Ir a listado
|
||||||
console.log(`✅ [Multi] Total expedientes detectados: ${expedientes.length}`);
|
await page.goto('https://web.multiasistencia.com/w3multi/frepasos_new.php?refresh=1', { waitUntil: 'domcontentloaded' });
|
||||||
|
|
||||||
|
// Sacar expedientes
|
||||||
|
const expedientes = await page.evaluate(() => {
|
||||||
|
const links = Array.from(document.querySelectorAll('a[href*="reparacion="]'));
|
||||||
|
return Array.from(new Set(links.map(a => a.href.match(/reparacion=(\d+)/)?.[1]).filter(Boolean)));
|
||||||
|
});
|
||||||
|
|
||||||
if (expedientes.length > 0) {
|
console.log(`🔍 [Multi] ${expedientes.length} expedientes encontrados.`);
|
||||||
await syncAndArchive(ownerId, 'multiasistencia', expedientes);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const ref of expedientes) {
|
for (const ref of expedientes) {
|
||||||
await gotoWithRetry(page, `https://web.multiasistencia.com/w3multi/repasos1.php?reparacion=${ref}`);
|
await page.goto(`https://web.multiasistencia.com/w3multi/repasos1.php?reparacion=${ref}`, { waitUntil: 'domcontentloaded' });
|
||||||
await page.waitForTimeout(1500);
|
|
||||||
let scrapData = null;
|
|
||||||
|
|
||||||
for (const frame of page.frames()) {
|
// --- NUEVA LÓGICA DE EXTRACCIÓN POR CELDAS ---
|
||||||
try {
|
const data = await page.evaluate(() => {
|
||||||
scrapData = await frame.evaluate(() => {
|
|
||||||
const clean = (t) => t ? t.replace(/\s+/g, ' ').trim() : "";
|
// Función auxiliar para buscar en celdas vecinas
|
||||||
const body = document.body?.innerText || "";
|
const findValueByHeader = (keywords) => {
|
||||||
if (!body.includes("Nombre Cliente") && !body.includes("Asegurado")) return null;
|
const cells = Array.from(document.querySelectorAll('td, th'));
|
||||||
|
for (const cell of cells) {
|
||||||
const cabeceras = Array.from(document.querySelectorAll('.tcab'));
|
const text = (cell.innerText || "").toUpperCase().trim();
|
||||||
const detalles = Array.from(document.querySelectorAll('.tdet'));
|
// Si la celda contiene una de las palabras clave
|
||||||
const findByCab = (texto) => {
|
if (keywords.some(k => text.includes(k.toUpperCase()))) {
|
||||||
const idx = cabeceras.findIndex(el => el.innerText.includes(texto));
|
// Devolvemos el texto de la celda SIGUIENTE
|
||||||
return idx !== -1 && detalles[idx] ? clean(detalles[idx].innerText) : null;
|
const nextCell = cell.nextElementSibling;
|
||||||
};
|
if (nextCell) return nextCell.innerText.trim();
|
||||||
|
|
||||||
let rawDesc = "";
|
|
||||||
const descHeader = Array.from(document.querySelectorAll('td.tcab')).find(td => td.innerText.includes("Descripción de la Reparación"));
|
|
||||||
if (descHeader && descHeader.nextElementSibling) rawDesc = clean(descHeader.nextElementSibling.innerText);
|
|
||||||
|
|
||||||
// CORRECCIÓN: Borrado el recorte de fechas destructivo.
|
|
||||||
// Ahora le pasamos todo el bloque de texto completo a la IA para que no se pierdan palabras clave.
|
|
||||||
const cleanDesc = rawDesc;
|
|
||||||
|
|
||||||
const rawCPField = findByCab("Distrito Postal") || "";
|
|
||||||
const cpMatch = rawCPField.match(/\b\d{5}\b/);
|
|
||||||
const cpOnly = cpMatch ? cpMatch[0] : "";
|
|
||||||
const popOnly = rawCPField.replace(cpOnly, '').replace('-', '').trim();
|
|
||||||
|
|
||||||
// 📞 EXTRAER TELÉFONO REAL (Evita coger el de la oficina)
|
|
||||||
let telefonoReal = "";
|
|
||||||
const titulosDiv = Array.from(document.querySelectorAll('.subtitulo'));
|
|
||||||
const divTelefono = titulosDiv.find(div => div.innerText && div.innerText.includes('Teléfono'));
|
|
||||||
if (divTelefono && divTelefono.nextElementSibling) {
|
|
||||||
const celdaNum = divTelefono.nextElementSibling.querySelector('.tdet');
|
|
||||||
if (celdaNum) telefonoReal = celdaNum.innerText.replace(/[^0-9]/g, '');
|
|
||||||
}
|
|
||||||
// Fallback por si acaso falla, usamos el que tenías antes
|
|
||||||
if (!telefonoReal || telefonoReal.length < 9) {
|
|
||||||
telefonoReal = (document.body.innerText.match(/[6789]\d{8}/) || [])[0] || "";
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
// Función auxiliar para descripción (busca bloques de texto grandes)
|
||||||
"Expediente": findByCab("Número Reparación") || "",
|
const findDescription = () => {
|
||||||
"Nombre Cliente": findByCab("Nombre Cliente") || "",
|
// Intento 1: Buscar celda "Descripción"
|
||||||
"Dirección": findByCab("Dirección") || "",
|
let desc = findValueByHeader(['Descripción', 'Daños', 'Solicitud']);
|
||||||
"Población": popOnly,
|
if(desc) return desc;
|
||||||
"Código Postal": cpOnly,
|
|
||||||
"Compañía": "MULTI - " + (findByCab("Procedencia") || "MULTIASISTENCIA"),
|
// Intento 2: Buscar en el cuerpo general si falla la tabla
|
||||||
"Descripción": cleanDesc,
|
// 🩹 FIX: antes se cortaba en el PRIMER salto de línea que hubiera después
|
||||||
"Teléfono": telefonoReal,
|
// de "Descripción" ([^\n]+), así que muchas veces el texto quedaba a medias.
|
||||||
"Estado": findByCab("Estado") || "", // 🛑 AÑADIDO EL ESTADO AQUÍ
|
// Ahora cogemos todo el texto hasta la siguiente etiqueta de campo conocida
|
||||||
"Urgente": findByCab("Urgente") || "No"
|
// (o una línea en blanco), con un límite de seguridad por si no encuentra ninguna.
|
||||||
};
|
const body = document.body.innerText;
|
||||||
});
|
const match = body.match(/Descripción\s*[:\-]?\s*([\s\S]*?)(?:\n\s*\n|Observaciones|Dirección|Domicilio|Población|Localidad|Tel[eé]fono[s]?|Fecha|Reparador|Gremio|C\.?P\.?\b|$)/i);
|
||||||
if (scrapData && scrapData['Nombre Cliente']) break;
|
if (!match) return "";
|
||||||
} catch (e) { continue; }
|
return match[1].replace(/[ \t]+/g, ' ').trim().substring(0, 3000);
|
||||||
}
|
|
||||||
|
|
||||||
if (scrapData && scrapData['Nombre Cliente']) {
|
|
||||||
const idGremioDetectado = clasificarGremio(scrapData['Descripción'], gremiosDB);
|
|
||||||
if (idGremioDetectado) {
|
|
||||||
scrapData['guild_id'] = idGremioDetectado;
|
|
||||||
}
|
}
|
||||||
await saveServiceToDB(ownerId, 'multiasistencia', ref, scrapData);
|
|
||||||
|
// Extracción más precisa
|
||||||
|
let address = findValueByHeader(['Dirección', 'Domicilio', 'Riesgo']);
|
||||||
|
let pob = findValueByHeader(['Población', 'Localidad']);
|
||||||
|
let cp = findValueByHeader(['C.P.', 'Postal']);
|
||||||
|
|
||||||
|
// Limpieza de dirección (quitar "Baremo" si se cuela)
|
||||||
|
if (address.includes("Baremo")) address = address.split("Baremo")[0].trim();
|
||||||
|
|
||||||
|
// Unir dirección completa
|
||||||
|
let fullAddress = address;
|
||||||
|
if (pob) fullAddress += `, ${pob}`;
|
||||||
|
if (cp) fullAddress += ` (${cp})`;
|
||||||
|
|
||||||
|
return {
|
||||||
|
clientName: findValueByHeader(['Nombre Cliente', 'Asegurado']) || "Desconocido",
|
||||||
|
address: fullAddress || "Sin dirección detectada",
|
||||||
|
phone: (document.body.innerText.match(/[6789]\d{8}/) || [])[0] || "",
|
||||||
|
description: findDescription()
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
if (data.clientName) {
|
||||||
|
// Solo guardamos si tenemos al menos un nombre
|
||||||
|
await saveServiceToDB(ownerId, 'multiasistencia', ref, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
console.error("❌ Error Multi:", e.message);
|
} catch (e) {
|
||||||
} finally {
|
console.error("❌ [Multi] Error:", e.message);
|
||||||
await browser.close();
|
} finally {
|
||||||
|
await browser.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 🧹 HOMESERVE (ROBOT MEJORADO Y DETECTOR DE URGENCIAS)
|
// 🛠️ ROBOT HOMESERVE (MANTENIDO)
|
||||||
// ==========================================
|
// ==========================================
|
||||||
async function runHomeserve(ownerId, user, pass, gremiosDB) {
|
async function runHomeserve(ownerId, user, pass) {
|
||||||
const browser = await chromium.launch({
|
const browser = await chromium.launch({ headless: HEADLESS, args: ['--no-sandbox'] });
|
||||||
headless: HEADLESS,
|
|
||||||
args: [
|
|
||||||
'--no-sandbox',
|
|
||||||
'--disable-setuid-sandbox',
|
|
||||||
'--disable-dev-shm-usage',
|
|
||||||
'--disable-gpu'
|
|
||||||
]
|
|
||||||
});
|
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log("🌍 [HomeServe] Entrando...");
|
console.log("🌍 [HomeServe] Conectando...");
|
||||||
await gotoWithRetry(page, 'https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS');
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS', { timeout: 60000 });
|
||||||
|
|
||||||
if (await page.isVisible('input[name="CODIGO"]')) {
|
if (await page.isVisible('input[name="CODIGO"]')) {
|
||||||
await page.fill('input[name="CODIGO"]', user);
|
await page.fill('input[name="CODIGO"]', user);
|
||||||
await page.fill('input[type="password"]', pass);
|
await page.fill('input[type="password"]', pass);
|
||||||
@@ -305,219 +181,70 @@ async function runHomeserve(ownerId, user, pass, gremiosDB) {
|
|||||||
await page.waitForTimeout(5000);
|
await page.waitForTimeout(5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
await gotoWithRetry(page, 'https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=lista_servicios_total');
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=lista_servicios_total');
|
||||||
await page.waitForTimeout(3000);
|
|
||||||
|
const refs = await page.evaluate(() => {
|
||||||
// 🛑 NUEVO: Ahora extraemos la referencia Y LOS ICONOS de la misma pasada
|
const filas = Array.from(document.querySelectorAll('table tr'));
|
||||||
const listaConIconos = await page.evaluate(() => {
|
return filas.map(tr => tr.querySelector('td')?.innerText.trim()).filter(t => /^\d{4,}$/.test(t));
|
||||||
const results = [];
|
|
||||||
const rows = Array.from(document.querySelectorAll('table[bgcolor="#FCF4D6"] tr'));
|
|
||||||
|
|
||||||
rows.forEach(tr => {
|
|
||||||
const firstTd = tr.querySelector('td');
|
|
||||||
if (!firstTd) return;
|
|
||||||
|
|
||||||
const textMatch = firstTd.innerText.trim().match(/^15\d{6}$/);
|
|
||||||
const aMatch = firstTd.querySelector('a') ? firstTd.querySelector('a').innerText.trim().match(/^15\d{6}$/) : null;
|
|
||||||
const ref = textMatch ? textMatch[0] : (aMatch ? aMatch[0] : null);
|
|
||||||
|
|
||||||
if (ref) {
|
|
||||||
const hasLock = firstTd.querySelector('img[src*="candado.gif"]') !== null;
|
|
||||||
const hasEyes = firstTd.querySelector('img[src*="ojos.gif"]') !== null;
|
|
||||||
results.push({ ref, hasLock, hasEyes });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return results;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Sacamos solo los números para la escoba (syncAndArchive)
|
console.log(`🔍 [HomeServe] ${refs.length} expedientes encontrados.`);
|
||||||
const refs = [...new Set(listaConIconos.map(item => item.ref))];
|
|
||||||
|
|
||||||
if (refs.length > 0) {
|
|
||||||
await syncAndArchive(ownerId, 'homeserve', refs);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`🔍 [HomeServe] ${refs.length} expedientes detectados.`);
|
|
||||||
|
|
||||||
for (const ref of refs) {
|
for (const ref of refs) {
|
||||||
await gotoWithRetry(page, `https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=ver_servicioencurso&Servicio=${ref}`);
|
try {
|
||||||
await page.waitForTimeout(2000);
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=lista_servicios_total');
|
||||||
|
await page.click(`text="${ref}"`, { timeout: 5000 });
|
||||||
|
await page.waitForTimeout(1000);
|
||||||
|
|
||||||
const scrapData = await page.evaluate(() => {
|
const data = await page.evaluate(() => {
|
||||||
const d = {};
|
const rows = Array.from(document.querySelectorAll('tr'));
|
||||||
let isUrgent = "No"; // <-- EMPEZAMOS ASUMIENDO QUE NO ES URGENTE
|
const d = {};
|
||||||
|
rows.forEach(r => {
|
||||||
const rows = Array.from(document.querySelectorAll('tr'));
|
const cells = r.querySelectorAll('td');
|
||||||
rows.forEach(r => {
|
if(cells.length > 1) {
|
||||||
const cells = r.querySelectorAll('td');
|
const k = cells[0].innerText.toUpperCase();
|
||||||
if (cells.length >= 2) {
|
const v = cells[1].innerText;
|
||||||
const k = cells[0].innerText.toUpperCase().trim().replace(':', '');
|
if(k.includes("CLIENTE")) d.clientName = v;
|
||||||
const v = cells[1].innerText.trim();
|
if(k.includes("DOMICILIO")) d.address = v;
|
||||||
|
if(k.includes("POBLACION")) d.address += ", " + v;
|
||||||
if (k.includes("COMENTARIOS")) {
|
if(k.includes("TELEFONOS")) d.phone = v;
|
||||||
const inputEl = cells[1].querySelector('textarea');
|
if(k.includes("COMENTARIOS") || k.includes("AVERIA")) d.description = v;
|
||||||
const txt = inputEl ? inputEl.value : (cells[1].innerText || "");
|
|
||||||
|
|
||||||
// 🔥 DETECTOR DE URGENCIA ANTES DE LIMPIAR 🔥
|
|
||||||
const txtLower = txt.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
|
||||||
if (txtLower.includes("atencion presencial urgencias") || txtLower.includes("atencion de la urgencia") || txtLower.includes("urgente")) {
|
|
||||||
isUrgent = "Sí"; // Si pilla la urgencia, la guarda.
|
|
||||||
}
|
|
||||||
|
|
||||||
const cleanDesc = txt.split('\n').filter(line => {
|
|
||||||
const l = line.toUpperCase();
|
|
||||||
// Quitamos los filtros destructivos que borraban el "Cambio de estado"
|
|
||||||
return !["ESTADO ASIGNADO", "SMS NO ENVIADO", "CONTACTO CON PROF", "0000"].some(b => l.includes(b));
|
|
||||||
}).join('\n').trim();
|
|
||||||
// Aplicamos el descodificador a la avería
|
|
||||||
d['Descripción'] = cleanDesc; // Se limpiará luego abajo
|
|
||||||
} else if (k.length > 1 && v.length > 0 && !k.includes("MENU")) {
|
|
||||||
d[k] = v;
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
return d;
|
||||||
});
|
});
|
||||||
|
|
||||||
const rawPop = d['POBLACION-PROVINCIA'] || "";
|
|
||||||
const cpMatch = rawPop.match(/\b\d{5}\b/);
|
|
||||||
d['Código Postal'] = cpMatch ? cpMatch[0] : "";
|
|
||||||
d['Población'] = rawPop.replace(d['Código Postal'], '').replace('-', '').trim();
|
|
||||||
d['Compañía'] = "HOME - " + (d['COMPAÑIA'] || "HOMESERVE");
|
|
||||||
d['Nombre Cliente'] = d['CLIENTE'] || "";
|
|
||||||
d['Dirección'] = d['DOMICILIO'] || "";
|
|
||||||
|
|
||||||
// INYECTAMOS LA URGENCIA AL ESTILO MULTIASISTENCIA
|
|
||||||
d['Urgente'] = isUrgent;
|
|
||||||
|
|
||||||
return d;
|
if (data.clientName) {
|
||||||
});
|
await saveServiceToDB(ownerId, 'homeserve', ref, data);
|
||||||
|
|
||||||
if (scrapData && scrapData['Nombre Cliente']) {
|
|
||||||
// 🪄 Arreglamos la codificación alienígena de HomeServe EN TODOS LOS CAMPOS IMPORTANTES
|
|
||||||
scrapData['Nombre Cliente'] = arreglarCodificacion(scrapData['Nombre Cliente']);
|
|
||||||
scrapData['Dirección'] = arreglarCodificacion(scrapData['Dirección']);
|
|
||||||
scrapData['Población'] = arreglarCodificacion(scrapData['Población']);
|
|
||||||
scrapData['Descripción'] = arreglarCodificacion(scrapData['Descripción']);
|
|
||||||
|
|
||||||
// 🪄 Arreglamos el formato de "Apellidos, Nombre" (Solo en HomeServe)
|
|
||||||
scrapData['Nombre Cliente'] = arreglarNombre(scrapData['Nombre Cliente']);
|
|
||||||
|
|
||||||
// 🛑 NUEVO: Le inyectamos los iconos que cazamos antes en la lista
|
|
||||||
const iconInfo = listaConIconos.find(item => item.ref === ref);
|
|
||||||
if (iconInfo) {
|
|
||||||
scrapData['has_lock'] = iconInfo.hasLock;
|
|
||||||
scrapData['has_eyes'] = iconInfo.hasEyes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const idGremioDetectado = clasificarGremio(scrapData['Descripción'], gremiosDB);
|
} catch (e) { console.error(`Error leyendo ${ref}: ${e.message}`); }
|
||||||
if (idGremioDetectado) {
|
|
||||||
scrapData['guild_id'] = idGremioDetectado;
|
|
||||||
}
|
|
||||||
await saveServiceToDB(ownerId, 'homeserve', ref, scrapData);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (e) { console.error("❌ [HomeServe] Error:", e.message); }
|
|
||||||
finally { await browser.close(); }
|
|
||||||
}
|
|
||||||
|
|
||||||
async function syncAndArchive(ownerId, provider, currentWebRefs) {
|
} catch (e) {
|
||||||
const client = await pool.connect();
|
console.error("❌ [HomeServe] Error:", e.message);
|
||||||
try {
|
|
||||||
// 1. Buscamos TODOS los expedientes que están vivos (en el buzón o en el panel)
|
|
||||||
const { rows: dbServices } = await client.query(
|
|
||||||
`SELECT id, service_ref, assigned_to, raw_data FROM scraped_services
|
|
||||||
WHERE owner_id = $1 AND provider = $2 AND status IN ('pending', 'imported')`,
|
|
||||||
[ownerId, provider]
|
|
||||||
);
|
|
||||||
|
|
||||||
// 2. Filtramos cuáles han desaparecido de la web oficial
|
|
||||||
const missingServices = dbServices.filter(s => !currentWebRefs.includes(s.service_ref));
|
|
||||||
const refsToArchive = missingServices.map(s => s.service_ref);
|
|
||||||
|
|
||||||
if (refsToArchive.length > 0) {
|
|
||||||
// 3. Los archivamos (los quitamos del buzón)
|
|
||||||
await client.query(
|
|
||||||
`UPDATE scraped_services
|
|
||||||
SET status = 'archived'
|
|
||||||
WHERE owner_id = $1
|
|
||||||
AND provider = $2
|
|
||||||
AND service_ref = ANY($3)`,
|
|
||||||
[ownerId, provider, refsToArchive]
|
|
||||||
);
|
|
||||||
|
|
||||||
// ========================================================
|
|
||||||
// 🛡️ EL ESCUDO ANTI-VIAJES EN BALDE (Para la App del Técnico)
|
|
||||||
// ========================================================
|
|
||||||
// Buscamos el ID del estado "Anulado" de este propietario
|
|
||||||
const statusQ = await client.query("SELECT id FROM service_statuses WHERE owner_id = $1 AND name ILIKE '%anulado%' LIMIT 1", [ownerId]);
|
|
||||||
const idAnulado = statusQ.rowCount > 0 ? statusQ.rows[0].id : null;
|
|
||||||
|
|
||||||
if (idAnulado) {
|
|
||||||
for (const svc of missingServices) {
|
|
||||||
// Si el expediente estaba asignado a un técnico...
|
|
||||||
if (svc.assigned_to) {
|
|
||||||
const raw = svc.raw_data || {};
|
|
||||||
const currentStatusId = String(raw.status_operativo || "");
|
|
||||||
|
|
||||||
// ...y su estado actual no era ya "Anulado"
|
|
||||||
if (currentStatusId !== String(idAnulado)) {
|
|
||||||
|
|
||||||
// Comprobamos si el técnico ya lo había "Finalizado" (para no pisarle el trabajo hecho)
|
|
||||||
let isFinal = false;
|
|
||||||
if (currentStatusId) {
|
|
||||||
const checkStatusQ = await client.query("SELECT is_final FROM service_statuses WHERE id = $1", [currentStatusId]);
|
|
||||||
isFinal = checkStatusQ.rowCount > 0 ? checkStatusQ.rows[0].is_final : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Si NO estaba finalizado, procedemos a ANULARLO automáticamente
|
|
||||||
if (!isFinal) {
|
|
||||||
raw.status_operativo = idAnulado;
|
|
||||||
|
|
||||||
await client.query("UPDATE scraped_services SET raw_data = $1 WHERE id = $2", [JSON.stringify(raw), svc.id]);
|
|
||||||
|
|
||||||
// Dejamos huella en la trazabilidad para que sepas por qué se anuló
|
|
||||||
await client.query(
|
|
||||||
"INSERT INTO scraped_service_logs (scraped_id, user_name, action, details) VALUES ($1, $2, $3, $4)",
|
|
||||||
[svc.id, "Sistema Robot", "Cancelación Automática", "La compañía ha cancelado/retirado el expediente. Se pasa a estado Anulado para avisar al técnico."]
|
|
||||||
);
|
|
||||||
console.log(`🛡️ [ESCUDO] Expediente ${svc.service_ref} anulado automáticamente (Técnico salvado de viajar en balde).`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// ========================================================
|
|
||||||
|
|
||||||
console.log(`📦 [${provider.toUpperCase()}] Archivados ${refsToArchive.length} expedientes desaparecidos.`);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`❌ Error archivando ${provider}:`, error.message);
|
|
||||||
} finally {
|
} finally {
|
||||||
client.release();
|
await browser.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==========================================
|
||||||
|
// 💾 GUARDADO EN BASE DE DATOS
|
||||||
|
// ==========================================
|
||||||
async function saveServiceToDB(ownerId, provider, ref, data) {
|
async function saveServiceToDB(ownerId, provider, ref, data) {
|
||||||
console.log(`💾 Guardando/Actualizando ${provider.toUpperCase()} ${ref}...`);
|
// Verificar si ya existe en servicios reales (para no duplicar trabajo)
|
||||||
|
const exists = await pool.query("SELECT id FROM services WHERE company_ref = $1 AND owner_id = $2", [ref, ownerId]);
|
||||||
// Aquí está la solución al problema del tilde ("sí" vs "si") que te comenté antes
|
if (exists.rowCount > 0) return;
|
||||||
const isUrgent = (data['Urgente'] && (data['Urgente'].toLowerCase().trim() === 'sí' || data['Urgente'].toLowerCase().trim() === 'si')) ? true : false;
|
|
||||||
|
// Verificar si ya existe en buzón, si existe ACTUALIZAMOS los datos (por si hemos mejorado el scraping)
|
||||||
|
console.log(`💾 Guardando/Actualizando: ${ref} (${data.address})`);
|
||||||
|
|
||||||
await pool.query(`
|
await pool.query(`
|
||||||
INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, status, is_urgent)
|
INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, status)
|
||||||
VALUES ($1, $2, $3, $4, 'pending', $5)
|
VALUES ($1, $2, $3, $4, 'pending')
|
||||||
ON CONFLICT (owner_id, provider, service_ref)
|
ON CONFLICT (owner_id, provider, service_ref)
|
||||||
DO UPDATE SET
|
DO UPDATE SET raw_data = EXCLUDED.raw_data, created_at = NOW()
|
||||||
-- 🛡️ EL ESCUDO: Fusiona todo, pero si ya había un gremio guardado, lo blinda para que el robot no lo pise
|
`, [ownerId, provider, ref, JSON.stringify(data)]);
|
||||||
raw_data = (scraped_services.raw_data || EXCLUDED.raw_data) ||
|
|
||||||
jsonb_build_object('guild_id', COALESCE(scraped_services.raw_data->'guild_id', EXCLUDED.raw_data->'guild_id')),
|
|
||||||
|
|
||||||
is_urgent = EXCLUDED.is_urgent,
|
|
||||||
status = CASE
|
|
||||||
WHEN scraped_services.status = 'archived' THEN 'archived'
|
|
||||||
WHEN scraped_services.status = 'imported' THEN 'imported'
|
|
||||||
ELSE 'pending'
|
|
||||||
END
|
|
||||||
`, [ownerId, provider, ref, JSON.stringify(data), isUrgent]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
Reference in New Issue
Block a user