226 lines
8.6 KiB
JavaScript
226 lines
8.6 KiB
JavaScript
const express = require('express');
|
|
const { chromium } = require('playwright');
|
|
const admin = require('firebase-admin');
|
|
const cors = require('cors');
|
|
|
|
// --- 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.");
|
|
}
|
|
} catch (e) { console.error("❌ Error Firebase:", e.message); }
|
|
|
|
const db = admin.apps.length ? admin.firestore() : null;
|
|
const APPOINTMENTS_COL = "appointments";
|
|
|
|
const app = express();
|
|
app.use(cors({ origin: '*' }));
|
|
app.use(express.json());
|
|
|
|
// --- ENDPOINT UNIFICADO ---
|
|
app.post('/api/robot-cobros', async (req, res) => {
|
|
const { action, urls } = req.body;
|
|
console.log(`🔔 Orden recibida: ${action.toUpperCase()}`);
|
|
|
|
try {
|
|
if (action === 'scan') {
|
|
const lista = await runScanner();
|
|
res.json({ success: true, mode: 'scan', data: lista });
|
|
}
|
|
else if (action === 'process') {
|
|
if (!urls || urls.length === 0) throw new Error("No has seleccionado ninguna fecha.");
|
|
const procesados = await runProcessor(urls);
|
|
res.json({ success: true, mode: 'process', count: procesados });
|
|
}
|
|
else {
|
|
throw new Error("Acción no válida");
|
|
}
|
|
} catch (err) {
|
|
console.error("❌ Error:", err.message);
|
|
res.status(500).json({ success: false, message: err.message });
|
|
}
|
|
});
|
|
|
|
// --- FUNCIÓN 1: ESCANEAR FECHAS (SCAN) ---
|
|
async function runScanner() {
|
|
let browser = null;
|
|
try {
|
|
const { browser: b, page } = await loginAndGoToLiquidaciones();
|
|
browser = b;
|
|
|
|
console.log("🔍 Buscando lista de liquidaciones...");
|
|
|
|
const liquidaciones = await page.evaluate(() => {
|
|
const links = Array.from(document.querySelectorAll('a'));
|
|
const results = [];
|
|
links.forEach(l => {
|
|
const txt = l.innerText.trim();
|
|
// Detectar formato fecha (DD/MM/YYYY)
|
|
if (/\d{2}\/\d{2}\/\d{4}/.test(txt)) {
|
|
results.push({ fecha: txt, url: l.href });
|
|
}
|
|
});
|
|
return results;
|
|
});
|
|
|
|
console.log(`✅ Encontradas ${liquidaciones.length} fechas.`);
|
|
return liquidaciones;
|
|
|
|
} catch (e) { throw e; } finally { if(browser) await browser.close(); }
|
|
}
|
|
|
|
// --- FUNCIÓN 2: PROCESAR SELECCIONADAS (PROCESS) ---
|
|
async function runProcessor(urlsAProcesar) {
|
|
let browser = null;
|
|
let totalActualizados = 0;
|
|
|
|
try {
|
|
const { browser: b, page } = await loginAndGoToLiquidaciones();
|
|
browser = b;
|
|
|
|
for (const targetUrl of urlsAProcesar) {
|
|
console.log(`➡️ Procesando URL: ${targetUrl}`);
|
|
|
|
// 1. Ir a la fecha
|
|
await page.goto(targetUrl, { timeout: 60000 });
|
|
await page.waitForTimeout(1500);
|
|
|
|
// 2. BUSCAR BOTÓN ESPECÍFICO "DESGLOSE SERVICIOS"
|
|
// Buscamos cualquier botón/input que contenga la palabra "SERVICIOS" (ignorando mayusc/minusc)
|
|
console.log(" 🔘 Buscando botón 'Desglose Servicios'...");
|
|
|
|
const botonPulsado = await page.evaluate(() => {
|
|
// Buscamos en inputs (tipo submit/button), botones y enlaces
|
|
const elementos = Array.from(document.querySelectorAll('input[type="button"], input[type="submit"], button, a'));
|
|
|
|
// Filtramos el que tenga "SERVICIOS" y "DESGLOSE" en su texto o valor
|
|
const target = elementos.find(el => {
|
|
const texto = (el.value || el.innerText || "").toUpperCase();
|
|
return texto.includes("SERVICIO") && texto.includes("DESGLOSE");
|
|
});
|
|
|
|
// Si no encontramos uno tan específico, probamos solo con "SERVICIOS" dentro de la tabla de botones
|
|
const targetSecundario = elements.find(el => (el.value || el.innerText || "").toUpperCase().includes("SERVICIOS"));
|
|
|
|
const finalTarget = target || targetSecundario;
|
|
|
|
if (finalTarget) {
|
|
finalTarget.click();
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
|
|
if (botonPulsado) {
|
|
console.log(" ✅ Botón pulsado. Esperando tabla...");
|
|
await page.waitForTimeout(3000); // Esperar a que cargue la tabla de datos
|
|
} else {
|
|
console.warn(" ⚠️ No encontré el botón de Servicios. Intentando leer por si ya estamos dentro.");
|
|
}
|
|
|
|
// 3. LEER LA TABLA DE DATOS
|
|
const datosTabla = await page.evaluate(() => {
|
|
const filas = Array.from(document.querySelectorAll('tr'));
|
|
const datos = [];
|
|
|
|
filas.forEach(tr => {
|
|
const tds = tr.querySelectorAll('td');
|
|
// Estructura saldo.html:
|
|
// Col 0: Servicio | Col 5: Saldo
|
|
if (tds.length >= 6) {
|
|
const servicio = tds[0].innerText.trim();
|
|
const saldoRaw = tds[5].innerText.trim();
|
|
|
|
// Validar que sea un número de servicio real
|
|
if (/^\d{5,}$/.test(servicio)) {
|
|
datos.push({ servicio, saldoRaw });
|
|
}
|
|
}
|
|
});
|
|
return datos;
|
|
});
|
|
|
|
console.log(` 💰 Leídos ${datosTabla.length} servicios en esta liquidación.`);
|
|
|
|
// 4. GUARDAR EN FIREBASE
|
|
if (db && datosTabla.length > 0) {
|
|
const batch = db.batch();
|
|
let batchCount = 0;
|
|
const nowISO = new Date().toISOString();
|
|
|
|
for (const item of datosTabla) {
|
|
// Limpieza "-33.48" -> 33.48
|
|
let cleanSaldo = item.saldoRaw.replace(/[^\d.-]/g, '');
|
|
let importe = parseFloat(cleanSaldo);
|
|
|
|
if (isNaN(importe)) continue;
|
|
|
|
// Convertimos a positivo para guardar en 'paidAmount'
|
|
importe = Math.abs(importe);
|
|
|
|
const q = await db.collection(APPOINTMENTS_COL).where("serviceNumber", "==", item.servicio).get();
|
|
|
|
q.forEach(doc => {
|
|
batch.update(doc.ref, {
|
|
paidAmount: importe,
|
|
status: 'completed',
|
|
paymentDate: nowISO,
|
|
homeservePaymentStatus: 'paid_saldo',
|
|
lastUpdatedByRobot: nowISO
|
|
});
|
|
batchCount++;
|
|
});
|
|
}
|
|
|
|
if (batchCount > 0) {
|
|
await batch.commit();
|
|
totalActualizados += batchCount;
|
|
console.log(` 💾 Guardados ${batchCount} registros.`);
|
|
}
|
|
}
|
|
}
|
|
|
|
return totalActualizados;
|
|
|
|
} catch (e) { throw e; } finally { if(browser) await browser.close(); }
|
|
}
|
|
|
|
// --- LOGIN ---
|
|
async function loginAndGoToLiquidaciones() {
|
|
let user = "SIN_USER", pass = "SIN_PASS";
|
|
if (db) {
|
|
const doc = await db.collection("providerCredentials").doc("homeserve").get();
|
|
if (doc.exists) { user = doc.data().user; pass = doc.data().pass; }
|
|
}
|
|
|
|
const browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] });
|
|
const page = await browser.newPage();
|
|
|
|
console.log("🌍 Login...");
|
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS', { timeout: 60000 });
|
|
|
|
if (await page.isVisible('input[name="CODIGO"]')) {
|
|
await page.fill('input[name="CODIGO"]', user);
|
|
await page.fill('input[type="password"]', pass);
|
|
await page.keyboard.press('Enter');
|
|
await page.waitForTimeout(4000);
|
|
}
|
|
|
|
console.log("📂 Sección Liquidaciones...");
|
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=CONSULTALIQ_WEB');
|
|
await page.waitForTimeout(2000);
|
|
|
|
return { browser, page };
|
|
}
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
app.listen(PORT, () => console.log(`🚀 Server on port ${PORT}`)); |