202 lines
7.1 KiB
JavaScript
202 lines
7.1 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({ limit: '10mb' }));
|
|
|
|
// --- ENDPOINT PRINCIPAL ---
|
|
app.post('/api/robot-cobros', async (req, res) => {
|
|
const { action, url, provider, dataToSave } = req.body;
|
|
console.log(`🔔 Orden: ${action.toUpperCase()} (${provider || 'HS'})`);
|
|
|
|
try {
|
|
if (action === 'scan') {
|
|
const lista = await runScanner();
|
|
res.json({ success: true, data: lista });
|
|
}
|
|
else if (action === 'analyze') {
|
|
if (!url) throw new Error("Falta URL");
|
|
const analisis = await runAnalyzer(url);
|
|
res.json({ success: true, ...analisis });
|
|
}
|
|
else if (action === 'save_data') {
|
|
if (!dataToSave || !Array.isArray(dataToSave)) throw new Error("No hay datos");
|
|
const count = await runSaver(dataToSave);
|
|
res.json({ success: true, count });
|
|
}
|
|
else {
|
|
throw new Error("Acción desconocida");
|
|
}
|
|
} catch (err) {
|
|
console.error("❌ Error:", err.message);
|
|
res.status(500).json({ success: false, message: err.message });
|
|
}
|
|
});
|
|
|
|
// --- 1. ESCÁNER ---
|
|
async function runScanner() {
|
|
let browser = null;
|
|
try {
|
|
const { browser: b, page } = await loginAndGoToLiquidaciones();
|
|
browser = b;
|
|
|
|
const liquidaciones = await page.evaluate(() => {
|
|
const links = Array.from(document.querySelectorAll('a'));
|
|
const results = [];
|
|
links.forEach(l => {
|
|
const txt = l.innerText.trim();
|
|
if (/\d{2}\/\d{2}\/\d{4}/.test(txt)) {
|
|
results.push({ fecha: txt, url: l.href });
|
|
}
|
|
});
|
|
return results;
|
|
});
|
|
return liquidaciones;
|
|
} catch (e) { throw e; } finally { if(browser) await browser.close(); }
|
|
}
|
|
|
|
// --- 2. ANALIZADOR ---
|
|
async function runAnalyzer(targetUrl) {
|
|
let browser = null;
|
|
try {
|
|
const { browser: b, page } = await loginAndGoToLiquidaciones();
|
|
browser = b;
|
|
|
|
console.log(`➡️ Analizando: ${targetUrl}`);
|
|
await page.goto(targetUrl, { timeout: 60000 });
|
|
await page.waitForTimeout(1500);
|
|
|
|
const botonPulsado = await page.evaluate(() => {
|
|
const elementos = Array.from(document.querySelectorAll('input, button, a'));
|
|
const target = elementos.find(el => {
|
|
const txt = (el.value || el.innerText || "").toUpperCase();
|
|
return txt.includes("SERVICIO") && txt.includes("DESGLOSE");
|
|
});
|
|
if (target) { target.click(); return true; }
|
|
return false;
|
|
});
|
|
|
|
if (botonPulsado) await page.waitForTimeout(3000);
|
|
|
|
const datosRaw = await page.evaluate(() => {
|
|
const filas = Array.from(document.querySelectorAll('tr'));
|
|
const datos = [];
|
|
filas.forEach(tr => {
|
|
const tds = tr.querySelectorAll('td');
|
|
if (tds.length >= 6) {
|
|
const servicio = tds[0].innerText.trim();
|
|
const direccion = tds[1].innerText.trim();
|
|
const saldoRaw = tds[5].innerText.trim();
|
|
if (/^\d{5,}$/.test(servicio)) {
|
|
datos.push({ servicio, direccion, saldoRaw });
|
|
}
|
|
}
|
|
});
|
|
return datos;
|
|
});
|
|
|
|
const encontrados = [];
|
|
const noEncontrados = [];
|
|
|
|
if (db) {
|
|
for (const item of datosRaw) {
|
|
let cleanSaldo = item.saldoRaw.replace(/[^\d.-]/g, '');
|
|
let importe = Math.abs(parseFloat(cleanSaldo) || 0);
|
|
|
|
const q = await db.collection(APPOINTMENTS_COL).where("serviceNumber", "==", item.servicio).get();
|
|
|
|
if (!q.empty) {
|
|
q.forEach(doc => {
|
|
encontrados.push({
|
|
servicio: item.servicio,
|
|
direccion: item.direccion,
|
|
importe: importe,
|
|
docId: doc.id
|
|
});
|
|
});
|
|
} else {
|
|
noEncontrados.push({
|
|
servicio: item.servicio,
|
|
direccion: item.direccion,
|
|
importe: importe
|
|
});
|
|
}
|
|
}
|
|
}
|
|
return { encontrados, noEncontrados };
|
|
|
|
} catch (e) { throw e; } finally { if(browser) await browser.close(); }
|
|
}
|
|
|
|
// --- 3. GUARDADOR (Con paymentState) ---
|
|
async function runSaver(items) {
|
|
if (!db) return 0;
|
|
const batch = db.batch();
|
|
const nowISO = new Date().toISOString();
|
|
let count = 0;
|
|
|
|
items.forEach(item => {
|
|
const ref = db.collection(APPOINTMENTS_COL).doc(item.docId);
|
|
batch.update(ref, {
|
|
paidAmount: item.importe,
|
|
paymentState: "Pagado", // <--- NUEVO CAMPO SOLICITADO
|
|
status: 'completed',
|
|
paymentDate: nowISO,
|
|
homeservePaymentStatus: 'paid_saldo',
|
|
lastUpdatedByRobot: nowISO
|
|
});
|
|
count++;
|
|
});
|
|
|
|
await batch.commit();
|
|
console.log(`💾 Guardados ${count} documentos.`);
|
|
return count;
|
|
}
|
|
|
|
// --- LOGIN ---
|
|
async function loginAndGoToLiquidaciones() {
|
|
let user = "", 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();
|
|
|
|
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);
|
|
}
|
|
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}`)); |