diff --git a/server.js b/server.js index 28ba61d..912a76d 100644 --- a/server.js +++ b/server.js @@ -1856,7 +1856,7 @@ async function dispatchToBolsa(serviceId, guildId, cp, accountId, userId) { } // ========================================== -// 📥 RECEPCIÓN DE SERVICIOS (DETECTOR MODO DIOS + AUTO-BOLSA) +// 📥 RECEPCIÓN DE SERVICIOS (DETECTOR + AUTO-BOLSA CORREGIDO) // ========================================== app.post("/providers/scraped", authMiddleware, async (req, res) => { try { @@ -1871,7 +1871,7 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => { [req.user.accountId, provider] ); - // CORRECCIÓN INTERRUPTOR (Acepta el '1' de la base de datos) + // ✅ CORRECCIÓN: Aceptamos 1, '1', 't' o true como "Encendido" let autoDispatchEnabled = false; if (credsQ.rowCount > 0) { const val = credsQ.rows[0].auto_dispatch; @@ -1880,8 +1880,14 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => { let count = 0; for (const svc of services) { - const ref = svc['service_ref'] || svc['SERVICIO'] || svc['Referencia'] || svc['Expediente'] || (svc.raw_data && (svc.raw_data['SERVICIO'] || svc.raw_data['Referencia'])); - if (!ref) continue; + + const ref = svc['service_ref'] + || svc['SERVICIO'] + || svc['Referencia'] + || svc['Expediente'] + || (svc.raw_data && (svc.raw_data['SERVICIO'] || svc.raw_data['Referencia'])); + + if (!ref) continue; let esUrgente = false; const todoElTexto = JSON.stringify(svc).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/\n/g, " "); @@ -1905,14 +1911,20 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => { const newSvcId = insertRes.rows[0].id; const autoStatus = insertRes.rows[0].automation_status; - // CHIVATO DE CONSOLA - console.log(`[DEBUG-BOLSA] Ref: ${ref} | Urgente: ${esUrgente} | Gremio: ${guildId} | Auto_ON: ${autoDispatchEnabled} | Estado: ${autoStatus}`); + // 🕵️ CHIVATO: Esto aparecerá en tu consola de Coolify + console.log(`[DEBUG-RECEPCION] Ref: ${ref} | Urgente: ${esUrgente} | Gremio: ${guildId} | Auto_ON: ${autoDispatchEnabled}`); - // LANZAMIENTO AUTOMÁTICO + // 🔥 LANZAMIENTO AUTOMÁTICO 🔥 + // Si es urgente, tiene gremio, el botón está ON y el servicio no ha sido procesado aún... if (esUrgente && guildId && autoDispatchEnabled && (autoStatus === 'manual' || autoStatus === 'pending')) { + console.log(`⚡ [AUTO-DISPATCH] ¡Hillmer detectada! Lanzando a la bolsa...`); const cpMatch = todoElTexto.match(/\b\d{5}\b/); - dispatchToBolsa(newSvcId, guildId, cpMatch ? cpMatch[0] : "00000", req.user.accountId, req.user.sub); + const cpFinal = cpMatch ? cpMatch[0] : "00000"; + + // Usamos await para asegurar que se ejecute la lógica de bolsa + await dispatchToBolsa(newSvcId, guildId, cpFinal, req.user.accountId, req.user.sub); } + count++; } res.json({ ok: true, inserted: count });