Actualizar server.js
This commit is contained in:
17
server.js
17
server.js
@@ -1986,24 +1986,29 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
|
|||||||
VALUES ($1, $2, $3, $4, $5, $6)
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
ON CONFLICT (owner_id, provider, service_ref)
|
ON CONFLICT (owner_id, provider, service_ref)
|
||||||
DO UPDATE SET
|
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
|
status = CASE
|
||||||
WHEN EXCLUDED.status = 'archived' THEN 'archived'
|
WHEN EXCLUDED.status = 'archived' THEN 'archived'
|
||||||
ELSE scraped_services.status
|
ELSE scraped_services.status
|
||||||
END,
|
END,
|
||||||
raw_data = scraped_services.raw_data || EXCLUDED.raw_data,
|
raw_data = scraped_services.raw_data || EXCLUDED.raw_data,
|
||||||
is_urgent = scraped_services.is_urgent OR EXCLUDED.is_urgent
|
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]);
|
`, [req.user.accountId, provider, ref, scraperStatus, JSON.stringify(svc), esUrgente]);
|
||||||
|
|
||||||
const newSvcId = insertRes.rows[0].id;
|
const newSvcId = insertRes.rows[0].id;
|
||||||
const autoStatus = insertRes.rows[0].automation_status;
|
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)
|
// 📢 ¡CHIVATO DE CONSOLA!
|
||||||
console.log(`[DETECTOR-PRO] Ref: ${ref} | Urgente: ${esUrgente} | Gremio: ${guildId} | Auto: ${autoDispatchEnabled} | Status: ${autoStatus}`);
|
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')) {
|
if (esUrgente && guildId && autoDispatchEnabled && (autoStatus === 'manual' || autoStatus === 'pending')) {
|
||||||
console.log(`⚡ [AUTO-DISPATCH] Lanzando a la bolsa: ${ref}`);
|
console.log(`⚡ [AUTO-DISPATCH] Lanzando a la bolsa: ${ref}`);
|
||||||
const cpMatch = textoLimpio.match(/\b\d{5}\b/);
|
const cpMatch = textoLimpio.match(/\b\d{5}\b/);
|
||||||
|
|||||||
Reference in New Issue
Block a user