Actualizar automatizaciones.html

This commit is contained in:
2026-02-15 21:13:04 +00:00
parent 747cfda943
commit 57e49dcc41

View File

@@ -38,14 +38,14 @@
</div> </div>
<div class="mt-12"> <div class="mt-12">
<h3 class="font-bold text-gray-400 uppercase text-xs tracking-widest mb-4">Automatismos Fallidos (Sin respuesta)</h3> <h3 class="font-bold text-gray-400 uppercase text-xs tracking-widest mb-4 text-left">Automatismos Fallidos (Sin respuesta)</h3>
<div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden"> <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
<table class="w-full text-left border-collapse"> <table class="w-full text-left border-collapse">
<thead> <thead>
<tr class="bg-gray-50 text-[10px] font-black uppercase text-gray-400 border-b"> <tr class="bg-gray-50 text-[10px] font-black uppercase text-gray-400 border-b">
<th class="p-4">Expediente</th> <th class="p-4">Expediente / Cliente</th>
<th class="p-4">Zona / Gremio</th> <th class="p-4">Dirección y Contacto</th>
<th class="p-4">Motivo</th> <th class="p-4">Operarios que fallaron</th>
<th class="p-4 text-right">Acción</th> <th class="p-4 text-right">Acción</th>
</tr> </tr>
</thead> </thead>
@@ -61,13 +61,12 @@
<script src="js/layout.js"></script> <script src="js/layout.js"></script>
<script> <script>
//const API_URL = "https://integrarepara-api.integrarepara.es";
let activeIntervals = []; let activeIntervals = [];
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
if (!localStorage.getItem("token")) { window.location.href = "index.html"; return; } if (!localStorage.getItem("token")) { window.location.href = "index.html"; return; }
loadAutomations(); loadAutomations();
setInterval(loadAutomations, 20000); // Recarga datos del servidor cada 20s setInterval(loadAutomations, 20000);
}); });
async function loadAutomations() { async function loadAutomations() {
@@ -77,7 +76,6 @@
}); });
const data = await res.json(); const data = await res.json();
if (data.ok) { if (data.ok) {
// Limpiamos intervalos antiguos para no saturar memoria
activeIntervals.forEach(clearInterval); activeIntervals.forEach(clearInterval);
activeIntervals = []; activeIntervals = [];
@@ -101,54 +99,41 @@
container.innerHTML = activeServices.map(s => { container.innerHTML = activeServices.map(s => {
const raw = s.raw_data; const raw = s.raw_data;
const cardId = `card-${s.id}`; const cardId = `card-${s.id}`;
return ` return `
<div id="${cardId}" class="bg-white rounded-2xl border-2 border-blue-50 shadow-sm overflow-hidden flex flex-col fade-in"> <div id="${cardId}" class="bg-white rounded-2xl border-2 border-blue-50 shadow-sm overflow-hidden flex flex-col fade-in">
<div class="p-5 border-b border-gray-50 bg-blue-50/30 text-left"> <div class="p-5 border-b border-gray-50 bg-blue-50/30 text-left">
<div class="flex justify-between items-start mb-2"> <div class="flex justify-between items-start mb-2 text-left">
<span class="text-[10px] font-black bg-blue-600 text-white px-2 py-0.5 rounded uppercase">${s.provider}</span> <span class="text-[10px] font-black bg-blue-600 text-white px-2 py-0.5 rounded uppercase text-left">${s.provider}</span>
<span class="text-xs font-black text-blue-600">Ref: ${s.service_ref}</span> <span class="text-xs font-black text-blue-600 text-left">Ref: ${s.service_ref}</span>
</div> </div>
<h4 class="font-black text-slate-800 uppercase truncate">${raw["Nombre Cliente"] || raw["CLIENTE"] || 'Sin nombre'}</h4> <h4 class="font-black text-slate-800 uppercase truncate text-left">${raw["Nombre Cliente"] || raw["CLIENTE"] || 'Sin nombre'}</h4>
<p class="text-[10px] text-gray-500 font-bold uppercase tracking-tighter">📍 CP: ${raw["Código Postal"] || raw["C.P."] || '---'}</p> <p class="text-[10px] text-gray-500 font-bold uppercase tracking-tighter text-left">📍 CP: ${raw["Código Postal"] || raw["C.P."] || '---'}</p>
</div> </div>
<div class="p-5 flex-1 space-y-4 text-left"> <div class="p-5 flex-1 space-y-4 text-left">
<div class="flex items-center gap-3"> <div class="flex items-center gap-3 text-left">
<div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center text-green-600"> <div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center text-green-600 text-left"><i data-lucide="user"></i></div>
<i data-lucide="user"></i> <div class="text-left">
</div> <p class="text-[10px] text-gray-400 font-bold uppercase leading-none text-left">Turno actual:</p>
<div> <p class="font-black text-slate-700 uppercase text-left">${s.current_worker_name || 'Buscando...'}</p>
<p class="text-[10px] text-gray-400 font-bold uppercase leading-none">Turno actual:</p>
<p class="font-black text-slate-700 uppercase">${s.current_worker_name || 'Buscando...'}</p>
</div> </div>
</div> </div>
<div class="space-y-1 text-left">
<div class="space-y-1"> <div class="flex justify-between text-[10px] font-bold uppercase tracking-widest text-blue-500 text-left">
<div class="flex justify-between text-[10px] font-bold uppercase tracking-widest text-blue-500">
<span>Caduca en</span> <span>Caduca en</span>
<span id="timer-${s.id}">--:--</span> <span id="timer-${s.id}">--:--</span>
</div> </div>
<div class="w-full bg-gray-100 h-1.5 rounded-full overflow-hidden"> <div class="w-full bg-gray-100 h-1.5 rounded-full overflow-hidden text-left">
<div id="progress-${s.id}" class="bg-blue-500 h-full progress-bar" style="width: 100%"></div> <div id="progress-${s.id}" class="bg-blue-500 h-full progress-bar text-left" style="width: 100%"></div>
</div> </div>
</div> </div>
</div> </div>
<div class="p-3 bg-gray-50 flex gap-2 text-left">
<div class="p-3 bg-gray-50 flex gap-2"> <button onclick="stopAutomation(${s.id})" class="flex-1 bg-white border border-red-200 text-red-500 py-2 rounded-lg text-[10px] font-black uppercase hover:bg-red-50 transition-colors text-left flex justify-center">Abortar Rueda</button>
<button onclick="stopAutomation(${s.id})" class="flex-1 bg-white border border-red-200 text-red-500 py-2 rounded-lg text-[10px] font-black uppercase hover:bg-red-50 transition-colors">
Abortar Rueda
</button>
</div> </div>
</div> </div>`;
`;
}).join(''); }).join('');
// Iniciar contadores dinámicos activeServices.forEach(s => { startLocalTimer(s.id, s.token_expires_at); });
activeServices.forEach(s => {
startLocalTimer(s.id, s.token_expires_at);
});
lucide.createIcons(); lucide.createIcons();
} }
@@ -156,26 +141,20 @@
if(!expiryDate) return; if(!expiryDate) return;
const timerEl = document.getElementById(`timer-${id}`); const timerEl = document.getElementById(`timer-${id}`);
const progressEl = document.getElementById(`progress-${id}`); const progressEl = document.getElementById(`progress-${id}`);
const update = () => { const update = () => {
const now = new Date().getTime(); const now = new Date().getTime();
const distance = new Date(expiryDate).getTime() - now; const distance = new Date(expiryDate).getTime() - now;
if (distance < 0) { if (distance < 0) {
timerEl.innerText = "EXPIRADO"; timerEl.innerText = "EXPIRADO";
progressEl.style.width = "0%"; progressEl.style.width = "0%";
return; return;
} }
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000); const seconds = Math.floor((distance % (1000 * 60)) / 1000);
timerEl.innerText = `${minutes}m ${seconds}s`; timerEl.innerText = `${minutes}m ${seconds}s`;
// Asumimos turnos de 5 minutos (300 segundos) para el porcentaje
const percent = (distance / (5 * 60 * 1000)) * 100; const percent = (distance / (5 * 60 * 1000)) * 100;
progressEl.style.width = `${percent}%`; progressEl.style.width = `${percent}%`;
}; };
update(); update();
const interval = setInterval(update, 1000); const interval = setInterval(update, 1000);
activeIntervals.push(interval); activeIntervals.push(interval);
@@ -183,17 +162,49 @@
function renderFailedTable(failedServices) { function renderFailedTable(failedServices) {
const tbody = document.getElementById('failed-list'); const tbody = document.getElementById('failed-list');
if (failedServices.length === 0) {
tbody.innerHTML = `<tr><td colspan="4" class="p-8 text-center text-gray-400 italic">No hay expedientes fallidos ahora.</td></tr>`;
return;
}
tbody.innerHTML = failedServices.map(s => { tbody.innerHTML = failedServices.map(s => {
const raw = s.raw_data || {};
const name = raw["Nombre Cliente"] || raw["CLIENTE"] || "Asegurado Desconocido";
const phone = (raw["Teléfono"] || raw["TELEFONO"] || "").match(/[6789]\d{8}/)?.[0] || "";
const address = raw["Dirección"] || raw["DOMICILIO"] || "Sin dirección";
const population = raw["Población"] || raw["POBLACION-PROVINCIA"] || "";
const intentos = s.attempted_workers ? s.attempted_workers.join(', ') : "Sin registros";
return ` return `
<tr class="hover:bg-red-50/30 transition text-left"> <tr class="border-b border-gray-50 hover:bg-slate-50 transition-all text-left">
<td class="p-4 font-bold text-slate-700">${s.service_ref}</td> <td class="p-4 text-left">
<td class="p-4 text-xs font-medium text-gray-500">CP: ${s.raw_data["Código Postal"] || '---'}</td> <div class="flex flex-col text-left">
<td class="p-4"><span class="text-[10px] font-black text-red-500 uppercase bg-red-100 px-2 py-0.5 rounded">Nadie respondió</span></td> <span class="font-black text-slate-700 text-left">#${s.service_ref}</span>
<td class="p-4 text-right"> <span class="text-[10px] text-blue-600 font-bold uppercase text-left">${name}</span>
<button onclick="window.location.href='validar.html'" class="text-blue-600 font-black text-[10px] uppercase hover:underline">Asignar Manual</button> </div>
</td>
<td class="p-4 text-left">
<div class="flex flex-col text-left">
<span class="text-xs font-bold text-slate-600 text-left">${address}</span>
<span class="text-[10px] text-gray-400 font-medium text-left">${population} ${raw["Código Postal"] ? '| CP: '+raw["Código Postal"] : ''}</span>
</div>
</td>
<td class="p-4 text-left">
<div class="bg-red-50 p-2 rounded-lg border border-red-100 text-left max-w-xs">
<p class="text-[9px] font-black text-red-500 uppercase leading-none mb-1 text-left">No contestaron:</p>
<p class="text-[10px] text-red-700 font-bold italic truncate text-left">${intentos}</p>
</div>
</td>
<td class="p-4 text-right text-left">
<div class="flex items-center justify-end gap-2 text-left">
${phone ? `<a href="tel:${phone}" class="bg-emerald-500 text-white p-2 rounded-lg hover:bg-emerald-600 shadow-sm text-left"><i data-lucide="phone" class="w-4 h-4"></i></a>` : ''}
<button onclick="window.location.href='validar.html'" class="bg-blue-600 text-white px-3 py-2 rounded-lg font-black text-[10px] uppercase shadow-md active:scale-95 text-left">Asignar a Mano</button>
</div>
</td> </td>
</tr>`; </tr>`;
}).join(''); }).join('');
lucide.createIcons();
} }
async function stopAutomation(id) { async function stopAutomation(id) {
@@ -201,22 +212,16 @@
try { try {
const res = await fetch(`${API_URL}/providers/scraped/${id}`, { const res = await fetch(`${API_URL}/providers/scraped/${id}`, {
method: 'PUT', method: 'PUT',
headers: { headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
"Content-Type": "application/json",
"Authorization": `Bearer ${localStorage.getItem("token")}`
},
body: JSON.stringify({ automation_status: 'manual' }) body: JSON.stringify({ automation_status: 'manual' })
}); });
if (res.ok) { if (res.ok) { showToast("Proceso detenido"); loadAutomations(); }
showToast("Proceso detenido");
loadAutomations();
}
} catch(e) { showToast("Error al detener", true); } } catch(e) { showToast("Error al detener", true); }
} }
function showToast(msg, err=false){ function showToast(msg, err=false){
const t=document.getElementById('toast'), m=document.getElementById('toastMsg'); const t=document.getElementById('toast'), m=document.getElementById('toastMsg');
t.className=`fixed bottom-5 right-5 px-6 py-3 rounded-xl shadow-2xl transition-all duration-300 z-50 flex items-center gap-3 font-bold ${err?'bg-red-600':'bg-slate-900'} text-white`; t.className=`fixed bottom-5 right-5 px-6 py-3 rounded-xl shadow-2xl transition-all duration-300 z-50 flex items-center gap-3 font-bold ${err?'bg-red-600':'bg-slate-900'} text-white text-left`;
m.innerText=msg; t.classList.remove('translate-y-20','opacity-0'); m.innerText=msg; t.classList.remove('translate-y-20','opacity-0');
setTimeout(()=>t.classList.add('translate-y-20','opacity-0'),3000); setTimeout(()=>t.classList.add('translate-y-20','opacity-0'),3000);
} }