Corrige mapeo de campos (Nombre Cliente/Telefono/Direccion/Descripcion) para que el panel muestre bi
This commit is contained in:
19
robot.js
19
robot.js
@@ -236,15 +236,30 @@ 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} (${data.address})`);
|
console.log(`💾 Guardando/Actualizando: ${ref} (${normalizedData["Dirección"]})`);
|
||||||
|
|
||||||
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(data)]);
|
`, [ownerId, provider, ref, JSON.stringify(normalizedData)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
Reference in New Issue
Block a user