160 lines
5.8 KiB
JavaScript
160 lines
5.8 KiB
JavaScript
const express = require('express');
|
|
const { chromium } = require('playwright');
|
|
const admin = require('firebase-admin');
|
|
const cors = require('cors');
|
|
|
|
// --- 1. CONFIGURACIÓN (Solo para leer credenciales) ---
|
|
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'),
|
|
}),
|
|
});
|
|
}
|
|
console.log("✅ Firebase inicializado (Solo para leer usuario/pass).");
|
|
}
|
|
} catch (e) { console.error("❌ Error Firebase:", e.message); }
|
|
|
|
const db = admin.apps.length ? admin.firestore() : null;
|
|
|
|
const app = express();
|
|
app.use(cors({ origin: '*' }));
|
|
app.use(express.json());
|
|
|
|
// --- 2. ENDPOINT (AHORA ESPERA AL ROBOT) ---
|
|
app.post('/api/robot-cobros', async (req, res) => {
|
|
console.log("🔔 Petición recibida: Modo Simulación.");
|
|
|
|
try {
|
|
// Esperamos a que el robot termine y nos de los datos
|
|
const resultados = await runLiquidationScanner();
|
|
|
|
// Se los enviamos al navegador
|
|
res.json({
|
|
success: true,
|
|
message: "Escaneo completado",
|
|
data: resultados
|
|
});
|
|
} catch (err) {
|
|
console.error("❌ Error en robot:", err);
|
|
res.status(500).json({ success: false, message: err.message });
|
|
}
|
|
});
|
|
|
|
app.get('/', (req, res) => res.send('🤖 Robot Scanner ONLINE (Modo Lectura)'));
|
|
|
|
// --- 3. LÓGICA DE ESCANEO (SIN GUARDAR) ---
|
|
async function runLiquidationScanner() {
|
|
let browser = null;
|
|
try {
|
|
console.log("🚀 Lanzando navegador...");
|
|
|
|
// 1. Credenciales
|
|
let user = "TU_USUARIO";
|
|
let pass = "TU_PASS";
|
|
|
|
if (db) {
|
|
const doc = await db.collection("providerCredentials").doc("homeserve").get();
|
|
if (doc.exists) {
|
|
user = doc.data().user;
|
|
pass = doc.data().pass;
|
|
}
|
|
}
|
|
|
|
browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] });
|
|
const page = await browser.newPage();
|
|
|
|
// 2. Login
|
|
console.log("🌍 Login HomeServe...");
|
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS', { timeout: 60000 });
|
|
|
|
const selUser = 'input[name="CODIGO"]';
|
|
if (await page.isVisible(selUser)) {
|
|
await page.fill(selUser, user);
|
|
await page.fill('input[type="password"]', pass);
|
|
await page.keyboard.press('Enter');
|
|
await page.waitForTimeout(4000);
|
|
}
|
|
|
|
// 3. Ir a Liquidaciones
|
|
console.log("📂 Sección Liquidaciones...");
|
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=CONSULTALIQ_WEB');
|
|
await page.waitForTimeout(2000);
|
|
|
|
// 4. Buscar enlace
|
|
console.log("🔍 Buscando última liquidación...");
|
|
const liquidacionClicked = await page.evaluate(() => {
|
|
const links = Array.from(document.querySelectorAll('a'));
|
|
// Busca enlace dentro de una celda que tenga texto largo (la fecha)
|
|
const target = links.find(l => l.closest('td') && l.innerText.length > 5);
|
|
if (target) { target.click(); return true; }
|
|
return false;
|
|
});
|
|
|
|
if (!liquidacionClicked) console.log("⚠️ No clické fecha (quizá ya estamos dentro).");
|
|
await page.waitForTimeout(3000);
|
|
|
|
// 5. Botón Desglose
|
|
const desgloseClicked = await page.evaluate(() => {
|
|
const inputs = Array.from(document.querySelectorAll('input[type="button"], a, button'));
|
|
const target = inputs.find(el => (el.value || el.innerText || "").toLowerCase().includes('desglose'));
|
|
if (target) { target.click(); return true; }
|
|
return false;
|
|
});
|
|
if(desgloseClicked) await page.waitForTimeout(3000);
|
|
|
|
// 6. LEER DATOS Y DEVOLVERLOS (NO GUARDAR)
|
|
console.log("💰 Extrayendo datos para el usuario...");
|
|
|
|
const extractedData = await page.evaluate(() => {
|
|
const filas = Array.from(document.querySelectorAll('tr'));
|
|
const lista = [];
|
|
|
|
filas.forEach(tr => {
|
|
const tds = tr.querySelectorAll('td');
|
|
// Estructura detectada en tu saldo.html:
|
|
// Col 0: Servicio, Col 1: Dirección, Col 4: Importe Autofra (Base)
|
|
if (tds.length >= 5) {
|
|
const txtServicio = tds[0].innerText.trim();
|
|
const txtDir = tds[1].innerText.trim();
|
|
const txtImporte = tds[4].innerText.trim();
|
|
|
|
if (/^\d{6,}$/.test(txtServicio)) {
|
|
lista.push({
|
|
expediente: txtServicio,
|
|
direccion: txtDir,
|
|
importeRaw: txtImporte
|
|
});
|
|
}
|
|
}
|
|
});
|
|
return lista;
|
|
});
|
|
|
|
// Procesar importes (Limpieza de puntos decimales)
|
|
const finalData = extractedData.map(item => {
|
|
// "27.67" -> 27.67 (Float)
|
|
const clean = item.importeRaw.replace(/[^\d.-]/g, '');
|
|
return {
|
|
...item,
|
|
importeLimpio: parseFloat(clean) || 0
|
|
};
|
|
});
|
|
|
|
console.log(`✅ Devolviendo ${finalData.length} registros al frontend.`);
|
|
return finalData;
|
|
|
|
} catch (e) {
|
|
console.error("❌ Error:", e);
|
|
throw e;
|
|
} finally {
|
|
if (browser) await browser.close();
|
|
}
|
|
}
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
app.listen(PORT, () => console.log(`🚀 Server on port ${PORT}`)); |