REVERT: restaura robot.js al estado anterior a los cambios de hoy
This commit is contained in:
32
robot.js
32
robot.js
@@ -119,14 +119,9 @@ async function runMultiasistencia(ownerId, user, pass) {
|
|||||||
if(desc) return desc;
|
if(desc) return desc;
|
||||||
|
|
||||||
// Intento 2: Buscar en el cuerpo general si falla la tabla
|
// Intento 2: Buscar en el cuerpo general si falla la tabla
|
||||||
// 🩹 FIX: antes se cortaba en el PRIMER salto de línea que hubiera después
|
|
||||||
// de "Descripción" ([^\n]+), así que muchas veces el texto quedaba a medias.
|
|
||||||
// Ahora cogemos todo el texto hasta la siguiente etiqueta de campo conocida
|
|
||||||
// (o una línea en blanco), con un límite de seguridad por si no encuentra ninguna.
|
|
||||||
const body = document.body.innerText;
|
const body = document.body.innerText;
|
||||||
const match = body.match(/Descripción\s*[:\-]?\s*([\s\S]*?)(?:\n\s*\n|Observaciones|Dirección|Domicilio|Población|Localidad|Tel[eé]fono[s]?|Fecha|Reparador|Gremio|C\.?P\.?\b|$)/i);
|
const match = body.match(/Descripción\s*[:\-]?\s*([^\n]+)/i);
|
||||||
if (!match) return "";
|
return match ? match[1].trim() : "";
|
||||||
return match[1].replace(/[ \t]+/g, ' ').trim().substring(0, 3000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extracción más precisa
|
// Extracción más precisa
|
||||||
@@ -236,30 +231,15 @@ async function saveServiceToDB(ownerId, provider, ref, data) {
|
|||||||
const exists = await pool.query("SELECT id FROM services WHERE company_ref = $1 AND owner_id = $2", [ref, ownerId]);
|
const exists = await pool.query("SELECT id FROM services WHERE company_ref = $1 AND owner_id = $2", [ref, ownerId]);
|
||||||
if (exists.rowCount > 0) return;
|
if (exists.rowCount > 0) return;
|
||||||
|
|
||||||
// 🩹 FIX: el panel web lee los campos en español (Nombre Cliente, Teléfono,
|
|
||||||
// Dirección, Descripción). Los extractores de arriba devuelven claves en
|
|
||||||
// inglés (clientName, phone, address, description), así que sin este mapeo
|
|
||||||
// el expediente se guardaba bien en la base de datos pero aparecía vacío
|
|
||||||
// ("Asegurado Sin Nombre") en el panel hasta que alguien lo abría y lo
|
|
||||||
// guardaba a mano. Mantenemos también las claves originales por si algo
|
|
||||||
// más las usa.
|
|
||||||
const normalizedData = {
|
|
||||||
...data,
|
|
||||||
"Nombre Cliente": data.clientName || data["Nombre Cliente"] || "",
|
|
||||||
"Teléfono": data.phone || data["Teléfono"] || "",
|
|
||||||
"Dirección": data.address || data["Dirección"] || "",
|
|
||||||
"Descripción": data.description || data["Descripción"] || ""
|
|
||||||
};
|
|
||||||
|
|
||||||
// Verificar si ya existe en buzón, si existe ACTUALIZAMOS los datos (por si hemos mejorado el scraping)
|
// Verificar si ya existe en buzón, si existe ACTUALIZAMOS los datos (por si hemos mejorado el scraping)
|
||||||
console.log(`💾 Guardando/Actualizando: ${ref} (${normalizedData["Dirección"]})`);
|
console.log(`💾 Guardando/Actualizando: ${ref} (${data.address})`);
|
||||||
|
|
||||||
await pool.query(`
|
await pool.query(`
|
||||||
INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, status)
|
INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, status)
|
||||||
VALUES ($1, $2, $3, $4, 'pending')
|
VALUES ($1, $2, $3, $4, 'pending')
|
||||||
ON CONFLICT (owner_id, provider, service_ref)
|
ON CONFLICT (owner_id, provider, service_ref)
|
||||||
DO UPDATE SET raw_data = EXCLUDED.raw_data, created_at = NOW()
|
DO UPDATE SET raw_data = EXCLUDED.raw_data, created_at = NOW()
|
||||||
`, [ownerId, provider, ref, JSON.stringify(normalizedData)]);
|
`, [ownerId, provider, ref, JSON.stringify(data)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
Reference in New Issue
Block a user