Actualizar index.js
This commit is contained in:
parent
c0ac4666a1
commit
8a1c660cd6
56
index.js
56
index.js
|
|
@ -1,7 +1,6 @@
|
|||
const { chromium } = require('playwright');
|
||||
const admin = require('firebase-admin');
|
||||
|
||||
|
||||
if (!admin.apps.length) {
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert({
|
||||
|
|
@ -17,8 +16,11 @@ const COLLECTION_PENDIENTES = "multiasistencia_pendientes";
|
|||
|
||||
async function runMultiasistencia() {
|
||||
console.log(`\n🕒 [${new Date().toLocaleTimeString()}] Iniciando ciclo...`);
|
||||
let user, pass;
|
||||
|
||||
// 1. Guardamos la hora de inicio para comparar después
|
||||
const startRunTime = new Date();
|
||||
|
||||
let user, pass;
|
||||
try {
|
||||
const credSnap = await db.collection("providerCredentials").doc("multiasistencia").get();
|
||||
if (!credSnap.exists) return console.error("❌ No hay credenciales.");
|
||||
|
|
@ -90,13 +92,11 @@ async function runMultiasistencia() {
|
|||
|
||||
for (const ref of expedientes) {
|
||||
const detalleUrl = `https://web.multiasistencia.com/w3multi/repasos1.php?reparacion=${ref}`;
|
||||
|
||||
try {
|
||||
await page.goto(detalleUrl, { waitUntil: 'domcontentloaded' });
|
||||
await page.waitForTimeout(1500);
|
||||
|
||||
let foundData = false;
|
||||
|
||||
for (const frame of page.frames()) {
|
||||
try {
|
||||
const scrapData = await frame.evaluate(() => {
|
||||
|
|
@ -105,7 +105,6 @@ async function runMultiasistencia() {
|
|||
|
||||
if (!bodyText.includes("Nombre Cliente") && !bodyText.includes("Asegurado")) return null;
|
||||
|
||||
// Buscar valor por fila: "Etiqueta" | "Valor"
|
||||
const findRowValue = (labels) => {
|
||||
const rows = Array.from(document.querySelectorAll('tr'));
|
||||
for (const tr of rows) {
|
||||
|
|
@ -122,7 +121,6 @@ async function runMultiasistencia() {
|
|||
};
|
||||
|
||||
const allCells = Array.from(document.querySelectorAll('td, th'));
|
||||
|
||||
const getVertical = (keywords) => {
|
||||
const header = allCells.find(el => keywords.some(k => (el.innerText || "").trim().toUpperCase() === k.toUpperCase()));
|
||||
if (!header) return null;
|
||||
|
|
@ -130,13 +128,11 @@ async function runMultiasistencia() {
|
|||
const row = header.parentElement;
|
||||
const tbody = row.parentElement;
|
||||
let nextRow = row.nextElementSibling;
|
||||
|
||||
if (!nextRow && tbody.tagName === 'THEAD') {
|
||||
const table = header.closest('table');
|
||||
const realBody = table ? table.querySelector('tbody') : null;
|
||||
if (realBody && realBody.rows && realBody.rows[0]) nextRow = realBody.rows[0];
|
||||
}
|
||||
|
||||
if (nextRow && nextRow.cells && nextRow.cells[cellIndex]) {
|
||||
return clean(nextRow.cells[cellIndex].innerText);
|
||||
}
|
||||
|
|
@ -151,16 +147,12 @@ async function runMultiasistencia() {
|
|||
return null;
|
||||
};
|
||||
|
||||
// ✅ DESCRIPTION: "Descripción de la Reparación" y cortar antes de la primera fecha dd/mm/yyyy
|
||||
const getDescription = () => {
|
||||
let text =
|
||||
findRowValue(["Descripción de la Reparación"]) ||
|
||||
getHorizontal(["Descripción de la Reparación", "Descripción", "Daños"]) ||
|
||||
"";
|
||||
|
||||
text = clean(text);
|
||||
|
||||
// Cortar en la primera fecha dd/mm/yyyy (da igual si va con paréntesis o no)
|
||||
const idxDate = text.search(/\b\d{2}\/\d{2}\/\d{4}\b/);
|
||||
if (idxDate !== -1) {
|
||||
text = text.substring(0, idxDate).trim();
|
||||
|
|
@ -168,10 +160,9 @@ async function runMultiasistencia() {
|
|||
return text;
|
||||
};
|
||||
|
||||
// ✅ multiStatus: guardar el ESTADO tal cual aparece (incluyendo "(27/07/2025 - 13:13)")
|
||||
const getStatus = () => {
|
||||
const st = findRowValue(["Estado", "Situación"]);
|
||||
if (st) return st; // guardamos todo el texto
|
||||
if (st) return st;
|
||||
return getHorizontal(["Estado", "Situación"]) || "PENDIENTE";
|
||||
};
|
||||
|
||||
|
|
@ -185,13 +176,11 @@ async function runMultiasistencia() {
|
|||
let rawText = "";
|
||||
const titleDiv = Array.from(document.querySelectorAll('div.subtitulo'))
|
||||
.find(d => (d.innerText || "").includes("Teléfono del Cliente"));
|
||||
|
||||
if (titleDiv) {
|
||||
const table = titleDiv.closest('table') || titleDiv.parentElement.querySelector('table');
|
||||
if (table) rawText = table.innerText || "";
|
||||
}
|
||||
if (!rawText) rawText = bodyText;
|
||||
|
||||
const match = rawText.match(/[6789]\d{8}/);
|
||||
return match ? match[0] : "Sin teléfono";
|
||||
};
|
||||
|
|
@ -232,7 +221,9 @@ async function runMultiasistencia() {
|
|||
await db.collection(COLLECTION_PENDIENTES).doc(ref).set({
|
||||
...scrapData,
|
||||
status: "pendiente_validacion",
|
||||
updatedAt: admin.firestore.FieldValue.serverTimestamp()
|
||||
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
// 2. AÑADIMOS "VISTO POR ÚLTIMA VEZ"
|
||||
lastSeenAt: admin.firestore.FieldValue.serverTimestamp()
|
||||
}, { merge: true });
|
||||
|
||||
foundData = true;
|
||||
|
|
@ -247,7 +238,9 @@ async function runMultiasistencia() {
|
|||
serviceNumber: ref,
|
||||
status: "error_formato",
|
||||
clientName: "ERROR - REVISAR MANUAL",
|
||||
updatedAt: admin.firestore.FieldValue.serverTimestamp()
|
||||
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
// También lo marcamos aquí para que no se "borre" si da error de lectura pero sigue existiendo
|
||||
lastSeenAt: admin.firestore.FieldValue.serverTimestamp()
|
||||
}, { merge: true });
|
||||
}
|
||||
|
||||
|
|
@ -274,6 +267,33 @@ async function runMultiasistencia() {
|
|||
}
|
||||
}
|
||||
|
||||
// 3. LIMPIEZA FINAL (Solo afecta a los que tengan 'lastSeenAt' antiguo)
|
||||
console.log("🧹 Verificando servicios que han desaparecido de la web...");
|
||||
|
||||
// Esta consulta busca documentos que tengan el campo lastSeenAt Y sea viejo.
|
||||
// Los documentos antiguos sin ese campo serán ignorados.
|
||||
const snapshotViejos = await db.collection(COLLECTION_PENDIENTES)
|
||||
.where('lastSeenAt', '<', startRunTime)
|
||||
.get();
|
||||
|
||||
if (!snapshotViejos.empty) {
|
||||
console.log(`🗑️ Se han detectado ${snapshotViejos.size} servicios rastreados que ya no están.`);
|
||||
const batch = db.batch();
|
||||
|
||||
snapshotViejos.docs.forEach(doc => {
|
||||
// Opcional: Verificar que no esté ya cerrado para no escribir de más
|
||||
if (doc.data().status !== "cerrado_multiasistencia") {
|
||||
batch.update(doc.ref, {
|
||||
status: "cerrado_multiasistencia",
|
||||
closedAt: admin.firestore.FieldValue.serverTimestamp()
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
await batch.commit();
|
||||
console.log("✅ Limpieza completada.");
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error("❌ Error General:", e.message);
|
||||
} finally {
|
||||
|
|
|
|||
Loading…
Reference in New Issue