Actualizar server.js

This commit is contained in:
2026-03-19 20:06:38 +00:00
parent 62d1b4c6fb
commit ca3e809bdc

View File

@@ -1977,10 +1977,12 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
}
// 🚨 SOLUCIÓN DEFINITIVA PARA ARCHIVADOS:
// Solo archivamos si el robot manda explícitamente la palabra 'archived'.
// Si manda 'pending', 'in_progress', o nada, se asume 'pending' pero NO pisa la BD si ya existía.
let scraperStatus = (svc.status === 'archived') ? 'archived' : 'pending';
// 🛑 NUEVO: ESCUDO DETECTOR DE NOVEDADES (100% Fiable)
const checkExist = await pool.query("SELECT id FROM scraped_services WHERE owner_id=$1 AND provider=$2 AND service_ref=$3", [req.user.accountId, provider, ref]);
const isNewRecord = (checkExist.rowCount === 0);
// GUARDAR EN BD
const insertRes = await pool.query(`
INSERT INTO scraped_services (owner_id, provider, service_ref, status, raw_data, is_urgent)
@@ -1992,19 +1994,18 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
ELSE scraped_services.status
END,
raw_data = scraped_services.raw_data || EXCLUDED.raw_data
RETURNING id, automation_status, (xmax = 0) AS is_new
RETURNING id, automation_status
`, [req.user.accountId, provider, ref, scraperStatus, JSON.stringify(svc), esUrgente]);
const newSvcId = insertRes.rows[0].id;
const autoStatus = insertRes.rows[0].automation_status;
const isNewRecord = insertRes.rows[0].is_new; // 👈 TRUCO NINJA: Si es true, el aviso acaba de nacer
// 📢 ¡CHIVATO DE CONSOLA!
console.log(`[DETECTOR-PRO] Ref: ${ref} | Urgente: ${esUrgente} | Gremio: ${guildId} | Nuevo: ${isNewRecord}`);
console.log(`[DETECTOR-PRO] Ref: ${ref} | Urgente: ${esUrgente} | Gremio: ${guildId} | Es Nuevo: ${isNewRecord}`);
// 👋 1. DISPARADOR DEL MENSAJE DE BIENVENIDA (Solo si acaba de entrar por primera vez)
// 👋 1. DISPARADOR DEL MENSAJE DE BIENVENIDA
if (isNewRecord) {
console.log(`👋 [BIENVENIDA] Nuevo expediente detectado. Disparando WhatsApp...`);
console.log(`👋 [BIENVENIDA] Expediente #${ref} acaba de nacer. Disparando WhatsApp...`);
triggerWhatsAppEvent(req.user.accountId, newSvcId, 'wa_evt_welcome').catch(console.error);
}