Actualizar server.js

This commit is contained in:
2026-03-18 21:57:04 +00:00
parent b2c7f614c7
commit 11e4f20ddf

View File

@@ -1986,24 +1986,29 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (owner_id, provider, service_ref)
DO UPDATE SET
-- Si el robot trae 'archived', lo machaca y lo archiva.
-- Si trae cualquier otra cosa, RESPETA el estado que tenga actualmente en nuestra BD.
status = CASE
WHEN EXCLUDED.status = 'archived' THEN 'archived'
ELSE scraped_services.status
END,
raw_data = scraped_services.raw_data || EXCLUDED.raw_data,
is_urgent = scraped_services.is_urgent OR EXCLUDED.is_urgent
RETURNING id, automation_status
RETURNING id, automation_status, (xmax = 0) AS is_new
`, [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! (Si no ves esto en Coolify, es que el código no ha subido)
console.log(`[DETECTOR-PRO] Ref: ${ref} | Urgente: ${esUrgente} | Gremio: ${guildId} | Auto: ${autoDispatchEnabled} | Status: ${autoStatus}`);
// 📢 ¡CHIVATO DE CONSOLA!
console.log(`[DETECTOR-PRO] Ref: ${ref} | Urgente: ${esUrgente} | Gremio: ${guildId} | Nuevo: ${isNewRecord}`);
// 🔥 LANZAMIENTO AUTOMÁTICO 🔥
// 👋 1. DISPARADOR DEL MENSAJE DE BIENVENIDA (Solo si acaba de entrar por primera vez)
if (isNewRecord) {
console.log(`👋 [BIENVENIDA] Nuevo expediente detectado. Disparando WhatsApp...`);
triggerWhatsAppEvent(req.user.accountId, newSvcId, 'wa_evt_welcome').catch(console.error);
}
// 🔥 2. LANZAMIENTO AUTOMÁTICO A LA BOLSA 🔥
if (esUrgente && guildId && autoDispatchEnabled && (autoStatus === 'manual' || autoStatus === 'pending')) {
console.log(`⚡ [AUTO-DISPATCH] Lanzando a la bolsa: ${ref}`);
const cpMatch = textoLimpio.match(/\b\d{5}\b/);