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 { chromium } = require('playwright');
|
||||||
const admin = require('firebase-admin');
|
const admin = require('firebase-admin');
|
||||||
|
|
||||||
|
|
||||||
if (!admin.apps.length) {
|
if (!admin.apps.length) {
|
||||||
admin.initializeApp({
|
admin.initializeApp({
|
||||||
credential: admin.credential.cert({
|
credential: admin.credential.cert({
|
||||||
|
|
@ -17,8 +16,11 @@ const COLLECTION_PENDIENTES = "multiasistencia_pendientes";
|
||||||
|
|
||||||
async function runMultiasistencia() {
|
async function runMultiasistencia() {
|
||||||
console.log(`\n🕒 [${new Date().toLocaleTimeString()}] Iniciando ciclo...`);
|
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 {
|
try {
|
||||||
const credSnap = await db.collection("providerCredentials").doc("multiasistencia").get();
|
const credSnap = await db.collection("providerCredentials").doc("multiasistencia").get();
|
||||||
if (!credSnap.exists) return console.error("❌ No hay credenciales.");
|
if (!credSnap.exists) return console.error("❌ No hay credenciales.");
|
||||||
|
|
@ -90,13 +92,11 @@ async function runMultiasistencia() {
|
||||||
|
|
||||||
for (const ref of expedientes) {
|
for (const ref of expedientes) {
|
||||||
const detalleUrl = `https://web.multiasistencia.com/w3multi/repasos1.php?reparacion=${ref}`;
|
const detalleUrl = `https://web.multiasistencia.com/w3multi/repasos1.php?reparacion=${ref}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await page.goto(detalleUrl, { waitUntil: 'domcontentloaded' });
|
await page.goto(detalleUrl, { waitUntil: 'domcontentloaded' });
|
||||||
await page.waitForTimeout(1500);
|
await page.waitForTimeout(1500);
|
||||||
|
|
||||||
let foundData = false;
|
let foundData = false;
|
||||||
|
|
||||||
for (const frame of page.frames()) {
|
for (const frame of page.frames()) {
|
||||||
try {
|
try {
|
||||||
const scrapData = await frame.evaluate(() => {
|
const scrapData = await frame.evaluate(() => {
|
||||||
|
|
@ -105,7 +105,6 @@ async function runMultiasistencia() {
|
||||||
|
|
||||||
if (!bodyText.includes("Nombre Cliente") && !bodyText.includes("Asegurado")) return null;
|
if (!bodyText.includes("Nombre Cliente") && !bodyText.includes("Asegurado")) return null;
|
||||||
|
|
||||||
// Buscar valor por fila: "Etiqueta" | "Valor"
|
|
||||||
const findRowValue = (labels) => {
|
const findRowValue = (labels) => {
|
||||||
const rows = Array.from(document.querySelectorAll('tr'));
|
const rows = Array.from(document.querySelectorAll('tr'));
|
||||||
for (const tr of rows) {
|
for (const tr of rows) {
|
||||||
|
|
@ -122,7 +121,6 @@ async function runMultiasistencia() {
|
||||||
};
|
};
|
||||||
|
|
||||||
const allCells = Array.from(document.querySelectorAll('td, th'));
|
const allCells = Array.from(document.querySelectorAll('td, th'));
|
||||||
|
|
||||||
const getVertical = (keywords) => {
|
const getVertical = (keywords) => {
|
||||||
const header = allCells.find(el => keywords.some(k => (el.innerText || "").trim().toUpperCase() === k.toUpperCase()));
|
const header = allCells.find(el => keywords.some(k => (el.innerText || "").trim().toUpperCase() === k.toUpperCase()));
|
||||||
if (!header) return null;
|
if (!header) return null;
|
||||||
|
|
@ -130,13 +128,11 @@ async function runMultiasistencia() {
|
||||||
const row = header.parentElement;
|
const row = header.parentElement;
|
||||||
const tbody = row.parentElement;
|
const tbody = row.parentElement;
|
||||||
let nextRow = row.nextElementSibling;
|
let nextRow = row.nextElementSibling;
|
||||||
|
|
||||||
if (!nextRow && tbody.tagName === 'THEAD') {
|
if (!nextRow && tbody.tagName === 'THEAD') {
|
||||||
const table = header.closest('table');
|
const table = header.closest('table');
|
||||||
const realBody = table ? table.querySelector('tbody') : null;
|
const realBody = table ? table.querySelector('tbody') : null;
|
||||||
if (realBody && realBody.rows && realBody.rows[0]) nextRow = realBody.rows[0];
|
if (realBody && realBody.rows && realBody.rows[0]) nextRow = realBody.rows[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nextRow && nextRow.cells && nextRow.cells[cellIndex]) {
|
if (nextRow && nextRow.cells && nextRow.cells[cellIndex]) {
|
||||||
return clean(nextRow.cells[cellIndex].innerText);
|
return clean(nextRow.cells[cellIndex].innerText);
|
||||||
}
|
}
|
||||||
|
|
@ -151,16 +147,12 @@ async function runMultiasistencia() {
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ✅ DESCRIPTION: "Descripción de la Reparación" y cortar antes de la primera fecha dd/mm/yyyy
|
|
||||||
const getDescription = () => {
|
const getDescription = () => {
|
||||||
let text =
|
let text =
|
||||||
findRowValue(["Descripción de la Reparación"]) ||
|
findRowValue(["Descripción de la Reparación"]) ||
|
||||||
getHorizontal(["Descripción de la Reparación", "Descripción", "Daños"]) ||
|
getHorizontal(["Descripción de la Reparación", "Descripción", "Daños"]) ||
|
||||||
"";
|
"";
|
||||||
|
|
||||||
text = clean(text);
|
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/);
|
const idxDate = text.search(/\b\d{2}\/\d{2}\/\d{4}\b/);
|
||||||
if (idxDate !== -1) {
|
if (idxDate !== -1) {
|
||||||
text = text.substring(0, idxDate).trim();
|
text = text.substring(0, idxDate).trim();
|
||||||
|
|
@ -168,10 +160,9 @@ async function runMultiasistencia() {
|
||||||
return text;
|
return text;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ✅ multiStatus: guardar el ESTADO tal cual aparece (incluyendo "(27/07/2025 - 13:13)")
|
|
||||||
const getStatus = () => {
|
const getStatus = () => {
|
||||||
const st = findRowValue(["Estado", "Situación"]);
|
const st = findRowValue(["Estado", "Situación"]);
|
||||||
if (st) return st; // guardamos todo el texto
|
if (st) return st;
|
||||||
return getHorizontal(["Estado", "Situación"]) || "PENDIENTE";
|
return getHorizontal(["Estado", "Situación"]) || "PENDIENTE";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -185,13 +176,11 @@ async function runMultiasistencia() {
|
||||||
let rawText = "";
|
let rawText = "";
|
||||||
const titleDiv = Array.from(document.querySelectorAll('div.subtitulo'))
|
const titleDiv = Array.from(document.querySelectorAll('div.subtitulo'))
|
||||||
.find(d => (d.innerText || "").includes("Teléfono del Cliente"));
|
.find(d => (d.innerText || "").includes("Teléfono del Cliente"));
|
||||||
|
|
||||||
if (titleDiv) {
|
if (titleDiv) {
|
||||||
const table = titleDiv.closest('table') || titleDiv.parentElement.querySelector('table');
|
const table = titleDiv.closest('table') || titleDiv.parentElement.querySelector('table');
|
||||||
if (table) rawText = table.innerText || "";
|
if (table) rawText = table.innerText || "";
|
||||||
}
|
}
|
||||||
if (!rawText) rawText = bodyText;
|
if (!rawText) rawText = bodyText;
|
||||||
|
|
||||||
const match = rawText.match(/[6789]\d{8}/);
|
const match = rawText.match(/[6789]\d{8}/);
|
||||||
return match ? match[0] : "Sin teléfono";
|
return match ? match[0] : "Sin teléfono";
|
||||||
};
|
};
|
||||||
|
|
@ -232,7 +221,9 @@ async function runMultiasistencia() {
|
||||||
await db.collection(COLLECTION_PENDIENTES).doc(ref).set({
|
await db.collection(COLLECTION_PENDIENTES).doc(ref).set({
|
||||||
...scrapData,
|
...scrapData,
|
||||||
status: "pendiente_validacion",
|
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 });
|
}, { merge: true });
|
||||||
|
|
||||||
foundData = true;
|
foundData = true;
|
||||||
|
|
@ -247,7 +238,9 @@ async function runMultiasistencia() {
|
||||||
serviceNumber: ref,
|
serviceNumber: ref,
|
||||||
status: "error_formato",
|
status: "error_formato",
|
||||||
clientName: "ERROR - REVISAR MANUAL",
|
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 });
|
}, { 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) {
|
} catch (e) {
|
||||||
console.error("❌ Error General:", e.message);
|
console.error("❌ Error General:", e.message);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue