165 lines
6.4 KiB
JavaScript
165 lines
6.4 KiB
JavaScript
const express = require('express');
|
|
const { chromium } = require('playwright');
|
|
const admin = require('firebase-admin');
|
|
const cors = require('cors');
|
|
|
|
// --- CONFIGURACIÓN ---
|
|
try {
|
|
if (process.env.FIREBASE_PRIVATE_KEY) {
|
|
if (!admin.apps.length) {
|
|
admin.initializeApp({
|
|
credential: admin.credential.cert({
|
|
projectId: process.env.FIREBASE_PROJECT_ID,
|
|
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
|
|
privateKey: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
|
|
}),
|
|
});
|
|
}
|
|
}
|
|
} catch (e) { console.error("Firebase Error:", e.message); }
|
|
|
|
const db = admin.apps.length ? admin.firestore() : null;
|
|
const app = express();
|
|
app.use(cors({ origin: '*' }));
|
|
app.use(express.json());
|
|
|
|
// --- ENDPOINT ---
|
|
app.post('/api/robot-cobros', async (req, res) => {
|
|
console.log("🔔 DIAGNÓSTICO: Iniciando...");
|
|
try {
|
|
const reporte = await runDiagnostic();
|
|
res.json({ success: true, message: "Diagnóstico finalizado", data: reporte });
|
|
} catch (err) {
|
|
console.error("❌ Error Fatal:", err);
|
|
res.status(500).json({ success: false, message: err.message });
|
|
}
|
|
});
|
|
|
|
app.get('/', (req, res) => res.send('🤖 Robot Sherlock ONLINE'));
|
|
|
|
// --- LÓGICA DE DETECTIVE ---
|
|
async function runDiagnostic() {
|
|
let browser = null;
|
|
const logs = []; // Aquí guardaremos las pistas
|
|
|
|
function apuntar(msg) {
|
|
console.log(msg);
|
|
logs.push({ expediente: "INFO", direccion: msg, importeLimpio: 0 });
|
|
}
|
|
|
|
try {
|
|
// 1. CREDENCIALES
|
|
let user = ""; let pass = "";
|
|
if (db) {
|
|
const doc = await db.collection("providerCredentials").doc("homeserve").get();
|
|
if (doc.exists) { user = doc.data().user; pass = doc.data().pass; }
|
|
}
|
|
if (!user) {
|
|
apuntar("❌ ERROR: No tengo usuario/pass en Firebase.");
|
|
return logs;
|
|
}
|
|
|
|
apuntar("🚀 Lanzando navegador...");
|
|
browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] });
|
|
const page = await browser.newPage();
|
|
|
|
// 2. LOGIN
|
|
apuntar("🌍 Entrando al Login...");
|
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS', { timeout: 60000 });
|
|
|
|
const enLogin = await page.isVisible('input[name="CODIGO"]');
|
|
if (enLogin) {
|
|
apuntar("🔑 Formulario detectado. Escribiendo...");
|
|
await page.fill('input[name="CODIGO"]', user);
|
|
await page.fill('input[type="password"]', pass);
|
|
await page.keyboard.press('Enter');
|
|
await page.waitForTimeout(4000);
|
|
} else {
|
|
apuntar("⚠️ No vi el formulario de login. ¿Ya estaba dentro?");
|
|
}
|
|
|
|
// 3. NAVEGAR A LIQUIDACIONES
|
|
apuntar("📂 Yendo a Consultar Liquidaciones...");
|
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=CONSULTALIQ_WEB');
|
|
await page.waitForTimeout(2000);
|
|
|
|
apuntar(`📍 URL actual: ${page.url()}`);
|
|
|
|
// 4. INTENTAR CLICKAR FECHA
|
|
// Buscamos enlaces que parezcan una fecha YYYYMMDD o similar
|
|
const clickado = await page.evaluate(() => {
|
|
const links = Array.from(document.querySelectorAll('a'));
|
|
// Busca el primer enlace que NO sea de menú y tenga numeros
|
|
const target = links.find(l => {
|
|
const txt = l.innerText.trim();
|
|
// Heurística: Si tiene más de 4 números seguidos, probablemente es una fecha/expediente
|
|
return /\d{4}/.test(txt) && !txt.includes('FACTURACION');
|
|
});
|
|
if (target) {
|
|
const texto = target.innerText;
|
|
target.click();
|
|
return texto;
|
|
}
|
|
return null;
|
|
});
|
|
|
|
if (clickado) {
|
|
apuntar(`🖱️ Click realizado en enlace: "${clickado}"`);
|
|
await page.waitForTimeout(3000);
|
|
} else {
|
|
apuntar("⚠️ No encontré ningún enlace de fecha para hacer clic. ¿Está la lista vacía?");
|
|
}
|
|
|
|
// 5. BUSCAR DESGLOSE
|
|
const hayBotonDesglose = await page.isVisible('input[value*="Desglose" i]');
|
|
if (hayBotonDesglose) {
|
|
apuntar("🔘 Botón 'Desglose' detectado. Pulsando...");
|
|
await page.click('input[value*="Desglose" i]');
|
|
await page.waitForTimeout(3000);
|
|
}
|
|
|
|
// 6. ANÁLISIS VISUAL DE LA TABLA (EL MOMENTO DE LA VERDAD)
|
|
apuntar("👀 Analizando estructura de la página...");
|
|
|
|
const diagnosticoPagina = await page.evaluate(() => {
|
|
const resultado = [];
|
|
|
|
// A) Buscar la tabla amarilla específica de saldo.html
|
|
const tablaAmarilla = document.querySelector('table[bgcolor="#F8E8A6"]');
|
|
|
|
if (tablaAmarilla) {
|
|
resultado.push({ expediente: "EXITO", direccion: "✅ TABLA AMARILLA ENCONTRADA", importeLimpio: 1 });
|
|
|
|
// Leemos las 3 primeras filas para ver qué hay dentro
|
|
const filas = Array.from(tablaAmarilla.querySelectorAll('tr'));
|
|
resultado.push({ expediente: "INFO", direccion: `La tabla tiene ${filas.length} filas.`, importeLimpio: 0 });
|
|
|
|
filas.slice(0, 5).forEach((tr, i) => {
|
|
resultado.push({
|
|
expediente: `FILA ${i}`,
|
|
direccion: tr.innerText.substring(0, 50) + "...", // Primeros 50 caracteres
|
|
importeLimpio: 0
|
|
});
|
|
});
|
|
} else {
|
|
// B) Si no hay tabla amarilla, ¿qué hay?
|
|
resultado.push({ expediente: "ERROR", direccion: "❌ NO VEO LA TABLA AMARILLA", importeLimpio: 0 });
|
|
const bodyText = document.body.innerText.substring(0, 100).replace(/\n/g, ' ');
|
|
resultado.push({ expediente: "DUMP", direccion: `Texto visible: ${bodyText}`, importeLimpio: 0 });
|
|
}
|
|
|
|
return resultado;
|
|
});
|
|
|
|
return [...logs, ...diagnosticoPagina];
|
|
|
|
} catch (e) {
|
|
apuntar("❌ CRASH: " + e.message);
|
|
return logs;
|
|
} finally {
|
|
if (browser) await browser.close();
|
|
}
|
|
}
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
app.listen(PORT, () => console.log(`🚀 Server on port ${PORT}`)); |