Actualizar server.js
This commit is contained in:
46
server.js
46
server.js
@@ -1812,7 +1812,7 @@ app.post("/providers/credentials", authMiddleware, async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 📥 RECEPCIÓN DE SERVICIOS (EMBUDO INTELIGENTE CON AUTO-DESPACHO Y TODOTERRENO)
|
// 📥 RECEPCIÓN DE SERVICIOS (EMBUDO INTELIGENTE CON AUTO-DESPACHO Y TODOTERRENO FINAL)
|
||||||
// ==========================================
|
// ==========================================
|
||||||
app.post("/providers/scraped", authMiddleware, async (req, res) => {
|
app.post("/providers/scraped", authMiddleware, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@@ -1833,37 +1833,29 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
|
|||||||
|
|
||||||
for (const svc of services) {
|
for (const svc of services) {
|
||||||
|
|
||||||
// 2. Extraer la referencia
|
// 2. Extraer la referencia (revisamos la raíz y el raw_data por si viene anidado)
|
||||||
const ref = svc['Referencia'] || svc['Nº Siniestro'] || svc['Expediente'] || svc['service_ref'];
|
const ref = svc['Referencia'] || svc['Nº Siniestro'] || svc['Expediente'] || svc['service_ref'] || (svc.raw_data && svc.raw_data['Referencia']);
|
||||||
if (!ref) continue;
|
if (!ref) continue;
|
||||||
|
|
||||||
// 🔥 3. DETECTOR DE URGENCIAS ANTI-FALLOS (TODOTERRENO) 🔥
|
// 🔥 3. DETECTOR DE URGENCIAS ANTI-FALLOS (APLASTAMIENTO TOTAL) 🔥
|
||||||
let esUrgente = false;
|
let esUrgente = false;
|
||||||
|
|
||||||
// Recorremos TODOS los campos que mande el proveedor
|
// Convertimos TODO el expediente (con todas sus subcapas) a un texto gigante,
|
||||||
for (const key in svc) {
|
// en minúsculas y sin acentos para que no se escape nada.
|
||||||
const valor = String(svc[key]).toLowerCase();
|
const todoElTexto = JSON.stringify(svc).toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
||||||
|
|
||||||
if (
|
if (
|
||||||
valor.includes('atencion presencial urgencias') ||
|
todoElTexto.includes('atencion presencial urgencias') ||
|
||||||
valor.includes('urgencia') ||
|
todoElTexto.includes('urgencia') ||
|
||||||
valor.includes('urgente')
|
todoElTexto.includes('urgente')
|
||||||
) {
|
) {
|
||||||
esUrgente = true;
|
esUrgente = true;
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key.toLowerCase().includes('urgent') && (valor === 'sí' || valor === 'si' || valor === 'true')) {
|
|
||||||
esUrgente = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 🕵️ 4. EXTRAER GREMIO (Ya viene en el objeto enviado)
|
// 🕵️ 4. EXTRAER GREMIO (Ya viene en el objeto enviado)
|
||||||
const guildId = svc.guild_id || svc['guild_id'];
|
const guildId = svc.guild_id || svc['guild_id'] || (svc.raw_data && svc.raw_data.guild_id);
|
||||||
|
|
||||||
// 5. Guardamos en la Base de Datos
|
// 5. Guardamos en la Base de Datos
|
||||||
// Usamos RETURNING para saber el ID generado y el estado de automatización actual
|
|
||||||
const insertRes = await pool.query(`
|
const insertRes = await pool.query(`
|
||||||
INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, is_urgent)
|
INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, is_urgent)
|
||||||
VALUES ($1, $2, $3, $4, $5)
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
@@ -1878,22 +1870,18 @@ app.post("/providers/scraped", authMiddleware, async (req, res) => {
|
|||||||
const autoStatus = insertRes.rows[0].automation_status;
|
const autoStatus = insertRes.rows[0].automation_status;
|
||||||
|
|
||||||
// 🚀 6. ¡DISPARADOR AUTOMÁTICO DE URGENCIAS!
|
// 🚀 6. ¡DISPARADOR AUTOMÁTICO DE URGENCIAS!
|
||||||
// Solo si: Es urgente + Tiene Gremio + Botón ON + No se ha enviado ya (status manual/pending)
|
|
||||||
if (esUrgente && guildId && autoDispatchEnabled && (autoStatus === 'manual' || autoStatus === 'pending')) {
|
if (esUrgente && guildId && autoDispatchEnabled && (autoStatus === 'manual' || autoStatus === 'pending')) {
|
||||||
console.log(`⚡ [AUTO-DISPATCH] Lanzando urgencia ${ref} de ${provider} a la bolsa...`);
|
console.log(`⚡ [AUTO-DISPATCH] Lanzando urgencia ${ref} de ${provider} a la bolsa...`);
|
||||||
|
|
||||||
// Extraemos CP para afinar la búsqueda si es posible
|
const cpMatch = todoElTexto.match(/\b\d{5}\b/);
|
||||||
const cpMatch = String(svc['Código Postal'] || svc['C.P.'] || "").match(/\b\d{5}\b/);
|
|
||||||
const cpFinal = cpMatch ? cpMatch[0] : "00000";
|
const cpFinal = cpMatch ? cpMatch[0] : "00000";
|
||||||
|
|
||||||
// Llamamos a tu lógica de automatización interna
|
|
||||||
// Usamos fetch local para reutilizar la lógica de asignación aleatoria y pings
|
|
||||||
const port = process.env.PORT || 3000;
|
const port = process.env.PORT || 3000;
|
||||||
fetch(`http://127.0.0.1:${port}/providers/automate/${newSvcId}`, {
|
fetch(`http://127.0.0.1:${port}/providers/automate/${newSvcId}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': req.headers.authorization // Reutilizamos el token del scraper
|
'Authorization': req.headers.authorization
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
guild_id: guildId,
|
guild_id: guildId,
|
||||||
|
|||||||
Reference in New Issue
Block a user