diff --git a/frontend/js/intenciones.js b/frontend/js/intenciones.js index d5051dc..8a8a3fc 100644 --- a/frontend/js/intenciones.js +++ b/frontend/js/intenciones.js @@ -16,6 +16,8 @@ function init() { usuario = verificarAuth(); if (!usuario) return; // verificarAuth ya redirige a login si no hay sesión + migrarDifuntosAnonimos(); + configurarPestanas(); configurarSelectorGrupo(); cargarIntenciones(); @@ -228,6 +230,33 @@ function keyDifuntos() { return `difuntos_personales_${usuario ? usuario.id : 'anonimo'}`; } +// Migra los difuntos guardados anónimamente al usuario actual via API +async function migrarDifuntosAnonimos() { + const claveAnonima = 'difuntos_personales_anonimo'; + const anonimos = JSON.parse(localStorage.getItem(claveAnonima) || '[]'); + if (anonimos.length === 0) return; + + let migrados = 0; + for (const d of anonimos) { + try { + const res = await apiCall('/difuntos/personales', { + method: 'POST', + body: JSON.stringify({ + nombre: d.nombre, + nacimiento: d.nacimiento || null, + defuncion: d.defuncion || null + }) + }); + if (res && res.ok) migrados++; + } catch (e) { /* si falla uno, continuar con el siguiente */ } + } + + if (migrados > 0) { + localStorage.removeItem(claveAnonima); + _difuntosCache = null; + } +} + async function agregarDifunto() { const nombre = document.getElementById("difunto-nombre").value.trim(); if (!nombre) return;