Buzón de Automatizaciones

Validación rápida y profesional de expedientes.

// CORRECCIÓN EN EL RENDERIZADO DEL LISTADO // Asegúrate de que en loadInbox() la imagen tenga un onError para evitar el cuadro blanco // card.innerHTML = `... { if (!localStorage.getItem("token")) window.location.href = "index.html"; setTimeout(async () => { await loadGuilds(); await loadInbox(); }, 200); }); async function loadGuilds() { const res = await fetch(`${API_URL}/guilds`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }); const data = await res.json(); allGuilds = data.guilds || []; const sel = document.getElementById('impGuild'); sel.innerHTML = ''; allGuilds.forEach(g => sel.innerHTML += ``); } async function loadOpsForGuild(guildId) { const sel = document.getElementById('impOperator'); if(!guildId) { sel.innerHTML = ''; return; } const res = await fetch(`${API_URL}/operators?guild_id=${guildId}`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }); const data = await res.json(); sel.innerHTML = ''; data.operators.forEach(op => sel.innerHTML += ``); } async function loadInbox() { const container = document.getElementById('inboxContainer'); try { const [resSvc, resMap] = await Promise.all([ fetch(`${API_URL}/providers/scraped`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }), fetch(`${API_URL}/discovery/mappings`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }) ]); const dataSvc = await resSvc.json(); scrapedData = dataSvc.services || []; container.innerHTML = ""; if(scrapedData.length === 0) { container.innerHTML = '
No hay expedientes pendientes.
'; return; } scrapedData.forEach(svc => { const raw = svc.raw_data || {}; const name = raw['Nombre Cliente'] || raw['CLIENTE'] || "S/N"; const addr = raw['Dirección'] || raw['DOMICILIO'] || ""; const pop = raw['Población'] || raw['POBLACION-PROVINCIA'] || ""; const fullAddr = `${addr} ${pop}`.trim(); const companyName = raw['Compañía'] || raw['COMPAÑIA'] || ""; const logoUrl = getLogoUrl(companyName); // const phone = (raw['Teléfono'] || raw['TELEFONOS'] || raw['TELEFONO'] || "").match(/[6789]\d{8}/)?.[0] || ""; const isBlocked = (!name || name.toUpperCase().includes('SIN NOMBRE') || !addr); const card = document.createElement('div'); card.className = `bg-white p-5 rounded-2xl border ${isBlocked ? 'opacity-50' : 'shadow-sm hover:border-blue-300'} flex items-center justify-between transition-all group fade-in`; card.innerHTML = `

${isBlocked ? 'Bloqueado' : name}

${svc.provider === 'multiasistencia' ? 'MULTI' : 'HOME'}

${isBlocked ? 'Pendiente de liberación' : fullAddr}

${!isBlocked ? ` ` : ''}
`; container.appendChild(card); }); lucide.createIcons(); } catch (e) { showToast("Error de conexión", true); } } function openEditor(id) { const svc = scrapedData.find(s => s.id === id); if(!svc) return; const raw = svc.raw_data; const companyName = raw['Compañía'] || raw['COMPAÑIA'] || ""; const logoUrl = getLogoUrl(companyName); // // ACTUALIZACIÓN DE CABECERA document.getElementById('displayRef').innerText = `REF: ${svc.service_ref}`; document.getElementById('displayCompany').innerText = companyName || svc.provider.toUpperCase(); document.getElementById('modalCompanyLogo').innerHTML = ``; // RELLENAR FORMULARIO document.getElementById('impScrapedId').value = id; document.getElementById('impName').value = raw['Nombre Cliente'] || raw['CLIENTE'] || ""; const rawPhone = raw['Teléfono'] || raw['TELEFONOS'] || raw['TELEFONO'] || ""; document.getElementById('impPhone').value = rawPhone.match(/[6789]\d{8}/)?.[0] || ""; const addr = raw['Dirección'] || raw['DOMICILIO'] || ""; const pop = raw['Población'] || raw['POBLACION-PROVINCIA'] || ""; document.getElementById('impAddress').value = `${addr} ${pop}`.trim(); document.getElementById('impCP').value = raw['Código Postal'] || ""; document.getElementById('impDesc').value = raw['Descripción'] || ""; document.getElementById('impUrgent').value = (raw['Urgente'] === 'Sí' || raw['Urgente'] === 'true').toString(); document.getElementById('impNotesInt').value = ""; document.getElementById('impNotesExt').value = ""; document.getElementById('impGuild').value = ""; document.getElementById('importModal').classList.remove('hidden'); lucide.createIcons(); } function closeModal() { document.getElementById('importModal').classList.add('hidden'); } async function handleFinalImport(event) { event.preventDefault(); const btn = event.submitter; btn.disabled = true; const payload = { name: document.getElementById('impName').value, phone: document.getElementById('impPhone').value, address: document.getElementById('impAddress').value, cp: document.getElementById('impCP').value, description: document.getElementById('impDesc').value, company_ref: document.getElementById('displayRef').innerText.replace('REF: ', ''), guild_id: document.getElementById('impGuild').value, assigned_to: document.getElementById('impOperator').value || null, internal_notes: document.getElementById('impNotesInt').value, client_notes: document.getElementById('impNotesExt').value, is_urgent: document.getElementById('impUrgent').value === 'true' }; try { const res = await fetch(`${API_URL}/providers/import/${document.getElementById('impScrapedId').value}`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem("token")}` }, body: JSON.stringify(payload) }); const result = await res.json(); if(result.ok) { showToast("✅ Expediente traspasado con éxito"); closeModal(); loadInbox(); } else { showToast("❌ Error al importar", true); } } catch (e) { showToast("❌ Error de red", true); } finally { btn.disabled = false; } } function showToast(msg, isError = false) { const t = document.getElementById('toast'), m = document.getElementById('toastMsg'); t.className = `fixed bottom-8 right-8 px-8 py-4 rounded-2xl shadow-2xl z-[200] flex items-center gap-3 transition-all ${isError ? 'bg-red-600' : 'bg-slate-900'} text-white`; m.innerText = msg; t.classList.remove('hidden'); setTimeout(() => t.classList.add('hidden'), 4000); }