Actualizar robot_cobros.js

This commit is contained in:
marsalva 2025-12-28 21:38:06 +00:00
parent 3cb5cdcaae
commit 09393c6b5f
1 changed files with 6 additions and 15 deletions

View File

@ -24,7 +24,7 @@ const APPOINTMENTS_COL = "appointments";
const app = express();
app.use(cors({ origin: '*' }));
app.use(express.json({ limit: '10mb' })); // Permitir payloads grandes para guardar
app.use(express.json({ limit: '10mb' }));
// --- ENDPOINT PRINCIPAL ---
app.post('/api/robot-cobros', async (req, res) => {
@ -33,18 +33,15 @@ app.post('/api/robot-cobros', async (req, res) => {
try {
if (action === 'scan') {
// Paso 1: Buscar fechas
const lista = await runScanner();
res.json({ success: true, data: lista });
}
else if (action === 'analyze') {
// Paso 2: Leer fecha y cruzar con DB (Sin guardar)
if (!url) throw new Error("Falta URL");
const analisis = await runAnalyzer(url);
res.json({ success: true, ...analisis });
}
else if (action === 'save_data') {
// Paso 3: Guardar lo confirmado
if (!dataToSave || !Array.isArray(dataToSave)) throw new Error("No hay datos");
const count = await runSaver(dataToSave);
res.json({ success: true, count });
@ -58,7 +55,7 @@ app.post('/api/robot-cobros', async (req, res) => {
}
});
// --- 1. ESCÁNER DE FECHAS ---
// --- 1. ESCÁNER ---
async function runScanner() {
let browser = null;
try {
@ -80,7 +77,7 @@ async function runScanner() {
} catch (e) { throw e; } finally { if(browser) await browser.close(); }
}
// --- 2. ANALIZADOR (PREVISUALIZACIÓN) ---
// --- 2. ANALIZADOR ---
async function runAnalyzer(targetUrl) {
let browser = null;
try {
@ -91,7 +88,6 @@ async function runAnalyzer(targetUrl) {
await page.goto(targetUrl, { timeout: 60000 });
await page.waitForTimeout(1500);
// Buscar botón "Desglose Servicios"
const botonPulsado = await page.evaluate(() => {
const elementos = Array.from(document.querySelectorAll('input, button, a'));
const target = elementos.find(el => {
@ -104,13 +100,11 @@ async function runAnalyzer(targetUrl) {
if (botonPulsado) await page.waitForTimeout(3000);
// Extraer datos visuales
const datosRaw = await page.evaluate(() => {
const filas = Array.from(document.querySelectorAll('tr'));
const datos = [];
filas.forEach(tr => {
const tds = tr.querySelectorAll('td');
// Col 0: Servicio, Col 1: Dirección, Col 5: Saldo
if (tds.length >= 6) {
const servicio = tds[0].innerText.trim();
const direccion = tds[1].innerText.trim();
@ -123,7 +117,6 @@ async function runAnalyzer(targetUrl) {
return datos;
});
// Cruzar con Firebase (Lectura)
const encontrados = [];
const noEncontrados = [];
@ -135,7 +128,6 @@ async function runAnalyzer(targetUrl) {
const q = await db.collection(APPOINTMENTS_COL).where("serviceNumber", "==", item.servicio).get();
if (!q.empty) {
// Si hay duplicados, cogemos el primero o todos
q.forEach(doc => {
encontrados.push({
servicio: item.servicio,
@ -153,14 +145,12 @@ async function runAnalyzer(targetUrl) {
}
}
}
console.log(`✅ Análisis: ${encontrados.length} encontrados, ${noEncontrados.length} no encontrados.`);
return { encontrados, noEncontrados };
} catch (e) { throw e; } finally { if(browser) await browser.close(); }
}
// --- 3. GUARDADOR (RÁPIDO) ---
// --- 3. GUARDADOR (Con paymentState) ---
async function runSaver(items) {
if (!db) return 0;
const batch = db.batch();
@ -171,6 +161,7 @@ async function runSaver(items) {
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',
@ -180,7 +171,7 @@ async function runSaver(items) {
});
await batch.commit();
console.log(`💾 Guardados ${count} documentos en DB.`);
console.log(`💾 Guardados ${count} documentos.`);
return count;
}