Actualizar configuracion.html

This commit is contained in:
2026-03-08 21:25:31 +00:00
parent fe595aea66
commit 3c7f5075fa

View File

@@ -730,7 +730,7 @@
loadWaSettings(); loadWaSettings();
}); });
// ========================================== // ==========================================
// LÓGICA DE NAVEGACIÓN Y TABS // LÓGICA DE NAVEGACIÓN Y TABS
// ========================================== // ==========================================
function showTab(tabId) { function showTab(tabId) {
@@ -754,6 +754,7 @@
if(tabId === 'providers') { if(tabId === 'providers') {
loadProviderCredentials(); loadProviderCredentials();
loadIaRules(); loadIaRules();
loadRobotConfig(); // 👇 ¡AQUÍ ESTABA EL FALLO! Faltaba llamar a esta función
} }
} }
@@ -1243,31 +1244,36 @@
} }
async function saveRobotConfig() { async function saveRobotConfig() {
const resGet = await fetch(`${API_URL}/whatsapp/settings`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }); // Ponemos el botón en modo "Cargando..."
const dataGet = await resGet.json(); const btn = document.querySelector('button[onclick="saveRobotConfig()"]');
const currentSettings = dataGet.settings || {}; const originalHtml = btn.innerHTML;
btn.innerHTML = '<i data-lucide="loader-2" class="w-4 h-4 animate-spin"></i> Guardando...';
lucide.createIcons();
currentSettings.robot_homeserve = { // Construimos SOLO el objeto del robot (el servidor ya se encarga de no borrar el de WhatsApp)
assign: { const payload = {
enabled: document.getElementById('hs_cfg_assign_enabled').checked, robot_homeserve: {
status: document.getElementById('hs_cfg_assign_status').value, assign: {
days_next: parseInt(document.getElementById('hs_cfg_assign_days').value) || 0, enabled: document.getElementById('hs_cfg_assign_enabled').checked,
check_inform: document.getElementById('hs_cfg_assign_inform').checked, status: document.getElementById('hs_cfg_assign_status').value,
obs: document.getElementById('hs_cfg_assign_obs').value days_next: parseInt(document.getElementById('hs_cfg_assign_days').value) || 0,
}, check_inform: document.getElementById('hs_cfg_assign_inform').checked,
date: { obs: document.getElementById('hs_cfg_assign_obs').value
enabled: document.getElementById('hs_cfg_date_enabled').checked, },
status: document.getElementById('hs_cfg_date_status').value, date: {
days_next: parseInt(document.getElementById('hs_cfg_date_days').value) || 0, enabled: document.getElementById('hs_cfg_date_enabled').checked,
check_inform: document.getElementById('hs_cfg_date_inform').checked, status: document.getElementById('hs_cfg_date_status').value,
obs: document.getElementById('hs_cfg_date_obs').value days_next: parseInt(document.getElementById('hs_cfg_date_days').value) || 0,
}, check_inform: document.getElementById('hs_cfg_date_inform').checked,
notfound: { obs: document.getElementById('hs_cfg_date_obs').value
enabled: document.getElementById('hs_cfg_notfound_enabled').checked, },
status: document.getElementById('hs_cfg_notfound_status').value, notfound: {
days_next: parseInt(document.getElementById('hs_cfg_notfound_days').value) || 0, enabled: document.getElementById('hs_cfg_notfound_enabled').checked,
check_inform: document.getElementById('hs_cfg_notfound_inform').checked, status: document.getElementById('hs_cfg_notfound_status').value,
obs: document.getElementById('hs_cfg_notfound_obs').value days_next: parseInt(document.getElementById('hs_cfg_notfound_days').value) || 0,
check_inform: document.getElementById('hs_cfg_notfound_inform').checked,
obs: document.getElementById('hs_cfg_notfound_obs').value
}
} }
}; };
@@ -1275,55 +1281,9 @@
const res = await fetch(`${API_URL}/whatsapp/settings`, { const res = await fetch(`${API_URL}/whatsapp/settings`, {
method: 'POST', method: 'POST',
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` }, headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
body: JSON.stringify(currentSettings) body: JSON.stringify(payload)
});
if (res.ok) {
showToast("✅ Reglas del Robot guardadas");
} else {
showToast("❌ Error al guardar", true);
}
} catch(e) {
showToast("❌ Error de conexión", true);
}
}
async function saveRobotConfig() {
// Obtenemos todos los settings actuales para no sobreescribir lo de WhatsApp
const resGet = await fetch(`${API_URL}/whatsapp/settings`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
const dataGet = await resGet.json();
const currentSettings = dataGet.settings || {};
// Construimos el nuevo objeto del robot
currentSettings.robot_homeserve = {
assign: {
enabled: document.getElementById('hs_cfg_assign_enabled').checked,
status: document.getElementById('hs_cfg_assign_status').value,
days_next: parseInt(document.getElementById('hs_cfg_assign_days').value) || 0,
check_inform: document.getElementById('hs_cfg_assign_inform').checked,
obs: document.getElementById('hs_cfg_assign_obs').value
},
date: {
enabled: document.getElementById('hs_cfg_date_enabled').checked,
status: document.getElementById('hs_cfg_date_status').value,
days_next: parseInt(document.getElementById('hs_cfg_date_days').value) || 0,
check_inform: document.getElementById('hs_cfg_date_inform').checked,
obs: document.getElementById('hs_cfg_date_obs').value
},
notfound: {
enabled: document.getElementById('hs_cfg_notfound_enabled').checked,
status: document.getElementById('hs_cfg_notfound_status').value,
days_next: parseInt(document.getElementById('hs_cfg_notfound_days').value) || 0,
check_inform: document.getElementById('hs_cfg_notfound_inform').checked,
obs: document.getElementById('hs_cfg_notfound_obs').value
}
};
try {
const res = await fetch(`${API_URL}/whatsapp/settings`, {
method: 'POST',
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
body: JSON.stringify(currentSettings)
}); });
if (res.ok) { if (res.ok) {
showToast("✅ Reglas del Robot guardadas"); showToast("✅ Reglas del Robot guardadas");
} else { } else {
@@ -1331,6 +1291,10 @@
} }
} catch(e) { } catch(e) {
showToast("❌ Error de conexión", true); showToast("❌ Error de conexión", true);
} finally {
// Restauramos el botón
btn.innerHTML = originalHtml;
lucide.createIcons();
} }
} }