From d8a6581fe546f4fcc7420b7869b9d11651c7cebb Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Tue, 16 Jun 2026 11:20:22 -0500 Subject: [PATCH] fix: load desk consents automatically when a turno is opened - cargarConsentimientosDesk() called from abrirFicha using DESK_ID - First checks existing DB records (get_consentimientos), falls back to desk config - abrirFormularioLugar shows proper errors if no patient or no turno - Starts polling on open to detect patient signature in real time - Fix creado_at column error in create_consent_token INSERT Co-Authored-By: Claude Haiku 4.5 --- modules/turnero/api/create_consent_token.php | 4 +- modules/turnero/views/recepcion.php | 72 ++++++++++++++++---- 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/modules/turnero/api/create_consent_token.php b/modules/turnero/api/create_consent_token.php index 75d40d5..fa44bd4 100644 --- a/modules/turnero/api/create_consent_token.php +++ b/modules/turnero/api/create_consent_token.php @@ -60,8 +60,8 @@ if ($existente) { ); $stmt = $pdo->prepare( - "INSERT INTO turnero_consentimientos (turno_id, formulario_id, token, estado, creado_at) - VALUES (?, ?, ?, 'pendiente', NOW())" + "INSERT INTO turnero_consentimientos (turno_id, formulario_id, token, estado) + VALUES (?, ?, ?, 'pendiente')" ); $stmt->execute([$turnoId, $formularioId, $token]); } diff --git a/modules/turnero/views/recepcion.php b/modules/turnero/views/recepcion.php index 695294e..58a38ef 100644 --- a/modules/turnero/views/recepcion.php +++ b/modules/turnero/views/recepcion.php @@ -506,8 +506,9 @@ async function onLugarChange() { } } async function abrirFormularioLugar(formularioId, nombre) { - if (!turnoActivo) return; - // Si ya hay solicitud guardada, usa el consent token + if (!turnoActivo) { mostrarError('Llama un turno primero.'); return; } + if (!pacienteActivo) { mostrarError('Vincula el paciente antes de abrir el consentimiento.'); return; } + try { const res = await fetch(API + 'create_consent_token.php', { method: 'POST', @@ -515,13 +516,17 @@ async function abrirFormularioLugar(formularioId, nombre) { body: JSON.stringify({ turno_id: turnoActivo.id, formulario_id: formularioId }), }); const json = await res.json(); - if (json.ok && json.data?.url) { - window.open(json.data.url, '_blank'); + if (json.ok && (json.data?.url || json.url)) { + window.open(json.data?.url || json.url, '_blank'); + // Polling automático para detectar la firma + if (!pollingConsentimientosId) { + pollingConsentimientosId = setInterval(refrescarConsentimientosLugar, 3000); + } } else { - window.open('lab_formularios.php', '_blank'); + mostrarError(json.error || 'No se pudo generar el enlace de firma.'); } - } catch (_) { - window.open('lab_formularios.php', '_blank'); + } catch (err) { + mostrarError('Error al generar enlace: ' + err.message); } } @@ -709,15 +714,56 @@ function abrirFicha(turno) { .catch(() => {}); } - // Buscar consentimientos si ya tiene solicitud - verificarSolicitudExistente(turno.id); + // Cargar consentimientos del desk actual para este turno + if (DESK_ID) cargarConsentimientosDesk(turno.id); } -async function verificarSolicitudExistente(turnoId) { +// ── Cargar consentimientos del desk para el turno ───────────── +async function cargarConsentimientosDesk(turnoId) { + const sec = document.getElementById('sec-consentimientos'); + const lista = document.getElementById('lista-consentimientos'); + try { - const res = await fetch(API + 'get_cola.php?area=recepcion'); - // La solicitud se carga solo después de guardar - } catch (_) {} + // 1. Si el turno ya tiene registros creados, mostrarlos con su estado real + const resExist = await fetch(`${API}get_consentimientos.php?turno_id=${turnoId}`); + const jsonExist = await resExist.json(); + if (jsonExist.ok && jsonExist.consentimientos?.length) { + consentimientos = jsonExist.consentimientos.map(c => ({ + ...c, + formulario_nombre: c.formulario_nombre || c.nombre, + })); + renderConsentimientos(consentimientos); + sec.style.display = ''; + return; + } + + // 2. Aún no existen registros: mostrar los configurados para el desk como pendientes + const resDesk = await fetch(`${API}get_lugar_formularios.php?lugar_id=${DESK_ID}`); + const jsonDesk = await resDesk.json(); + if (!jsonDesk.ok || !jsonDesk.formularios?.length) { + sec.style.display = 'none'; + return; + } + + lista.innerHTML = jsonDesk.formularios.map(f => { + const nom = escHtml(f.nombre); + return ``; + }).join(''); + sec.style.display = ''; + } catch (_) { + sec.style.display = 'none'; + } } // ── Buscador de paciente ──────────────────────────────────────