Actualizar robot_cobros.js
This commit is contained in:
parent
ccd58e1940
commit
9e957a8498
160
robot_cobros.js
160
robot_cobros.js
|
|
@ -33,55 +33,35 @@ const app = express();
|
||||||
app.use(cors({ origin: '*' }));
|
app.use(cors({ origin: '*' }));
|
||||||
app.use(express.json({ limit: '50mb' }));
|
app.use(express.json({ limit: '50mb' }));
|
||||||
|
|
||||||
// --- ENDPOINT DE STREAMING ---
|
|
||||||
app.post('/api/robot-cobros', async (req, res) => {
|
app.post('/api/robot-cobros', async (req, res) => {
|
||||||
// Configurar cabeceras para STREAMING (Evita timeout 504)
|
|
||||||
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
||||||
res.setHeader('Transfer-Encoding', 'chunked');
|
res.setHeader('Transfer-Encoding', 'chunked');
|
||||||
|
const send = (type, payload) => res.write(JSON.stringify({ type, payload }) + "\n");
|
||||||
// Función auxiliar para enviar mensajes al navegador
|
|
||||||
const send = (type, payload) => {
|
|
||||||
res.write(JSON.stringify({ type, payload }) + "\n");
|
|
||||||
};
|
|
||||||
|
|
||||||
const { action, url, provider, dataToSave, month, year } = req.body;
|
const { action, url, provider, dataToSave, month, year } = req.body;
|
||||||
console.log(`🔔 Orden: ${action} [${provider}]`);
|
console.log(`🔔 Orden: ${action} [${provider}]`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (provider === 'MULTI') {
|
if (provider === 'MULTI') {
|
||||||
if (action === 'scan') {
|
if (action === 'scan') await runMultiStream(month, year, send);
|
||||||
await runMultiStream(month, year, send);
|
|
||||||
}
|
|
||||||
else if (action === 'save_data') {
|
else if (action === 'save_data') {
|
||||||
const count = await runSaver(dataToSave, 'MULTI');
|
const count = await runSaver(dataToSave, 'MULTI');
|
||||||
send('DONE', { count });
|
send('DONE', { count });
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
// HOMESERVE (Simplificado)
|
||||||
// HOMESERVE (Mantenemos modo normal por ahora, adaptado a respuesta simple)
|
if (action === 'scan') { const l = await runHSScanner(); send('HS_DATES', l); }
|
||||||
if (action === 'scan') {
|
else if (action === 'analyze') { const a = await runHSAnalyzer(url); send('HS_DATA', a); }
|
||||||
const lista = await runHSScanner();
|
else if (action === 'save_data') { const c = await runSaver(dataToSave, 'HS'); send('DONE', { count: c }); }
|
||||||
send('HS_DATES', lista);
|
|
||||||
}
|
|
||||||
else if (action === 'analyze') {
|
|
||||||
const analisis = await runHSAnalyzer(url);
|
|
||||||
send('HS_DATA', analisis);
|
|
||||||
}
|
|
||||||
else if (action === 'save_data') {
|
|
||||||
const count = await runSaver(dataToSave, 'HS');
|
|
||||||
send('DONE', { count });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("❌ CRASH:", err.message);
|
console.error("❌ CRASH:", err.message);
|
||||||
send('ERROR', err.message);
|
send('ERROR', err.message);
|
||||||
} finally {
|
} finally { res.end(); }
|
||||||
res.end(); // Cerrar conexión al terminar
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// ==================================================================
|
// ==================================================================
|
||||||
// 🤖 LÓGICA MULTIASISTENCIA (STREAMING)
|
// 🤖 LÓGICA MULTIASISTENCIA V13 (LECTURA MEJORADA)
|
||||||
// ==================================================================
|
// ==================================================================
|
||||||
|
|
||||||
async function runMultiStream(mes, anio, send) {
|
async function runMultiStream(mes, anio, send) {
|
||||||
|
|
@ -90,23 +70,21 @@ async function runMultiStream(mes, anio, send) {
|
||||||
const valD2 = `${anio}`;
|
const valD2 = `${anio}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
send('LOG', `🚀 Iniciando robot para: ${valD1}`);
|
send('LOG', `🚀 Iniciando para: ${valD1}`);
|
||||||
const { browser: b, page } = await loginMulti();
|
const { browser: b, page } = await loginMulti();
|
||||||
browser = b;
|
browser = b;
|
||||||
|
|
||||||
send('LOG', "📂 Accediendo a Servicios Cerrados...");
|
send('LOG', "📂 Yendo a Servicios Cerrados...");
|
||||||
await page.goto(URLS.MULTI_LIST, { waitUntil: 'domcontentloaded' });
|
await page.goto(URLS.MULTI_LIST, { waitUntil: 'domcontentloaded' });
|
||||||
|
|
||||||
// Filtro Fecha
|
|
||||||
if (await page.isVisible('select[name="D1"]')) {
|
if (await page.isVisible('select[name="D1"]')) {
|
||||||
send('LOG', `📅 Aplicando filtro de fecha...`);
|
|
||||||
await page.selectOption('select[name="D1"]', valD1);
|
await page.selectOption('select[name="D1"]', valD1);
|
||||||
await page.selectOption('select[name="D2"]', valD2);
|
await page.selectOption('select[name="D2"]', valD2);
|
||||||
await page.click('input[name="continuar"]');
|
await page.click('input[name="continuar"]');
|
||||||
await page.waitForTimeout(2000);
|
await page.waitForTimeout(2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recolectar IDs
|
// --- FASE 1: RECOLECTAR IDs ---
|
||||||
let idsServicios = [];
|
let idsServicios = [];
|
||||||
let tieneSiguiente = true;
|
let tieneSiguiente = true;
|
||||||
let pagActual = 1;
|
let pagActual = 1;
|
||||||
|
|
@ -116,9 +94,7 @@ async function runMultiStream(mes, anio, send) {
|
||||||
const nuevosIds = await page.evaluate(() => {
|
const nuevosIds = await page.evaluate(() => {
|
||||||
const celdas = Array.from(document.querySelectorAll('td.tdet'));
|
const celdas = Array.from(document.querySelectorAll('td.tdet'));
|
||||||
const lista = [];
|
const lista = [];
|
||||||
celdas.forEach(td => {
|
celdas.forEach(td => { if (/^\d{8}$/.test(td.innerText.trim())) lista.push(td.innerText.trim()); });
|
||||||
if (/^\d{8}$/.test(td.innerText.trim())) lista.push(td.innerText.trim());
|
|
||||||
});
|
|
||||||
return lista;
|
return lista;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -127,17 +103,14 @@ async function runMultiStream(mes, anio, send) {
|
||||||
send('LOG', ` -> Encontrados ${unicosPag.length} servicios.`);
|
send('LOG', ` -> Encontrados ${unicosPag.length} servicios.`);
|
||||||
|
|
||||||
const haySiguiente = await page.$('a:has-text("Página siguiente")');
|
const haySiguiente = await page.$('a:has-text("Página siguiente")');
|
||||||
if (haySiguiente) {
|
if (haySiguiente) { await haySiguiente.click(); await page.waitForTimeout(3000); pagActual++; }
|
||||||
await haySiguiente.click();
|
else { tieneSiguiente = false; }
|
||||||
await page.waitForTimeout(3000);
|
|
||||||
pagActual++;
|
|
||||||
} else { tieneSiguiente = false; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
idsServicios = [...new Set(idsServicios)];
|
idsServicios = [...new Set(idsServicios)];
|
||||||
send('LOG', `🔍 Total servicios a revisar: ${idsServicios.length}`);
|
send('LOG', `🔍 Total a revisar: ${idsServicios.length}`);
|
||||||
|
|
||||||
// PROCESAR UNO A UNO Y ENVIAR AL MOMENTO
|
// --- FASE 2: LEER DETALLES ---
|
||||||
for (const [index, idServicio] of idsServicios.entries()) {
|
for (const [index, idServicio] of idsServicios.entries()) {
|
||||||
send('PROGRESS', { current: index + 1, total: idsServicios.length });
|
send('PROGRESS', { current: index + 1, total: idsServicios.length });
|
||||||
|
|
||||||
|
|
@ -145,72 +118,77 @@ async function runMultiStream(mes, anio, send) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await page.goto(urlPresupuesto, { timeout: 45000, waitUntil: 'domcontentloaded' });
|
await page.goto(urlPresupuesto, { timeout: 45000, waitUntil: 'domcontentloaded' });
|
||||||
|
// Espera crítica para que Angular pinte el precio
|
||||||
// ESPERA EXPLÍCITA DEL PRECIO (Clave para que no salga 0€)
|
try { await page.waitForSelector('td[data-label*="TOTAL"]', { timeout: 4000 }); } catch(e){}
|
||||||
try {
|
|
||||||
// Esperamos hasta 5 segundos a que aparezca el elemento del precio
|
|
||||||
await page.waitForSelector('td[data-label*="TOTAL REPARACION"]', { timeout: 5000 });
|
|
||||||
} catch(e) { /* Si falla, seguimos, quizás no hay precio */ }
|
|
||||||
|
|
||||||
const info = await page.evaluate(() => {
|
const info = await page.evaluate(() => {
|
||||||
const clean = (t) => t ? t.trim() : "Desconocido";
|
const clean = (t) => t ? t.trim().replace(/\s+/g, ' ') : "";
|
||||||
let cliente = "", direccion = "", totalStr = "0";
|
let cliente = "", direccion = "", totalStr = "";
|
||||||
|
|
||||||
// Datos Cliente
|
// 1. BUSCAR DIRECCIÓN Y CLIENTE (Iterando los bloques azules)
|
||||||
|
// Buscamos contenedores que tengan título y valor
|
||||||
const bloques = Array.from(document.querySelectorAll('.policy-info-block'));
|
const bloques = Array.from(document.querySelectorAll('.policy-info-block'));
|
||||||
bloques.forEach(b => {
|
bloques.forEach(b => {
|
||||||
const t = b.querySelector('.policy-info-title')?.innerText.toUpperCase() || "";
|
const titulo = b.querySelector('.policy-info-title')?.innerText.toUpperCase() || "";
|
||||||
const v = b.querySelector('.policy-info-value')?.innerText || "";
|
const valor = b.querySelector('.policy-info-value')?.innerText || "";
|
||||||
if (t.includes("CLIENTE")) cliente = v;
|
|
||||||
if (t.includes("DIRECCIÓN")) direccion = v;
|
if (titulo.includes("CLIENTE")) cliente = valor;
|
||||||
|
if (titulo.includes("DIRECCIÓN") || titulo.includes("DIRECCION")) direccion = valor;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Fallback antiguo
|
// 2. BUSCAR PRECIO (Estrategia agresiva)
|
||||||
if (!cliente) {
|
// A) Por atributo data-label (lo más fiable según tu captura)
|
||||||
const tds = Array.from(document.querySelectorAll('td'));
|
const celdaDataLabel = document.querySelector('td[data-label*="TOTAL REPARACION"]');
|
||||||
for(let i=0; i<tds.length; i++) {
|
if (celdaDataLabel) totalStr = celdaDataLabel.innerText;
|
||||||
const txt = tds[i].innerText.toUpperCase();
|
|
||||||
if(txt.includes("NOMBRE CLIENTE") && tds[i+1]) cliente = tds[i+1].innerText;
|
// B) Si falla, buscar texto visible en cualquier celda
|
||||||
if(txt.includes("DIRECCIÓN") && tds[i+1]) direccion = tds[i+1].innerText;
|
if (!totalStr) {
|
||||||
}
|
const celdas = Array.from(document.querySelectorAll('td, th, div'));
|
||||||
|
const celdaTexto = celdas.find(el => el.innerText && el.innerText.includes("TOTAL REPARACION") && el.innerText.includes("€"));
|
||||||
|
if(celdaTexto) totalStr = celdaTexto.innerText.split("TOTAL")[1] || celdaTexto.innerText;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PRECIO (Selector exacto que me diste)
|
|
||||||
const celdaTotal = document.querySelector('td[data-label*="TOTAL REPARACION"]');
|
|
||||||
if (celdaTotal) totalStr = celdaTotal.innerText;
|
|
||||||
|
|
||||||
return { cliente: clean(cliente), direccion: clean(direccion), totalStr };
|
return { cliente: clean(cliente), direccion: clean(direccion), totalStr };
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// LIMPIAR PRECIO
|
||||||
let importe = 0;
|
let importe = 0;
|
||||||
if (info.totalStr) {
|
if (info.totalStr) {
|
||||||
let cleanMoney = info.totalStr.replace(/[^\d.,-]/g, '').replace(',', '.');
|
// Quitamos símbolos raros, espacios, letras... dejamos solo números y coma
|
||||||
importe = Math.abs(parseFloat(cleanMoney) || 0);
|
let cleanMoney = info.totalStr.replace(/[^\d,]/g, '').replace(',', '.');
|
||||||
|
importe = parseFloat(cleanMoney) || 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cruzar con BD
|
// CONSULTAR BASE DE DATOS (Para saber el color)
|
||||||
let docId = null;
|
let docId = null;
|
||||||
let enBD = false;
|
let enBD = false;
|
||||||
|
let direccionBD = "";
|
||||||
|
|
||||||
if (db) {
|
if (db) {
|
||||||
const q = await db.collection(APPOINTMENTS_COL).where("serviceNumber", "==", idServicio).get();
|
const q = await db.collection(APPOINTMENTS_COL).where("serviceNumber", "==", idServicio).get();
|
||||||
if (!q.empty) {
|
if (!q.empty) {
|
||||||
docId = q.docs[0].id;
|
const doc = q.docs[0];
|
||||||
|
docId = doc.id;
|
||||||
enBD = true;
|
enBD = true;
|
||||||
|
direccionBD = doc.data().address || "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ENVIAR DATO EN TIEMPO REAL AL NAVEGADOR
|
// SI LA WEB NO DA DIRECCIÓN, USAR LA DE LA BD (O viceversa)
|
||||||
|
const direccionFinal = info.direccion && info.direccion.length > 3 ? info.direccion : (direccionBD || "Desconocido");
|
||||||
|
|
||||||
|
// ENVIAR
|
||||||
send('ITEM_FOUND', {
|
send('ITEM_FOUND', {
|
||||||
servicio: idServicio,
|
servicio: idServicio,
|
||||||
direccion: info.direccion,
|
direccion: direccionFinal,
|
||||||
|
cliente: info.cliente,
|
||||||
importe: importe,
|
importe: importe,
|
||||||
enBD: enBD,
|
enBD: enBD,
|
||||||
docId: docId
|
docId: docId
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (errDetail) {
|
} catch (errDetail) {
|
||||||
console.error(errDetail);
|
send('LOG', `⚠️ Error ${idServicio}: ${errDetail.message}`);
|
||||||
send('LOG', `⚠️ Error en ${idServicio}: ${errDetail.message}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -246,10 +224,7 @@ async function loginMulti() {
|
||||||
return { browser, page };
|
return { browser, page };
|
||||||
}
|
}
|
||||||
|
|
||||||
// ... (Resto de funciones HS y Saver se mantienen igual que la V10) ...
|
// ... (RESTO DE FUNCIONES HS Y SAVER IGUALES) ...
|
||||||
// IMPORTANTE: Asegúrate de incluirlas aquí abajo (runHSScanner, runHSAnalyzer, runSaver, loginHS)
|
|
||||||
// Copialas del mensaje anterior si hace falta, no cambian.
|
|
||||||
|
|
||||||
async function runHSScanner() {
|
async function runHSScanner() {
|
||||||
let browser = null;
|
let browser = null;
|
||||||
try {
|
try {
|
||||||
|
|
@ -328,14 +303,23 @@ async function runSaver(items, providerType) {
|
||||||
const nowISO = new Date().toISOString();
|
const nowISO = new Date().toISOString();
|
||||||
let count = 0;
|
let count = 0;
|
||||||
items.forEach(item => {
|
items.forEach(item => {
|
||||||
const ref = db.collection(APPOINTMENTS_COL).doc(item.docId);
|
const ref = db.collection(APPOINTMENTS_COL).doc(item.docId || item.servicio); // Fallback al servicio si no hay docId (caso nuevo)
|
||||||
const updateData = { paidAmount: item.importe, paymentState: "Pagado", status: 'completed', paymentDate: nowISO, lastUpdatedByRobot: nowISO };
|
|
||||||
if (providerType === 'MULTI') updateData.multiasistenciaPaymentStatus = 'paid_verified';
|
// Si el documento no existe, lo creamos con lo básico (opcional)
|
||||||
else updateData.homeservePaymentStatus = 'paid_saldo';
|
// Pero tu petición es guardar importes en servicios existentes.
|
||||||
batch.update(ref, updateData);
|
// Si no existe, no hacemos update, o hacemos un set con merge.
|
||||||
count++;
|
// Asumo update si existe.
|
||||||
|
|
||||||
|
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';
|
||||||
|
batch.update(ref, updateData);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
await batch.commit();
|
// Solo hacemos commit de lo que está en BD
|
||||||
|
if(count > 0) await batch.commit();
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue