Actualizar server.js

This commit is contained in:
2026-03-15 11:04:13 +00:00
parent 78785ca44a
commit 17fdecced2

View File

@@ -1812,7 +1812,7 @@ app.post("/providers/credentials", authMiddleware, async (req, res) => {
});
// ==========================================
// 📥 RECEPCIÓN DE SERVICIOS (EMBUDO INTELIGENTE CON MEMORIA DE URGENCIA)
// 📥 RECEPCIÓN DE SERVICIOS (EMBUDO INTELIGENTE DEFINITIVO)
// ==========================================
app.post("/providers/scraped", authMiddleware, async (req, res) => {
try {
@@ -1832,6 +1832,7 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
for (const svc of services) {
// 1. EXTRAER REFERENCIA (¡AQUÍ FALTABA 'SERVICIO'!)
const ref = svc['service_ref']
|| svc['SERVICIO']
|| svc['Referencia']
@@ -1841,20 +1842,20 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
if (!ref) continue;
// 🔥 3. DETECTOR DE URGENCIAS (SOLO VALORES, CERO FALSOS POSITIVOS) 🔥
// 2. DETECTOR DE URGENCIAS (SOLO VALORES, CERO FALSOS POSITIVOS)
let esUrgente = false;
for (const key in svc) {
const nombreColumna = key.toLowerCase();
const nombreColumna = String(key).toLowerCase();
const valor = String(svc[key]).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
// 1. Caso HomeServe (Frases exactas en cualquier valor)
// HomeServe: Frases exactas dentro de los valores
if (valor.includes('atencion presencial urgencias') || valor.includes('atencion de la urgencia')) {
esUrgente = true;
break;
}
// 2. Caso Multiasistencia u otros (Columna "Urgente" = Sí)
// Multiasistencia: Si la columna contiene "urgent" y el valor es "si"
if ((nombreColumna.includes('urgent') || nombreColumna === 'urgencia') && (valor === 'si' || valor === 'true')) {
esUrgente = true;
break;
@@ -1863,6 +1864,7 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
const guildId = svc.guild_id || svc['guild_id'] || (svc.raw_data && svc.raw_data.guild_id);
// 3. GUARDAMOS EN LA BD (CON MEMORIA: OR EXCLUDED.is_urgent)
const insertRes = await pool.query(`
INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, is_urgent)
VALUES ($1, $2, $3, $4, $5)
@@ -1876,6 +1878,7 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
const newSvcId = insertRes.rows[0].id;
const autoStatus = insertRes.rows[0].automation_status;
// 4. AUTO-DESPACHO
if (esUrgente && guildId && autoDispatchEnabled && (autoStatus === 'manual' || autoStatus === 'pending')) {
console.log(`⚡ [AUTO-DISPATCH] Lanzando urgencia ${ref} de ${provider} a la bolsa...`);
@@ -1902,39 +1905,6 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
}
});
app.get("/providers/scraped", authMiddleware, async (req, res) => {
try {
const q = await pool.query(`
SELECT
s.*,
ap.token as active_token,
EXTRACT(EPOCH FROM (ap.expires_at - CURRENT_TIMESTAMP)) as seconds_left,
u.full_name as current_worker_name,
(SELECT json_agg(json_build_object('name', u2.full_name, 'phone', u2.phone))
FROM assignment_pings ap2
JOIN users u2 ON ap2.user_id = u2.id
WHERE ap2.scraped_id = s.id AND ap2.status IN ('expired', 'rejected')) as attempted_workers_data
FROM scraped_services s
LEFT JOIN assignment_pings ap ON s.id = ap.scraped_id AND ap.status = 'pending'
LEFT JOIN users u ON ap.user_id = u.id
WHERE s.owner_id = $1
ORDER BY s.created_at DESC
`, [req.user.accountId]);
const services = q.rows.map(row => {
if (row.seconds_left && row.seconds_left > 0) {
row.token_expires_at = new Date(Date.now() + (row.seconds_left * 1000));
} else if (row.seconds_left <= 0) {
row.token_expires_at = new Date(Date.now() - 1000);
}
delete row.seconds_left;
return row;
});
res.json({ ok: true, services });
} catch (e) { res.status(500).json({ ok: false }); }
});
// ==========================================
// 🤖 RUTA DE AUTOMATIZACIÓN (MEJORADA)
// ==========================================