Actualizar robot.js
This commit is contained in:
56
robot.js
56
robot.js
@@ -380,17 +380,19 @@ async function runHomeserve(ownerId, user, pass, gremiosDB) {
|
||||
async function syncAndArchive(ownerId, provider, currentWebRefs) {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
// 1. Buscamos TODOS los expedientes que están vivos (en el buzón o en el panel)
|
||||
const { rows: dbServices } = await client.query(
|
||||
`SELECT service_ref FROM scraped_services
|
||||
WHERE owner_id = $1 AND provider = $2 AND status = 'pending'`,
|
||||
`SELECT id, service_ref, assigned_to, raw_data FROM scraped_services
|
||||
WHERE owner_id = $1 AND provider = $2 AND status IN ('pending', 'imported')`,
|
||||
[ownerId, provider]
|
||||
);
|
||||
|
||||
const refsToArchive = dbServices
|
||||
.map(s => s.service_ref)
|
||||
.filter(ref => !currentWebRefs.includes(ref));
|
||||
// 2. Filtramos cuáles han desaparecido de la web oficial
|
||||
const missingServices = dbServices.filter(s => !currentWebRefs.includes(s.service_ref));
|
||||
const refsToArchive = missingServices.map(s => s.service_ref);
|
||||
|
||||
if (refsToArchive.length > 0) {
|
||||
// 3. Los archivamos (los quitamos del buzón)
|
||||
await client.query(
|
||||
`UPDATE scraped_services
|
||||
SET status = 'archived'
|
||||
@@ -399,6 +401,50 @@ async function syncAndArchive(ownerId, provider, currentWebRefs) {
|
||||
AND service_ref = ANY($3)`,
|
||||
[ownerId, provider, refsToArchive]
|
||||
);
|
||||
|
||||
// ========================================================
|
||||
// 🛡️ EL ESCUDO ANTI-VIAJES EN BALDE (Para la App del Técnico)
|
||||
// ========================================================
|
||||
// Buscamos el ID del estado "Anulado" de este propietario
|
||||
const statusQ = await client.query("SELECT id FROM service_statuses WHERE owner_id = $1 AND name ILIKE '%anulado%' LIMIT 1", [ownerId]);
|
||||
const idAnulado = statusQ.rowCount > 0 ? statusQ.rows[0].id : null;
|
||||
|
||||
if (idAnulado) {
|
||||
for (const svc of missingServices) {
|
||||
// Si el expediente estaba asignado a un técnico...
|
||||
if (svc.assigned_to) {
|
||||
const raw = svc.raw_data || {};
|
||||
const currentStatusId = String(raw.status_operativo || "");
|
||||
|
||||
// ...y su estado actual no era ya "Anulado"
|
||||
if (currentStatusId !== String(idAnulado)) {
|
||||
|
||||
// Comprobamos si el técnico ya lo había "Finalizado" (para no pisarle el trabajo hecho)
|
||||
let isFinal = false;
|
||||
if (currentStatusId) {
|
||||
const checkStatusQ = await client.query("SELECT is_final FROM service_statuses WHERE id = $1", [currentStatusId]);
|
||||
isFinal = checkStatusQ.rowCount > 0 ? checkStatusQ.rows[0].is_final : false;
|
||||
}
|
||||
|
||||
// Si NO estaba finalizado, procedemos a ANULARLO automáticamente
|
||||
if (!isFinal) {
|
||||
raw.status_operativo = idAnulado;
|
||||
|
||||
await client.query("UPDATE scraped_services SET raw_data = $1 WHERE id = $2", [JSON.stringify(raw), svc.id]);
|
||||
|
||||
// Dejamos huella en la trazabilidad para que sepas por qué se anuló
|
||||
await client.query(
|
||||
"INSERT INTO scraped_service_logs (scraped_id, user_name, action, details) VALUES ($1, $2, $3, $4)",
|
||||
[svc.id, "Sistema Robot", "Cancelación Automática", "La compañía ha cancelado/retirado el expediente. Se pasa a estado Anulado para avisar al técnico."]
|
||||
);
|
||||
console.log(`🛡️ [ESCUDO] Expediente ${svc.service_ref} anulado automáticamente (Técnico salvado de viajar en balde).`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// ========================================================
|
||||
|
||||
console.log(`📦 [${provider.toUpperCase()}] Archivados ${refsToArchive.length} expedientes desaparecidos.`);
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user