From 94feed217c63ce6e519aa14e8062bd319a273fbd Mon Sep 17 00:00:00 2001 From: marsalva Date: Wed, 31 Dec 2025 12:39:28 +0000 Subject: [PATCH] Actualizar robot_cobros.js --- robot_cobros.js | 247 ++++++++++++++++++------------------------------ 1 file changed, 93 insertions(+), 154 deletions(-) diff --git a/robot_cobros.js b/robot_cobros.js index ee59925..cb5a487 100644 --- a/robot_cobros.js +++ b/robot_cobros.js @@ -27,33 +27,42 @@ const APPOINTMENTS_COL = "appointments"; const URLS = { MULTI_LOGIN: "https://web.multiasistencia.com/w3multi/acceso.php", MULTI_LIST: "https://web.multiasistencia.com/w3multi/cerrados.php", - HS_LOGIN: "https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS", - HS_LIQ: "https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=CONSULTALIQ_WEB" }; const app = express(); app.use(cors({ origin: '*' })); app.use(express.json({ limit: '50mb' })); +/** + * Endpoint streaming NDJSON: + * - provider: "MULTI" + * - action: "scan" | "save_data" + * + * Respuestas por línea: + * {type:"LOG", payload:"..."} + * {type:"PROGRESS", payload:{current,total}} + * {type:"ITEM_FOUND", payload:{servicio,direccion,cliente,importe,enBD,docId}} + * {type:"FINISH_SCAN", payload:{}} + * {type:"DONE", payload:{count}} + * {type:"ERROR", payload:"mensaje"} + */ app.post('/api/robot-cobros', async (req, res) => { res.setHeader('Content-Type', 'text/plain; charset=utf-8'); res.setHeader('Transfer-Encoding', 'chunked'); - const send = (type, payload) => res.write(JSON.stringify({ type, payload }) + "\n"); - const { action, url, provider, dataToSave, month, year } = req.body; + const send = (type, payload) => res.write(JSON.stringify({ type, payload }) + "\n"); + const { action, provider, dataToSave, month, year } = req.body; try { - if (provider === 'MULTI') { - if (action === 'scan') await runMultiStream(month, year, send); - else if (action === 'save_data') { - const count = await runSaver(dataToSave, 'MULTI'); - send('DONE', { count }); - } + if (provider !== 'MULTI') throw new Error("Provider inválido. Usa provider='MULTI'."); + + if (action === 'scan') { + await runMultiStream(month, year, send); + } else if (action === 'save_data') { + const count = await runSaver(dataToSave, send); + send('DONE', { count }); } else { - // HOMESERVE - if (action === 'scan') { const l = await runHSScanner(); send('HS_DATES', l); } - else if (action === 'analyze') { const a = await runHSAnalyzer(url); send('HS_DATA', a); } - else if (action === 'save_data') { const c = await runSaver(dataToSave, 'HS'); send('DONE', { count: c }); } + throw new Error("Acción inválida. Usa action='scan' o action='save_data'."); } } catch (err) { send('ERROR', err.message); @@ -69,16 +78,16 @@ app.post('/api/robot-cobros', async (req, res) => { function parseEuro(raw) { if (!raw) return 0; // "53,66 €" -> 53.66 - const clean = raw.replace(/[^\d,]/g, '').replace(',', '.'); + const clean = String(raw).replace(/[^\d,]/g, '').replace(',', '.'); const val = parseFloat(clean); return Number.isFinite(val) ? val : 0; } async function getTotalReparacion(page) { - // Selector deep: atraviesa Shadow DOM + // Selector "deep" (Shadow DOM) con Playwright const deepSel = 'css=multiasistencia-valoraciones >>> td[data-label^="TOTAL REPARACION"]'; - // 1) prueba en frames por si lo meten dentro + // 1) prueba en frames for (const frame of page.frames()) { try { const loc = frame.locator(deepSel).first(); @@ -89,10 +98,9 @@ async function getTotalReparacion(page) { } catch {} } - // 2) fallback: búsqueda manual por shadowRoots abiertos (por si cambian el componente) + // 2) fallback: traversal shadowRoots abiertos const raw2 = await page.evaluate(() => { const seen = new Set(); - function findIn(root) { if (!root || seen.has(root)) return null; seen.add(root); @@ -109,7 +117,6 @@ async function getTotalReparacion(page) { } return null; } - return findIn(document); }); @@ -117,17 +124,22 @@ async function getTotalReparacion(page) { } // ================================================================== -// 🤖 LÓGICA MULTIASISTENCIA (sin reintentos del importe) +// MULTI SCAN // ================================================================== async function runMultiStream(mes, anio, send) { let browser = null; - const valD1 = `${mes}_${anio}`; - const valD2 = `${anio}`; + const mm = String(mes || '').padStart(2, '0'); + const yyyy = String(anio || ''); + if (!/^\d{2}$/.test(mm) || !/^\d{4}$/.test(yyyy)) throw new Error("month/year inválidos. Ej: month='01', year='2025'."); + + const valD1 = `${mm}_${yyyy}`; + const valD2 = `${yyyy}`; try { send('LOG', `🚀 (MULTI) Iniciando scan: ${valD1}`); - const { browser: b, page } = await loginMulti(); + + const { browser: b, page } = await loginMulti(send); browser = b; send('LOG', "📂 Yendo a Cerrados..."); @@ -139,9 +151,11 @@ async function runMultiStream(mes, anio, send) { await page.selectOption('select[name="D2"]', valD2); await page.click('input[name="continuar"]'); await page.waitForTimeout(2500); + } else { + send('LOG', "⚠️ No veo el filtro D1/D2 (¿cambió la página?)."); } - // --- FASE 1: RECOLECTAR IDs --- + // --- FASE 1: IDS --- let idsServicios = []; let tieneSiguiente = true; let pagActual = 1; @@ -152,7 +166,7 @@ async function runMultiStream(mes, anio, send) { const celdas = Array.from(document.querySelectorAll('td.tdet')); const lista = []; celdas.forEach(td => { - const txt = td.innerText.trim(); + const txt = (td.innerText || '').trim(); if (/^\d{8}$/.test(txt)) lista.push(txt); }); return lista; @@ -162,16 +176,24 @@ async function runMultiStream(mes, anio, send) { send('LOG', ` -> Encontrados ${nuevosIds.length} servicios.`); const haySiguiente = await page.$('a:has-text("Página siguiente")'); - if (haySiguiente) { await haySiguiente.click(); await page.waitForTimeout(3000); pagActual++; } - else { tieneSiguiente = false; } + if (haySiguiente) { + await haySiguiente.click(); + await page.waitForTimeout(3000); + pagActual++; + } else { + tieneSiguiente = false; + } } idsServicios = [...new Set(idsServicios)]; send('LOG', `🔍 Total a revisar: ${idsServicios.length}`); - if (idsServicios.length === 0) return { encontrados: [], noEncontrados: [] }; + if (idsServicios.length === 0) { + send('FINISH_SCAN', {}); + return; + } - // --- FASE 2: LEER DETALLES --- + // --- FASE 2: DETALLES --- for (const [index, idServicio] of idsServicios.entries()) { send('PROGRESS', { current: index + 1, total: idsServicios.length }); @@ -186,7 +208,7 @@ async function runMultiStream(mes, anio, send) { let clienteWeb = "Desconocido"; let direccionWeb = "Sin dirección"; - // Cliente/dirección (igual que antes) + // Cliente/dirección (policy-info-block) for (const frame of page.frames()) { try { const data = await frame.evaluate(() => { @@ -206,12 +228,11 @@ async function runMultiStream(mes, anio, send) { } catch {} } - // ✅ IMPORTE (una sola lectura, sin reintentos) + // ✅ IMPORTE (una sola lectura) const importe = await getTotalReparacion(page); - if (importe === 0) send('LOG', `⚠️ Aviso: Servicio ${idServicio} en 0€ (no encontrado).`); - // --- CRUCE BD --- + // --- CRUCE Firestore --- let docId = null; let enBD = false; let clienteFinal = clienteWeb; @@ -223,6 +244,8 @@ async function runMultiStream(mes, anio, send) { const doc = q.docs[0]; docId = doc.id; enBD = true; + + // Prioridad Firestore if (doc.data().clientName) clienteFinal = doc.data().clientName; if (doc.data().address) direccionFinal = doc.data().address; } @@ -232,9 +255,9 @@ async function runMultiStream(mes, anio, send) { servicio: idServicio, direccion: direccionFinal, cliente: clienteFinal, - importe: importe, - enBD: enBD, - docId: docId + importe, + enBD, + docId }); } catch (errDetail) { @@ -243,21 +266,21 @@ async function runMultiStream(mes, anio, send) { } send('FINISH_SCAN', {}); - - } catch (e) { - throw e; } finally { - if (browser) await browser.close(); + if (browser) await browser.close().catch(()=>{}); } } -async function loginMulti() { +async function loginMulti(send) { let user = "", pass = ""; + if (db) { const doc = await db.collection("providerCredentials").doc("multiasistencia").get(); if (doc.exists) { user = doc.data().user; pass = doc.data().pass; } } - if (!user) throw new Error("Faltan credenciales"); + if (!user) throw new Error("Faltan credenciales (providerCredentials/multiasistencia)"); + + send('LOG', '🔐 Login en Multiasistencia…'); const browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] }); const page = await browser.newPage(); @@ -279,129 +302,45 @@ async function loginMulti() { } // ================================================================== -// HOMESERVE (igual) +// SAVE: marca pagado por transferencia + importe (SOLO si existe docId) // ================================================================== -async function runHSScanner() { - let browser = null; - try { - const { browser: b, page } = await loginHS(); - 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(); - } -} - -async function runHSAnalyzer(targetUrl) { - let browser = null; - try { - const { browser: b, page } = await loginHS(); - browser = b; - - await page.goto(targetUrl, { timeout: 60000, waitUntil: 'domcontentloaded' }); - await page.waitForTimeout(1500); - - const botonPulsado = await page.evaluate(() => { - const el = Array.from(document.querySelectorAll('input, button, a')) - .find(el => (el.value || el.innerText || "").toUpperCase().includes("DESGLOSE")); - if (el) { el.click(); return true; } - return false; - }); - - if (botonPulsado) await page.waitForTimeout(3000); - - const datos = await page.evaluate(() => { - const rows = Array.from(document.querySelectorAll('tr')); - return rows.map(tr => { - const tds = tr.querySelectorAll('td'); - if (tds.length >= 6 && /^\d{5,}$/.test(tds[0].innerText.trim())) { - return { servicio: tds[0].innerText.trim(), direccion: tds[1].innerText.trim(), saldoRaw: tds[5].innerText.trim() }; - } - }).filter(Boolean); - }); - - const encontrados = [], noEncontrados = []; - if (db) { - for (const item of datos) { - const importe = Math.abs(parseFloat(item.saldoRaw.replace(/[^\d.-]/g, '')) || 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, docId: doc.id })); - else noEncontrados.push({ servicio: item.servicio, direccion: item.direccion, importe }); - } - } - return { encontrados, noEncontrados }; - } catch (e) { - throw e; - } finally { - if (browser) await browser.close(); - } -} - -async function loginHS() { - 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(URLS.HS_LOGIN, { timeout: 60000, waitUntil: 'domcontentloaded' }); - - 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(URLS.HS_LIQ, { waitUntil: 'domcontentloaded' }); - await page.waitForTimeout(2000); - - return { browser, page }; -} - -async function runSaver(items, providerType) { +async function runSaver(items, send) { 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 || item.servicio); - if (item.enBD || item.docId) { - const updateData = { - paidAmount: item.importe, - paymentState: "Pagado", - status: 'completed', - paymentDate: nowISO, - lastUpdatedByRobot: nowISO - }; - if (providerType === 'MULTI') updateData.multiasistenciaPaymentStatus = 'paid_verified'; - else updateData.homeservePaymentStatus = 'paid_saldo'; + (items || []).forEach(item => { + // Ultra-seguro: si no hay docId, NO guardamos (no existe en Marsalva) + if (!item || !item.docId) return; - batch.update(ref, updateData); - count++; - } + const ref = db.collection(APPOINTMENTS_COL).doc(item.docId); + + batch.update(ref, { + paidAmount: Number(item.importe || 0), + paymentState: "Pagado", + paymentMethod: "transferencia", + paymentType: "transferencia", + status: "completed", + paymentDate: nowISO, + lastUpdatedByRobot: nowISO, + multiasistenciaPaymentStatus: "paid_transfer" + }); + + count++; }); - if (count > 0) await batch.commit(); + if (count > 0) { + await batch.commit(); + send && send('LOG', `💾 Guardados ${count} servicios (transferencia).`); + } else { + send && send('LOG', '💾 Nada que guardar (0 servicios con docId).'); + } + return count; } const PORT = process.env.PORT || 3000; -app.listen(PORT, () => console.log(`🚀 Server on port ${PORT}`)); \ No newline at end of file +app.listen(PORT, () => console.log(`🚀 MULTI robot server on port ${PORT}`)); \ No newline at end of file