Actualizar robot_cobros.js
This commit is contained in:
parent
fddd71c5f8
commit
47bac60db6
|
|
@ -29,7 +29,7 @@ app.use(express.json());
|
||||||
// --- ENDPOINT UNIFICADO ---
|
// --- ENDPOINT UNIFICADO ---
|
||||||
app.post('/api/robot-cobros', async (req, res) => {
|
app.post('/api/robot-cobros', async (req, res) => {
|
||||||
const { action, urls } = req.body;
|
const { action, urls } = req.body;
|
||||||
console.log(`🔔 Orden recibida: ${action.toUpperCase()}`);
|
console.log(`🔔 Orden recibida: ${action ? action.toUpperCase() : 'DESCONOCIDA'}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (action === 'scan') {
|
if (action === 'scan') {
|
||||||
|
|
@ -42,7 +42,7 @@ app.post('/api/robot-cobros', async (req, res) => {
|
||||||
res.json({ success: true, mode: 'process', count: procesados });
|
res.json({ success: true, mode: 'process', count: procesados });
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new Error("Acción no válida");
|
throw new Error("Acción no válida o faltan parámetros.");
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("❌ Error:", err.message);
|
console.error("❌ Error:", err.message);
|
||||||
|
|
@ -95,21 +95,21 @@ async function runProcessor(urlsAProcesar) {
|
||||||
await page.waitForTimeout(1500);
|
await page.waitForTimeout(1500);
|
||||||
|
|
||||||
// 2. BUSCAR BOTÓN ESPECÍFICO "DESGLOSE SERVICIOS"
|
// 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'...");
|
console.log(" 🔘 Buscando botón 'Desglose Servicios'...");
|
||||||
|
|
||||||
const botonPulsado = await page.evaluate(() => {
|
const botonPulsado = await page.evaluate(() => {
|
||||||
// Buscamos en inputs (tipo submit/button), botones y enlaces
|
// Buscamos inputs, botones y enlaces
|
||||||
const elementos = Array.from(document.querySelectorAll('input[type="button"], input[type="submit"], button, a'));
|
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
|
// Opción A: Buscar texto exacto "DESGLOSE" y "SERVICIO"
|
||||||
const target = elementos.find(el => {
|
const target = elementos.find(el => {
|
||||||
const texto = (el.value || el.innerText || "").toUpperCase();
|
const texto = (el.value || el.innerText || "").toUpperCase();
|
||||||
return texto.includes("SERVICIO") && texto.includes("DESGLOSE");
|
return texto.includes("SERVICIO") && texto.includes("DESGLOSE");
|
||||||
});
|
});
|
||||||
|
|
||||||
// Si no encontramos uno tan específico, probamos solo con "SERVICIOS" dentro de la tabla de botones
|
// Opción B (Fallback): Buscar solo "SERVICIOS" si falla lo anterior
|
||||||
const targetSecundario = elements.find(el => (el.value || el.innerText || "").toUpperCase().includes("SERVICIOS"));
|
// CORRECCIÓN AQUÍ: antes ponía 'elements' y fallaba
|
||||||
|
const targetSecundario = elementos.find(el => (el.value || el.innerText || "").toUpperCase().includes("SERVICIOS"));
|
||||||
|
|
||||||
const finalTarget = target || targetSecundario;
|
const finalTarget = target || targetSecundario;
|
||||||
|
|
||||||
|
|
@ -122,7 +122,7 @@ async function runProcessor(urlsAProcesar) {
|
||||||
|
|
||||||
if (botonPulsado) {
|
if (botonPulsado) {
|
||||||
console.log(" ✅ Botón pulsado. Esperando tabla...");
|
console.log(" ✅ Botón pulsado. Esperando tabla...");
|
||||||
await page.waitForTimeout(3000); // Esperar a que cargue la tabla de datos
|
await page.waitForTimeout(3000);
|
||||||
} else {
|
} else {
|
||||||
console.warn(" ⚠️ No encontré el botón de Servicios. Intentando leer por si ya estamos dentro.");
|
console.warn(" ⚠️ No encontré el botón de Servicios. Intentando leer por si ya estamos dentro.");
|
||||||
}
|
}
|
||||||
|
|
@ -140,7 +140,7 @@ async function runProcessor(urlsAProcesar) {
|
||||||
const servicio = tds[0].innerText.trim();
|
const servicio = tds[0].innerText.trim();
|
||||||
const saldoRaw = tds[5].innerText.trim();
|
const saldoRaw = tds[5].innerText.trim();
|
||||||
|
|
||||||
// Validar que sea un número de servicio real
|
// Validar que sea un número de servicio real (5 o más dígitos)
|
||||||
if (/^\d{5,}$/.test(servicio)) {
|
if (/^\d{5,}$/.test(servicio)) {
|
||||||
datos.push({ servicio, saldoRaw });
|
datos.push({ servicio, saldoRaw });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue