From 5e9fae3eec8d01c0a017b2a45353e8a1ebfcc459 Mon Sep 17 00:00:00 2001 From: marsalva Date: Sun, 6 Sep 2026 19:20:30 +0000 Subject: [PATCH] =?UTF-8?q?Fix:=20verificaci=C3=B3n=20de=20cierre=20real?= =?UTF-8?q?=20antes=20de=20anular=20en=20HomeServe=20(misma=20protecci?= =?UTF-8?q?=C3=B3n=20que=20Multi)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Antes de anular por ausencia en "Servicios en curso", se comprueba la ficha real del expediente (campo "Actualmente en"): solo se anula si claramente pone cerrado/finalizado/anulado/cancelado/rechazado. Auditoría previa: de los últimos 50 "Anulado" de HomeServe, 1 (15849187) seguía abierto de verdad; ya corregido a mano y etiquetado para reapertura automática. --- robot.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/robot.js b/robot.js index c856878..f851980 100644 --- a/robot.js +++ b/robot.js @@ -326,6 +326,43 @@ async function runMultiasistencia(ownerId, user, pass, gremiosDB) { } } +// 🔍 NUEVO: Antes de anular un expediente de HomeServe que "ha desaparecido" de la lista de +// Servicios en curso, comprobamos su ficha individual ("ver_servicioencurso"). El campo +// "ACTUALMENTE EN" es la señal fiable: si dice algo como "SERVICIO CERRADO", "FINALIZADO DEL +// TODO", "ANULADO" o similar, confirma que está cerrado de verdad. Si dice cualquier otra cosa +// (p.ej. "En espera de..."), sigue abierto y NO se debe anular, aunque no saliera en la lista +// en este ciclo del robot (la lista puede fallar por un hipo de red, no solo por cierre real). +// Devuelve: true = confirmado cerrado (se puede anular) | false = sigue abierto, NO anular | +// null = no se ha podido comprobar (fail-safe: tampoco anular). +async function verificarCierreRealHomeServe(page, ref) { + const PALABRAS_CIERRE = ['CERRADO', 'FINALIZADO', 'ANULADO', 'CANCELADO', 'RECHAZADO']; + try { + await gotoWithRetry(page, `https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=ver_servicioencurso&Servicio=${ref}`); + await page.waitForTimeout(1200); + const actualmenteEn = await page.evaluate(() => { + const rows = Array.from(document.querySelectorAll('tr')); + let valor = null; + rows.forEach(r => { + const cells = r.querySelectorAll('td'); + if (cells.length >= 2) { + const k = cells[0].innerText.replace(/\s+/g, ' ').trim().toUpperCase(); + if (k.startsWith('ACTUALMENTE EN')) valor = cells[1].innerText.trim(); + } + }); + return valor; + }); + if (!actualmenteEn) { + console.log(`⚠️ [HomeServe] No se encontró el campo "Actualmente en" para ${ref}. No se anula por precaución.`); + return null; + } + const actualmenteEnUpper = actualmenteEn.toUpperCase(); + return PALABRAS_CIERRE.some(p => actualmenteEnUpper.includes(p)); + } catch (e) { + console.error(`⚠️ [HomeServe] No se pudo verificar el cierre real de ${ref}: ${e.message}`); + return null; + } +} + // ========================================== // 🧹 HOMESERVE (ROBOT MEJORADO Y DETECTOR DE URGENCIAS) // ========================================== @@ -380,7 +417,9 @@ async function runHomeserve(ownerId, user, pass, gremiosDB) { const refs = [...new Set(listaConIconos.map(item => item.ref))]; if (refs.length > 0) { - await syncAndArchive(ownerId, 'homeserve', refs); + await syncAndArchive(ownerId, 'homeserve', refs, { + verificarRealmenteCerrado: (ref) => verificarCierreRealHomeServe(page, ref) + }); } console.log(`🔍 [HomeServe] ${refs.length} expedientes detectados.`);