Actualizar automatizaciones.html
This commit is contained in:
@@ -6,230 +6,217 @@
|
|||||||
<title>Automatizaciones - Integra Repara</title>
|
<title>Automatizaciones - Integra Repara</title>
|
||||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
||||||
<link rel="stylesheet" href="./css/style.css"> <style>
|
<style>
|
||||||
body { background-color: #f8fafc; }
|
/* Estilos para integrarse con tu layout.js */
|
||||||
.main-wrapper { display: flex; min-height: 100vh; }
|
body { background-color: #f8fafc; margin: 0; }
|
||||||
.content-area { flex-grow: 1; padding: 2rem; margin-top: 60px; } /* Ajuste por barra superior */
|
#main-content { margin-left: 260px; padding: 2rem; margin-top: 60px; min-height: calc(100vh - 60px); transition: 0.3s; }
|
||||||
|
|
||||||
.service-card { border: none; border-radius: 12px; background: white; border-left: 5px solid #cbd5e1; transition: 0.2s; }
|
.service-card { border: none; border-radius: 12px; background: white; border-left: 5px solid #22c55e; transition: 0.2s; margin-bottom: 1rem; }
|
||||||
.service-card:hover { box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1); }
|
.service-card:hover { box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1); }
|
||||||
.service-card.ready { border-left-color: #22c55e; }
|
|
||||||
|
|
||||||
.badge-provider { font-weight: 800; font-size: 0.7rem; padding: 4px 10px; border-radius: 20px; text-transform: uppercase; }
|
.badge-provider { font-weight: 800; font-size: 0.7rem; padding: 4px 10px; border-radius: 20px; text-transform: uppercase; }
|
||||||
.multiasistencia { background: #dbeafe; color: #1e40af; }
|
.multiasistencia { background: #dbeafe; color: #1e40af; }
|
||||||
.homeserve { background: #fee2e2; color: #991b1b; }
|
.homeserve { background: #fee2e2; color: #991b1b; }
|
||||||
|
|
||||||
.preview-box { background: #f1f5f9; border-radius: 8px; padding: 10px; font-size: 0.85rem; color: #475569; }
|
@media (max-width: 992px) { #main-content { margin-left: 0; } }
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<div id="layout-header"></div>
|
<div id="layout-header"></div>
|
||||||
<div class="main-wrapper">
|
|
||||||
<div id="layout-sidebar"></div>
|
<div id="layout-sidebar"></div>
|
||||||
|
|
||||||
<main class="content-area">
|
<div id="main-content">
|
||||||
<div class="container-fluid">
|
<div class="container-fluid">
|
||||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||||
<div>
|
<div>
|
||||||
<h2 class="fw-bold mb-0">🤖 Buzón de Automatizaciones</h2>
|
<h2 class="fw-bold mb-0">🤖 Buzón de Automatizaciones</h2>
|
||||||
<p class="text-muted">Revisa, edita e importa los servicios detectados por los robots.</p>
|
<p class="text-muted">Revisa, edita e importa los servicios de Multiasistencia y HomeServe.</p>
|
||||||
</div>
|
</div>
|
||||||
<button onclick="loadInbox()" class="btn btn-primary rounded-pill px-4">
|
<button onclick="loadInbox()" class="btn btn-primary rounded-pill px-4 shadow-sm">
|
||||||
<i class="fas fa-sync-alt me-2"></i>Actualizar Buzón
|
<i class="fas fa-sync-alt me-2"></i>Actualizar Buzón
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="inboxContainer" class="row g-3">
|
<div id="inboxContainer">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="modal fade" id="importModal" tabindex="-1">
|
<div class="modal fade" id="editImportModal" tabindex="-1">
|
||||||
<div class="modal-dialog modal-lg">
|
<div class="modal-dialog modal-lg">
|
||||||
<div class="modal-content">
|
<div class="modal-content border-0 shadow-lg">
|
||||||
<div class="modal-header bg-light">
|
<div class="modal-header bg-primary text-white">
|
||||||
<h5 class="modal-title fw-bold">Confirmar Importación de Servicio</h5>
|
<h5 class="modal-title fw-bold"><i class="fas fa-edit me-2"></i>Revisar e Importar Servicio</h5>
|
||||||
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body p-4">
|
||||||
<form id="importForm" class="row g-3">
|
<form id="importForm" class="row g-3">
|
||||||
<input type="hidden" id="scrapedId">
|
<input type="hidden" id="scrapedId">
|
||||||
|
|
||||||
<div class="col-md-6">
|
<div class="col-md-7">
|
||||||
<label class="form-label small fw-bold">Nombre del Cliente</label>
|
<label class="form-label small fw-bold text-secondary">NOMBRE DEL CLIENTE</label>
|
||||||
<input type="text" id="imp_name" class="form-control" required>
|
<input type="text" id="imp_name" class="form-control form-control-lg border-2">
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-6">
|
<div class="col-md-5">
|
||||||
<label class="form-label small fw-bold">Teléfono</label>
|
<label class="form-label small fw-bold text-secondary">TELÉFONO DE CONTACTO</label>
|
||||||
<input type="text" id="imp_phone" class="form-control" required>
|
<input type="text" id="imp_phone" class="form-control form-control-lg border-2">
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-12">
|
<div class="col-md-12">
|
||||||
<label class="form-label small fw-bold">Dirección Completa</label>
|
<label class="form-label small fw-bold text-secondary">DIRECCIÓN DE LA REPARACIÓN</label>
|
||||||
<input type="text" id="imp_address" class="form-control" required>
|
<div class="input-group">
|
||||||
</div>
|
<span class="input-group-text bg-white border-2"><i class="fas fa-map-marker-alt text-danger"></i></span>
|
||||||
<div class="col-md-12">
|
<input type="text" id="imp_address" class="form-control form-control-lg border-2">
|
||||||
<label class="form-label small fw-bold">Descripción del Trabajo (Detectada)</label>
|
</div>
|
||||||
<textarea id="imp_description" class="form-control" rows="4"></textarea>
|
</div>
|
||||||
</div>
|
<div class="col-md-12">
|
||||||
<div class="col-md-4">
|
<label class="form-label small fw-bold text-secondary">DESCRIPCIÓN DE TRABAJOS</label>
|
||||||
<label class="form-label small fw-bold">Expediente Ref.</label>
|
<textarea id="imp_description" class="form-control border-2" rows="5" placeholder="Detalle de la avería detectada..."></textarea>
|
||||||
<input type="text" id="imp_ref" class="form-control" readonly>
|
</div>
|
||||||
</div>
|
<div class="col-md-4">
|
||||||
<div class="col-md-4">
|
<label class="form-label small fw-bold text-secondary">REFERENCIA</label>
|
||||||
<label class="form-label small fw-bold">Compañía</label>
|
<input type="text" id="imp_ref" class="form-control border-2 bg-light" readonly>
|
||||||
<input type="text" id="imp_company" class="form-control">
|
</div>
|
||||||
</div>
|
<div class="col-md-4">
|
||||||
<div class="col-md-4">
|
<label class="form-label small fw-bold text-secondary">COMPAÑÍA</label>
|
||||||
<label class="form-label small fw-bold">Urgente</label>
|
<input type="text" id="imp_company" class="form-control border-2 bg-light">
|
||||||
<select id="imp_urgent" class="form-select">
|
</div>
|
||||||
<option value="false">No</option>
|
<div class="col-md-4">
|
||||||
<option value="true">Sí</option>
|
<label class="form-label small fw-bold text-secondary">URGENCIA</label>
|
||||||
</select>
|
<select id="imp_urgent" class="form-select border-2">
|
||||||
</div>
|
<option value="false">Normal</option>
|
||||||
</form>
|
<option value="true">⚠️ URGENTE</option>
|
||||||
</div>
|
</select>
|
||||||
<div class="modal-footer">
|
</div>
|
||||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
|
</form>
|
||||||
<button onclick="confirmImport()" class="btn btn-success px-4 fw-bold">
|
</div>
|
||||||
<i class="fas fa-file-import me-2"></i>GUARDAR EN CRM
|
<div class="modal-footer bg-light border-0">
|
||||||
</button>
|
<button type="button" class="btn btn-link text-secondary" data-bs-dismiss="modal">Descartar</button>
|
||||||
|
<button onclick="saveToCRM()" class="btn btn-success btn-lg px-5 fw-bold rounded-pill">
|
||||||
|
<i class="fas fa-cloud-upload-alt me-2"></i>GUARDAR EN CRM
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<script src="./js/layout.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
<script src="./js/layout.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
let currentMappings = [];
|
let mappings = [];
|
||||||
|
|
||||||
async function loadInbox() {
|
async function loadInbox() {
|
||||||
const container = document.getElementById('inboxContainer');
|
const container = document.getElementById('inboxContainer');
|
||||||
container.innerHTML = '<div class="text-center py-5"><div class="spinner-border text-primary"></div></div>';
|
container.innerHTML = '<div class="text-center py-5"><div class="spinner-border text-primary"></div></div>';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const [svcRes, mapRes] = await Promise.all([
|
const [svcRes, mapRes] = await Promise.all([
|
||||||
fetch(`${API_URL}/providers/scraped`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` }}),
|
fetch(`${API_URL}/providers/scraped`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` }}),
|
||||||
fetch(`${API_URL}/discovery/mappings`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` }})
|
fetch(`${API_URL}/discovery/mappings`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` }})
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const services = await svcRes.json();
|
const svcData = await svcRes.json();
|
||||||
currentMappings = await mapRes.json();
|
mappings = await mapRes.json();
|
||||||
|
|
||||||
if (!services.services || services.services.length === 0) {
|
if (!svcData.services || svcData.services.length === 0) {
|
||||||
container.innerHTML = '<div class="col-12 text-center py-5 text-muted">Buzón vacío. Todo al día.</div>';
|
container.innerHTML = '<div class="alert alert-light text-center py-5 border-dashed">No hay servicios nuevos pendientes.</div>';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
container.innerHTML = '';
|
container.innerHTML = '';
|
||||||
services.services.forEach(svc => {
|
svcData.services.forEach(svc => {
|
||||||
const raw = svc.raw_data || {};
|
const raw = svc.raw_data || {};
|
||||||
// Buscamos las reglas de este proveedor
|
const myMap = mappings.filter(m => m.provider === svc.provider);
|
||||||
const myMap = currentMappings.filter(m => m.provider === svc.provider);
|
|
||||||
|
|
||||||
// Aplicamos el mapeo dinámico
|
|
||||||
const nameKey = myMap.find(m => m.target_key === 'clientName')?.original_key;
|
|
||||||
const addrKey = myMap.find(m => m.target_key === 'address')?.original_key;
|
|
||||||
const phoneKey = myMap.find(m => m.target_key === 'phone')?.original_key;
|
|
||||||
const descKey = myMap.find(m => m.target_key === 'descripcion')?.original_key;
|
|
||||||
|
|
||||||
const name = raw[nameKey] || "Sin nombre detectado";
|
// Extraer según tu mapeo configurado
|
||||||
const addr = raw[addrKey] || "Sin dirección detectada";
|
const name = raw[myMap.find(m => m.target_key === 'clientName')?.original_key] || "Sin nombre";
|
||||||
const phone = raw[phoneKey] || raw['phone_fixed'] || "S/T";
|
const addr = raw[myMap.find(m => m.target_key === 'address')?.original_key] || "Sin dirección";
|
||||||
const desc = raw[descKey] || raw['Descripción'] || "Sin descripción";
|
const phone = raw[myMap.find(m => m.target_key === 'phone')?.original_key] || raw['Teléfono'] || "S/T";
|
||||||
|
const desc = raw[myMap.find(m => m.target_key === 'descripcion')?.original_key] || raw['Descripción'] || "Sin descripción";
|
||||||
|
|
||||||
const col = document.createElement('div');
|
const card = document.createElement('div');
|
||||||
col.className = 'col-12';
|
card.className = 'service-card shadow-sm p-4 d-flex justify-content-between align-items-center';
|
||||||
col.innerHTML = `
|
card.innerHTML = `
|
||||||
<div class="service-card shadow-sm p-3 d-flex justify-content-between align-items-center ready">
|
<div class="row w-100 align-items-center">
|
||||||
<div class="flex-grow-1 row align-items-center">
|
<div class="col-md-2 border-end">
|
||||||
<div class="col-md-2">
|
|
||||||
<span class="badge-provider ${svc.provider}">${svc.provider}</span>
|
<span class="badge-provider ${svc.provider}">${svc.provider}</span>
|
||||||
<div class="small fw-bold mt-1">#${svc.service_ref}</div>
|
<div class="small fw-bold mt-2 text-muted">#${svc.service_ref}</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-3">
|
<div class="col-md-3">
|
||||||
<div class="small text-muted fw-bold">CLIENTE</div>
|
<div class="small fw-bold text-secondary">CLIENTE</div>
|
||||||
<div class="text-dark">${name}</div>
|
<div class="fw-bold">${name}</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-4">
|
<div class="col-md-4">
|
||||||
<div class="small text-muted fw-bold">UBICACIÓN</div>
|
<div class="small fw-bold text-secondary">UBICACIÓN</div>
|
||||||
<div class="text-dark text-truncate" style="max-width: 250px;">${addr}</div>
|
<div class="text-truncate" style="max-width: 250px;">${addr}</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-3">
|
<div class="col-md-2">
|
||||||
<div class="small text-muted fw-bold">DESCRIPCIÓN</div>
|
<div class="small fw-bold text-secondary">TELÉFONO</div>
|
||||||
<div class="text-muted small text-truncate" style="max-width: 200px;">${desc}</div>
|
<div>${phone}</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-1 text-end">
|
||||||
|
<button onclick="openEditor(${svc.id}, '${svc.provider}')" class="btn btn-outline-success rounded-circle shadow-sm">
|
||||||
|
<i class="fas fa-check"></i>
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="ms-3">
|
`;
|
||||||
<button onclick="openEditModal(${svc.id}, '${svc.provider}')" class="btn btn-outline-success btn-sm px-3 rounded-pill">
|
container.appendChild(card);
|
||||||
<i class="fas fa-edit me-1"></i> Revisar e Importar
|
});
|
||||||
</button>
|
} catch (e) {
|
||||||
</div>
|
container.innerHTML = '<div class="alert alert-danger">Error de sincronización con la API.</div>';
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
container.appendChild(col);
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
container.innerHTML = '<div class="alert alert-danger">Error al conectar con la API.</div>';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function openEditModal(scrapedId, provider) {
|
|
||||||
try {
|
|
||||||
const res = await fetch(`${API_URL}/providers/scraped`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` }});
|
|
||||||
const data = await res.json();
|
|
||||||
const svc = data.services.find(s => s.id === scrapedId);
|
|
||||||
const raw = svc.raw_data;
|
|
||||||
const myMap = currentMappings.filter(m => m.provider === provider);
|
|
||||||
|
|
||||||
// Rellenar formulario con mapeos
|
|
||||||
document.getElementById('scrapedId').value = scrapedId;
|
|
||||||
document.getElementById('imp_name').value = raw[myMap.find(m => m.target_key === 'clientName')?.original_key] || "";
|
|
||||||
document.getElementById('imp_phone').value = raw[myMap.find(m => m.target_key === 'phone')?.original_key] || raw['phone_fixed'] || "";
|
|
||||||
document.getElementById('imp_address').value = raw[myMap.find(m => m.target_key === 'address')?.original_key] || "";
|
|
||||||
document.getElementById('imp_ref').value = svc.service_ref;
|
|
||||||
document.getElementById('imp_company').value = raw[myMap.find(m => m.target_key === 'compania')?.original_key] || provider.toUpperCase();
|
|
||||||
document.getElementById('imp_description').value = raw[myMap.find(m => m.target_key === 'descripcion')?.original_key] || raw['Descripción'] || "";
|
|
||||||
|
|
||||||
new bootstrap.Modal(document.getElementById('importModal')).show();
|
|
||||||
} catch (e) { alert("Error al abrir detalle."); }
|
|
||||||
}
|
|
||||||
|
|
||||||
async function confirmImport() {
|
|
||||||
const payload = {
|
|
||||||
name: document.getElementById('imp_name').value,
|
|
||||||
phone: document.getElementById('imp_phone').value,
|
|
||||||
address: document.getElementById('imp_address').value,
|
|
||||||
description: document.getElementById('imp_description').value,
|
|
||||||
company_ref: document.getElementById('imp_ref').value,
|
|
||||||
is_urgent: document.getElementById('imp_urgent').value === 'true',
|
|
||||||
status_id: null // El server pondrá el por defecto
|
|
||||||
};
|
|
||||||
|
|
||||||
const id = document.getElementById('scrapedId').value;
|
|
||||||
|
|
||||||
try {
|
|
||||||
// NOTA: Usaremos una ruta que acepte edición o la estándar de import
|
|
||||||
const res = await fetch(`${API_URL}/providers/import/${id}`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': `Bearer ${localStorage.getItem('token')}`
|
|
||||||
},
|
|
||||||
body: JSON.stringify(payload)
|
|
||||||
});
|
|
||||||
|
|
||||||
if ((await res.json()).ok) {
|
|
||||||
bootstrap.Modal.getInstance(document.getElementById('importModal')).hide();
|
|
||||||
loadInbox();
|
|
||||||
alert("✅ ¡Servicio importado con éxito!");
|
|
||||||
}
|
}
|
||||||
} catch (e) { alert("Error al importar."); }
|
}
|
||||||
}
|
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', loadInbox);
|
async function openEditor(scrapedId, provider) {
|
||||||
</script>
|
try {
|
||||||
|
const res = await fetch(`${API_URL}/providers/scraped`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}` }});
|
||||||
|
const data = await res.json();
|
||||||
|
const svc = data.services.find(s => s.id === scrapedId);
|
||||||
|
const raw = svc.raw_data;
|
||||||
|
const myMap = mappings.filter(m => m.provider === provider);
|
||||||
|
|
||||||
|
// Llenar el formulario del modal
|
||||||
|
document.getElementById('scrapedId').value = scrapedId;
|
||||||
|
document.getElementById('imp_name').value = raw[myMap.find(m => m.target_key === 'clientName')?.original_key] || "";
|
||||||
|
document.getElementById('imp_phone').value = raw[myMap.find(m => m.target_key === 'phone')?.original_key] || raw['Teléfono'] || "";
|
||||||
|
document.getElementById('imp_address').value = raw[myMap.find(m => m.target_key === 'address')?.original_key] || "";
|
||||||
|
document.getElementById('imp_ref').value = svc.service_ref;
|
||||||
|
document.getElementById('imp_company').value = raw[myMap.find(m => m.target_key === 'compania')?.original_key] || provider.toUpperCase();
|
||||||
|
document.getElementById('imp_description').value = raw[myMap.find(m => m.target_key === 'descripcion')?.original_key] || raw['Descripción'] || "";
|
||||||
|
document.getElementById('imp_urgent').value = (raw['Urgente'] === 'Sí').toString();
|
||||||
|
|
||||||
|
new bootstrap.Modal(document.getElementById('editImportModal')).show();
|
||||||
|
} catch (e) { alert("No se pudo abrir el editor."); }
|
||||||
|
}
|
||||||
|
|
||||||
|
async function saveToCRM() {
|
||||||
|
const id = document.getElementById('scrapedId').value;
|
||||||
|
const payload = {
|
||||||
|
name: document.getElementById('imp_name').value,
|
||||||
|
phone: document.getElementById('imp_phone').value,
|
||||||
|
address: document.getElementById('imp_address').value,
|
||||||
|
description: document.getElementById('imp_description').value,
|
||||||
|
company_ref: document.getElementById('imp_ref').value,
|
||||||
|
is_urgent: document.getElementById('imp_urgent').value === 'true'
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API_URL}/providers/import/${id}`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` },
|
||||||
|
body: JSON.stringify(payload)
|
||||||
|
});
|
||||||
|
|
||||||
|
if ((await res.json()).ok) {
|
||||||
|
bootstrap.Modal.getInstance(document.getElementById('editImportModal')).hide();
|
||||||
|
loadInbox();
|
||||||
|
alert("✅ ¡Servicio creado correctamente en el CRM!");
|
||||||
|
}
|
||||||
|
} catch (e) { alert("Error al importar al CRM."); }
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', loadInbox);
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user