const express = require('express'); const { chromium } = require('playwright'); const admin = require('firebase-admin'); const cors = require('cors'); // --- 1. CONFIGURACIÓN FIREBASE --- 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."); } else { console.warn("⚠️ SIN FIREBASE: Modo simulación."); } } catch (e) { console.error("❌ Error Firebase:", e.message); } const db = admin.apps.length ? admin.firestore() : null; const APPOINTMENTS_COL = "appointments"; const app = express(); // --- 2. CORS PERMISIVO (Para evitar bloqueos) --- app.use(cors({ origin: '*' })); app.use(express.json()); // --- 3. ENDPOINT PRINCIPAL --- app.post('/api/robot-cobros', async (req, res) => { console.log("🔔 Orden recibida: Rescatar cobros."); res.json({ success: true, message: "Robot iniciado. Procesando liquidaciones..." }); // Ejecutar en segundo plano runLiquidationRobot().catch(err => console.error("❌ Error robot:", err)); }); app.get('/', (req, res) => res.send('🤖 Robot Liquidaciones ONLINE (Usa POST para activar)')); // --- 4. LÓGICA DE NAVEGACIÓN (Liquidaciones) --- async function runLiquidationRobot() { let browser = null; try { console.log("🚀 Lanzando navegador..."); // 1. Obtener 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 || !pass) throw new Error("No hay credenciales en Firebase"); browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] }); const page = await browser.newPage(); // 2. Login console.log("🌍 Entrando en HomeServe (PROF_PASS)..."); 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)) { console.log("🔑 Logueándose..."); 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("📂 Navegando a CONSULTALIQ_WEB..."); await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=CONSULTALIQ_WEB'); await page.waitForTimeout(2000); // 4. Buscar primera fecha (enlace) console.log("🔍 Buscando última liquidación..."); // Buscamos el primer enlace dentro de la tabla principal const liquidacionClicked = await page.evaluate(() => { const link = document.querySelector('table a'); // Clic en el primer enlace que encuentre en una tabla if (link) { link.click(); return true; } return false; }); if (!liquidacionClicked) throw new Error("No se encontraron liquidaciones."); await page.waitForTimeout(3000); // 5. Buscar botón "Desglose" console.log("📄 Buscando desglose..."); const desgloseClicked = await page.evaluate(() => { // Buscar input tipo button o enlace que diga "Desglose" 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) console.warn("⚠️ No vi botón 'Desglose', intentando leer tabla actual..."); await page.waitForTimeout(3000); // 6. Leer Tabla de Cobros console.log("💰 Leyendo datos..."); const cobros = await page.evaluate(() => { const filas = Array.from(document.querySelectorAll('tr')); const datos = []; filas.forEach(tr => { const text = tr.innerText; // Buscamos patrones de expediente (ej: 15xxxxxx) y dinero // Esta es una heurística básica, ajustar según HTML real const tds = tr.querySelectorAll('td'); let exp = ""; let money = ""; tds.forEach(td => { const t = td.innerText.trim(); if (/^\d{6,}$/.test(t)) exp = t; // Numeros largos = expediente if (t.includes(',') && (t.includes('€') || /\d/.test(t))) money = t; }); if (exp && money) datos.push({ exp, money }); }); return datos; }); console.log(`✅ Detectados ${cobros.length} posibles cobros.`); // 7. Guardar en Firebase if (db && cobros.length > 0) { const batch = db.batch(); let count = 0; const nowISO = new Date().toISOString(); for (const item of cobros) { // Limpiar importe const importe = parseFloat(item.money.replace(/[^\d,]/g, '').replace(',', '.')); if (!importe) continue; // Buscar servicio const q = await db.collection(APPOINTMENTS_COL).where("serviceNumber", "==", item.exp).get(); q.forEach(doc => { batch.update(doc.ref, { paidAmount: importe, status: 'completed', // Asumimos completado si pagan paymentDate: nowISO, lastUpdatedByRobot: nowISO }); count++; }); } await batch.commit(); console.log(`💾 Base de datos actualizada: ${count} registros.`); } } catch (e) { console.error("❌ Error en proceso:", e); } finally { if (browser) await browser.close(); } } const PORT = process.env.PORT || 3000; app.listen(PORT, () => console.log(`🚀 Server on port ${PORT}`));