Actualizar robot.js

This commit is contained in:
2026-02-16 15:24:46 +00:00
parent 678053b16c
commit 0aa100c36e

View File

@@ -214,12 +214,25 @@ async function syncAndArchive(ownerId, provider, currentWebRefs) {
} }
async function saveServiceToDB(ownerId, provider, ref, data) { async function saveServiceToDB(ownerId, provider, ref, data) {
console.log(`💾 Guardando ${provider.toUpperCase()} ${ref}...`); console.log(`💾 Guardando/Actualizando ${provider.toUpperCase()} ${ref}...`);
// Utilizamos el operador || de JSONB en Postgres para fusionar los datos.
// EXCLUDED.raw_data contiene lo nuevo del scraper.
// scraped_services.raw_data contiene lo que ya estaba (incluyendo assigned_to, etc).
// Al poner EXCLUDED primero y concatenar lo existente después, preservamos los campos internos
// que el scraper no conoce, y solo actualizamos lo que viene de fuera.
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(), status = 'pending' DO UPDATE SET
raw_data = EXCLUDED.raw_data || scraped_services.raw_data,
status = CASE
WHEN scraped_services.status = 'archived' THEN 'archived'
WHEN scraped_services.status = 'imported' THEN 'imported'
ELSE 'pending'
END
`, [ownerId, provider, ref, JSON.stringify(data)]); `, [ownerId, provider, ref, JSON.stringify(data)]);
} }