feat: teclado nativo en transferencias, recarga en tiempo real ultra rapida y barra de botones admin unificada

This commit is contained in:
unknown
2026-10-04 23:50:40 -06:00
parent 9ab515fe1a
commit fda8d50778
4 changed files with 274 additions and 84 deletions
+82 -13
View File
@@ -1245,23 +1245,77 @@ body.dark-mode .floating-cart-bar {
flex-wrap: wrap;
}
.admin-action-btn-caja {
padding: 7px 12px;
background: #0284c7;
color: #ffffff;
.admin-header-actions .theme-toggle-btn {
width: 38px;
height: 38px;
min-width: 38px;
border-radius: 10px;
font-size: 0.78rem;
font-weight: 800;
text-decoration: none;
display: inline-flex;
align-items: center;
gap: 4px;
justify-content: center;
font-size: 1.05rem;
box-sizing: border-box;
margin: 0;
border: 1.5px solid var(--border);
}
.admin-action-btn {
height: 38px;
padding: 0 14px;
border-radius: 10px;
font-size: 0.82rem;
font-weight: 800;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 6px;
white-space: nowrap;
transition: background 0.15s ease;
border: none;
cursor: pointer;
text-decoration: none;
box-sizing: border-box;
transition: all 0.15s ease;
line-height: 1;
}
.admin-action-btn:active {
transform: scale(0.96);
}
.admin-action-btn-recarga {
background: linear-gradient(135deg, #10b981, #059669);
color: #ffffff;
box-shadow: 0 2px 6px rgba(16, 185, 129, 0.25);
}
.admin-action-btn-recarga:hover {
background: linear-gradient(135deg, #059669, #047857);
}
.admin-action-btn-caja {
background: linear-gradient(135deg, #0284c7, #0369a1);
color: #ffffff;
box-shadow: 0 2px 6px rgba(2, 132, 199, 0.25);
}
.admin-action-btn-caja:hover {
background: #0369a1;
background: linear-gradient(135deg, #0369a1, #075985);
}
.admin-action-btn-salir {
background: #fee2e2;
color: #dc2626;
border: 1px solid #fecaca;
}
.admin-action-btn-salir:hover {
background: #fecaca;
}
body.dark-mode .admin-action-btn-salir {
background: rgba(239, 68, 68, 0.15);
color: #f87171;
border-color: rgba(239, 68, 68, 0.3);
}
.admin-badge {
@@ -1432,13 +1486,28 @@ body.dark-mode .floating-cart-bar {
}
.admin-header {
flex-direction: column;
align-items: flex-start;
gap: 10px;
align-items: stretch;
gap: 12px;
padding: 12px;
}
.admin-header-actions {
width: 100%;
justify-content: space-between;
display: grid;
grid-template-columns: 38px 1fr 1fr 1fr;
gap: 6px;
align-items: center;
}
.admin-action-btn {
height: 38px;
padding: 0 4px;
font-size: 0.74rem;
gap: 3px;
}
@media (max-width: 380px) {
.admin-action-btn {
font-size: 0.67rem;
padding: 0 2px;
}
}
.admin-stats-grid {
grid-template-columns: repeat(2, 1fr);
+17 -33
View File
@@ -10,7 +10,7 @@
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-mobile-web-app-title" content="RecreoPay">
<meta name="mobile-web-app-capable" content="yes">
<link rel="stylesheet" href="/css/styles.css?v=8.8">
<link rel="stylesheet" href="/css/styles.css?v=9.4">
<style>
/* Transiciones suaves */
.fade-enter { animation: fadeIn 0.2s ease-in-out; }
@@ -131,15 +131,14 @@
<button onclick="toggleTheme()" class="theme-toggle-btn" title="Alternar Modo Oscuro / Modo Claro">
🌙
</button>
<button onclick="switchAdminTab('recarga')" style="padding: 8px 14px; background: linear-gradient(135deg, #10b981, #059669); color: white; border: none; border-radius: 10px; font-size: 0.82rem; font-weight: 800; cursor: pointer; display: flex; align-items: center; gap: 6px; box-shadow: 0 2px 6px rgba(16, 185, 129, 0.3);" title="Cargar dinero a un estudiante">
<span>💵</span> Cargar Dinero
<button onclick="switchAdminTab('recarga')" class="admin-action-btn admin-action-btn-recarga" title="Cargar dinero a un estudiante">
<span>💵</span> <span class="admin-btn-text">Cargar Dinero</span>
</button>
<a href="/pos.html" target="_blank" class="admin-action-btn-caja" title="Abrir Caja / Terminal POS">
📟 Ir a Caja
<a href="/pos.html" target="_blank" class="admin-action-btn admin-action-btn-caja" title="Abrir Caja / Terminal POS">
<span>📟</span> <span class="admin-btn-text">Ir a Caja</span>
</a>
<button onclick="logout(true)" class="logout-btn" title="Cerrar sesión y cambiar de cuenta">
<span>🚪</span>
<span class="logout-text">Salir</span>
<button onclick="logout(true)" class="admin-action-btn admin-action-btn-salir" title="Cerrar sesión y cambiar de cuenta">
<span>🚪</span> <span class="admin-btn-text">Salir</span>
</button>
</div>
</header>
@@ -1042,33 +1041,18 @@
<input type="text" id="inputTransferMotivo" placeholder="Ej: Mitad de la empanada, fresco..." style="width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid #cbd5e1; font-size: 0.85rem;">
</div>
<!-- PIN DE SEGURIDAD (CON PANEL EXCLUSIVO DE NÚMEROS Y BOTONES TÁCTILES) -->
<div style="background: #f8fafc; border: 1.5px dashed #cbd5e1; border-radius: 12px; padding: 12px; margin-bottom: 14px;">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px;">
<label style="font-size: 0.78rem; font-weight: 800; color: #0f172a;">🔐 Digita tu PIN de 4 dígitos:</label>
<!-- PIN DE SEGURIDAD (TECLADO NATIVO DEL TELÉFONO) -->
<div style="background: #f8fafc; border: 1.5px dashed #cbd5e1; border-radius: 14px; padding: 14px; margin-bottom: 14px; text-align: center;">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px;">
<label for="inputTransferPin" style="font-size: 0.8rem; font-weight: 800; color: #0f172a;">🔐 Digita tu PIN de seguridad:</label>
<span id="lblTransferPinHint" style="font-size: 0.72rem; color: #166534; font-weight: 800; background: #dcfce7; padding: 2px 8px; border-radius: 6px;">(PIN: 1234)</span>
</div>
<div style="position: relative; margin-bottom: 8px;">
<input type="password" inputmode="numeric" pattern="[0-9]*" maxlength="4" autocomplete="one-time-code" id="inputTransferPin" placeholder="••••" style="width: 100%; text-align: center; font-size: 1.5rem; letter-spacing: 8px; padding: 10px 40px 10px 10px; border-radius: 10px; border: 2px solid #cbd5e1; font-weight: 900; background: white; color: #0f172a;">
<button type="button" id="btnTogglePin" onclick="togglePinVisibility()" style="position: absolute; right: 10px; top: 50%; transform: translateY(-50%); background: none; border: none; font-size: 1.15rem; cursor: pointer; color: #64748b; padding: 4px;" title="Ver/Ocultar PIN">👁️</button>
</div>
<!-- TECLADO NUMÉRICO TÁCTIL EN PANTALLA (FACILITA A LOS NIÑOS Y USUARIOS MÓVILES) -->
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px;">
<button type="button" onclick="appendPinDigit('1')" style="padding: 10px; background: white; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 1.15rem; font-weight: 800; color: #1e293b; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.05);">1</button>
<button type="button" onclick="appendPinDigit('2')" style="padding: 10px; background: white; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 1.15rem; font-weight: 800; color: #1e293b; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.05);">2</button>
<button type="button" onclick="appendPinDigit('3')" style="padding: 10px; background: white; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 1.15rem; font-weight: 800; color: #1e293b; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.05);">3</button>
<button type="button" onclick="appendPinDigit('4')" style="padding: 10px; background: white; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 1.15rem; font-weight: 800; color: #1e293b; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.05);">4</button>
<button type="button" onclick="appendPinDigit('5')" style="padding: 10px; background: white; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 1.15rem; font-weight: 800; color: #1e293b; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.05);">5</button>
<button type="button" onclick="appendPinDigit('6')" style="padding: 10px; background: white; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 1.15rem; font-weight: 800; color: #1e293b; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.05);">6</button>
<button type="button" onclick="appendPinDigit('7')" style="padding: 10px; background: white; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 1.15rem; font-weight: 800; color: #1e293b; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.05);">7</button>
<button type="button" onclick="appendPinDigit('8')" style="padding: 10px; background: white; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 1.15rem; font-weight: 800; color: #1e293b; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.05);">8</button>
<button type="button" onclick="appendPinDigit('9')" style="padding: 10px; background: white; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 1.15rem; font-weight: 800; color: #1e293b; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.05);">9</button>
<button type="button" onclick="fillQuickPin('1234')" style="padding: 10px; background: #e0f2fe; color: #0284c7; border: 1px solid #bae6fd; border-radius: 8px; font-size: 0.74rem; font-weight: 900; cursor: pointer;">⚡ 1234</button>
<button type="button" onclick="appendPinDigit('0')" style="padding: 10px; background: white; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 1.15rem; font-weight: 800; color: #1e293b; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.05);">0</button>
<button type="button" onclick="backspacePinDigit()" style="padding: 10px; background: #fee2e2; color: #dc2626; border: 1px solid #fecaca; border-radius: 8px; font-size: 1.15rem; font-weight: 800; cursor: pointer;">⌫</button>
<div style="position: relative; max-width: 240px; margin: 0 auto;">
<input type="password" inputmode="numeric" pattern="[0-9]*" maxlength="4" autocomplete="one-time-code" id="inputTransferPin" placeholder="••••" style="width: 100%; text-align: center; font-size: 1.8rem; letter-spacing: 10px; padding: 10px 42px 10px 18px; border-radius: 12px; border: 2px solid #cbd5e1; font-weight: 900; background: white; color: #0f172a; box-sizing: border-box; outline: none; transition: border-color 0.2s;" onfocus="this.style.borderColor='#0284c7'" onblur="this.style.borderColor='#cbd5e1'">
<button type="button" id="btnTogglePin" onclick="togglePinVisibility()" style="position: absolute; right: 10px; top: 50%; transform: translateY(-50%); background: none; border: none; font-size: 1.25rem; cursor: pointer; color: #64748b; padding: 4px;" title="Ver/Ocultar PIN">👁️</button>
</div>
<div style="font-size: 0.72rem; color: #64748b; margin-top: 6px; font-weight: 600;">Se abrirá automáticamente el teclado numérico de tu teléfono</div>
</div>
<button id="btnConfirmTransfer" onclick="executeP2PTransfer()" style="width: 100%; padding: 14px; background: linear-gradient(135deg, #10b981, #059669); color: white; border: none; border-radius: 14px; font-weight: 900; font-size: 1.05rem; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 8px; box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);">
@@ -1244,8 +1228,8 @@
</button>
</nav>
<script src="/js/sounds.js?v=9.3"></script>
<script src="/js/sounds.js?v=9.4"></script>
<script src="/js/jsQR.js"></script>
<script src="/js/app.js?v=9.3"></script>
<script src="/js/app.js?v=9.4"></script>
</body>
</html>
+143 -23
View File
@@ -74,6 +74,21 @@ document.addEventListener('DOMContentLoaded', async () => {
}
initStudentSSE();
const transferPinEl = document.getElementById('inputTransferPin');
if (transferPinEl) {
transferPinEl.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
executeP2PTransfer();
}
});
transferPinEl.addEventListener('input', (e) => {
if (window.sounds && e.data) {
window.sounds.playTap();
}
});
}
});
// ==========================================
@@ -1014,6 +1029,15 @@ function onTransferTargetIdentified(target) {
document.getElementById('transferStepScan').style.display = 'none';
document.getElementById('transferStepConfirm').style.display = 'block';
// Enfocar automáticamente el PIN para desplegar el teclado nativo del teléfono
setTimeout(() => {
const pin = document.getElementById('inputTransferPin');
if (pin) {
pin.focus();
try { pin.scrollIntoView({ behavior: 'smooth', block: 'center' }); } catch(e){}
}
}, 200);
}
function resetTransferScan() {
@@ -1409,30 +1433,54 @@ function triggerBalancePulse() {
}
// SSE en tiempo real para eventos de la soda y monederos
let sseSource = null;
let sseReconnectTimer = null;
function initStudentSSE() {
const sse = new EventSource('/api/events');
if (sseSource) {
try { sseSource.close(); } catch (e) {}
}
sseSource = new EventSource('/api/events');
const sse = sseSource;
sse.onerror = () => {
try { sse.close(); } catch(e) {}
if (!sseReconnectTimer) {
sseReconnectTimer = setTimeout(() => {
sseReconnectTimer = null;
initStudentSSE();
}, 3000);
}
};
// Cobro inmediato en caja / pre-orden realizada
sse.addEventListener('nueva_orden', (e) => {
try {
const data = JSON.parse(e.data);
const estId = data.estudiante_id || (data.financiero && data.financiero.estudiante && data.financiero.estudiante.id);
const estId = Number(data.estudiante_id || (data.financiero && data.financiero.estudiante && data.financiero.estudiante.id));
const myId = currentStudent ? Number(currentStudent.id) : (currentUser && currentUser.estudiante ? Number(currentUser.estudiante.id) : null);
// Si el estudiante en pantalla fue a quien se le cobró
if (currentStudent && currentStudent.id === estId) {
if (myId && myId === estId) {
if (window.sounds) window.sounds.playCoin();
if (data.financiero && data.financiero.estudiante) {
const f = data.financiero.estudiante;
if (currentStudent) {
currentStudent.saldo_colones = f.saldo_nuevo;
currentStudent.gastado_hoy = f.gastado_hoy;
currentStudent.disponible_hoy = (typeof f.disponible_hoy === 'number')
? f.disponible_hoy
: Math.max(0, (f.limite_diario || currentStudent.limite_diario_colones || 0) - (f.gastado_hoy || 0));
}
if (currentUser && currentUser.estudiante) {
currentUser.estudiante.saldo_colones = f.saldo_nuevo;
}
updateStudentUI();
triggerBalancePulse();
}
selectStudent(currentStudent.id);
fetch(`/api/estudiantes/${estId}`).then(r => r.json()).then(s => { if (s && currentStudent && currentStudent.id === s.id) { currentStudent = s; updateStudentUI(); } }).catch(()=>{});
}
// Si es padre de este estudiante
@@ -1441,7 +1489,7 @@ function initStudentSSE() {
}
// Si es admin
if (currentUser && currentUser.rol === 'admin') {
if (currentUser && (currentUser.rol === 'admin' || currentUser.rol === 'cajero')) {
loadAdminData();
}
} catch (err) {
@@ -1453,22 +1501,30 @@ function initStudentSSE() {
sse.addEventListener('saldo_actualizado', (e) => {
try {
const data = JSON.parse(e.data);
const estId = data.estudiante_id || data.id;
const estId = Number(data.estudiante_id || data.id);
const myId = currentStudent ? Number(currentStudent.id) : (currentUser && currentUser.estudiante ? Number(currentUser.estudiante.id) : null);
if (currentStudent && currentStudent.id === estId) {
if (myId && myId === estId) {
if (window.sounds) window.sounds.playCoin();
if (currentStudent) {
if (typeof data.saldo_colones === 'number') currentStudent.saldo_colones = data.saldo_colones;
if (typeof data.saldo_nuevo === 'number') currentStudent.saldo_colones = data.saldo_nuevo;
if (typeof data.disponible_hoy === 'number') currentStudent.disponible_hoy = data.disponible_hoy;
if (typeof data.gastado_hoy === 'number') currentStudent.gastado_hoy = data.gastado_hoy;
}
if (currentUser && currentUser.estudiante) {
if (typeof data.saldo_colones === 'number') currentUser.estudiante.saldo_colones = data.saldo_colones;
if (typeof data.saldo_nuevo === 'number') currentUser.estudiante.saldo_colones = data.saldo_nuevo;
}
updateStudentUI();
triggerBalancePulse();
selectStudent(currentStudent.id);
fetch(`/api/estudiantes/${estId}`).then(r => r.json()).then(s => { if (s && currentStudent && currentStudent.id === s.id) { currentStudent = s; updateStudentUI(); } }).catch(()=>{});
}
if (currentUser && currentUser.rol === 'padre') {
loadParentDashboard();
}
if (currentUser && currentUser.rol === 'admin') {
if (currentUser && (currentUser.rol === 'admin' || currentUser.rol === 'cajero')) {
loadAdminData();
}
} catch (err) {}
@@ -1477,18 +1533,23 @@ function initStudentSSE() {
sse.addEventListener('transferencia_realizada', (e) => {
try {
const data = JSON.parse(e.data);
if (currentStudent && data.receptor && data.receptor.id === currentStudent.id) {
const myId = currentStudent ? Number(currentStudent.id) : (currentUser && currentUser.estudiante ? Number(currentUser.estudiante.id) : null);
if (myId && data.receptor && Number(data.receptor.id) === myId) {
if (window.sounds) window.sounds.playCoin();
alert(`🔔 ¡Te pasaron plata!\n${data.emisor.nombre} te transfirió ₡${data.monto.toLocaleString('es-CR')}.\nMotivo: ${data.motivo}`);
selectStudent(currentStudent.id);
if (currentStudent && typeof data.receptor.saldo_nuevo === 'number') {
currentStudent.saldo_colones = data.receptor.saldo_nuevo;
updateStudentUI();
triggerBalancePulse();
} else if (currentStudent && data.emisor && data.emisor.id === currentStudent.id) {
if (typeof data.emisor.saldo_nuevo === 'number') {
}
alert(`🔔 ¡Te pasaron plata!\n${data.emisor.nombre} te transfirió ₡${data.monto.toLocaleString('es-CR')}.\nMotivo: ${data.motivo}`);
fetch(`/api/estudiantes/${myId}`).then(r => r.json()).then(s => { if (s && currentStudent && currentStudent.id === s.id) { currentStudent = s; updateStudentUI(); } }).catch(()=>{});
} else if (myId && data.emisor && Number(data.emisor.id) === myId) {
if (currentStudent && typeof data.emisor.saldo_nuevo === 'number') {
currentStudent.saldo_colones = data.emisor.saldo_nuevo;
updateStudentUI();
triggerBalancePulse();
}
selectStudent(currentStudent.id);
fetch(`/api/estudiantes/${myId}`).then(r => r.json()).then(s => { if (s && currentStudent && currentStudent.id === s.id) { currentStudent = s; updateStudentUI(); } }).catch(()=>{});
}
if (currentUser && currentUser.rol === 'padre') {
@@ -1497,18 +1558,23 @@ function initStudentSSE() {
} catch (err) {}
});
// RECARGA DE SALDO INMEDIATA (0ms de latencia visual)
sse.addEventListener('recarga_exitosa', (e) => {
try {
const data = JSON.parse(e.data);
const estId = data.estudiante_id || data.id;
if (currentStudent && currentStudent.id === estId) {
const estId = Number(data.estudiante_id || data.id);
const myId = currentStudent ? Number(currentStudent.id) : (currentUser && currentUser.estudiante ? Number(currentUser.estudiante.id) : null);
if (myId && myId === estId) {
if (window.sounds) window.sounds.playCoin();
if (typeof data.saldo_nuevo === 'number') {
currentStudent.saldo_colones = data.saldo_nuevo;
const nuevo = typeof data.saldo_nuevo === 'number' ? data.saldo_nuevo : data.saldo_colones;
if (typeof nuevo === 'number') {
if (currentStudent) currentStudent.saldo_colones = nuevo;
if (currentUser && currentUser.estudiante) currentUser.estudiante.saldo_colones = nuevo;
updateStudentUI();
triggerBalancePulse();
}
selectStudent(currentStudent.id);
fetch(`/api/estudiantes/${estId}`).then(r => r.json()).then(s => { if (s && currentStudent && currentStudent.id === s.id) { currentStudent = s; updateStudentUI(); } }).catch(()=>{});
}
if (currentUser && currentUser.rol === 'padre') {
loadParentDashboard();
@@ -1546,15 +1612,23 @@ function initStudentSSE() {
sse.addEventListener('estudiante_actualizado', (e) => {
try {
const data = JSON.parse(e.data);
const estId = data.id || data.estudiante_id;
if (currentStudent && currentStudent.id === estId) {
const estId = Number(data.id || data.estudiante_id);
const myId = currentStudent ? Number(currentStudent.id) : (currentUser && currentUser.estudiante ? Number(currentUser.estudiante.id) : null);
if (myId && myId === estId) {
if (currentStudent) {
if (typeof data.saldo_colones === 'number') currentStudent.saldo_colones = data.saldo_colones;
if (typeof data.saldo_nuevo === 'number') currentStudent.saldo_colones = data.saldo_nuevo;
if (typeof data.disponible_hoy === 'number') currentStudent.disponible_hoy = data.disponible_hoy;
if (typeof data.gastado_hoy === 'number') currentStudent.gastado_hoy = data.gastado_hoy;
if (typeof data.limite_diario_colones === 'number') currentStudent.limite_diario_colones = data.limite_diario_colones;
}
if (currentUser && currentUser.estudiante) {
if (typeof data.saldo_colones === 'number') currentUser.estudiante.saldo_colones = data.saldo_colones;
if (typeof data.saldo_nuevo === 'number') currentUser.estudiante.saldo_colones = data.saldo_nuevo;
}
updateStudentUI();
triggerBalancePulse();
selectStudent(currentStudent.id);
fetch(`/api/estudiantes/${estId}`).then(r => r.json()).then(s => { if (s && currentStudent && currentStudent.id === s.id) { currentStudent = s; updateStudentUI(); } }).catch(()=>{});
}
if (currentUser && currentUser.rol === 'padre') {
loadParentDashboard();
@@ -1582,6 +1656,52 @@ function initStudentSSE() {
});
}
// Sincronización en segundo plano: al volver a la pestaña/desbloquear el teléfono
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
if (currentStudent && currentStudent.id) {
fetch(`/api/estudiantes/${currentStudent.id}`)
.then(r => r.json())
.then(fresh => {
if (fresh && currentStudent && currentStudent.id === fresh.id) {
const cambio = currentStudent.saldo_colones !== fresh.saldo_colones;
currentStudent = fresh;
updateStudentUI();
if (cambio) {
triggerBalancePulse();
if (window.sounds) window.sounds.playCoin();
}
}
})
.catch(() => {});
}
if (currentUser && currentUser.rol === 'padre') {
loadParentDashboard();
}
if (!sseSource || sseSource.readyState === EventSource.CLOSED) {
initStudentSSE();
}
}
});
// Comprobación rápida periódica (cada 4s) cuando la app de estudiante está visible
setInterval(async () => {
if (document.visibilityState === 'visible' && currentStudent && currentStudent.id && currentUser && currentUser.rol === 'estudiante') {
try {
const res = await fetch(`/api/estudiantes/${currentStudent.id}`);
if (res.ok) {
const fresh = await res.json();
if (fresh && currentStudent && currentStudent.id === fresh.id && fresh.saldo_colones !== currentStudent.saldo_colones) {
currentStudent = fresh;
updateStudentUI();
triggerBalancePulse();
if (window.sounds) window.sounds.playCoin();
}
}
} catch (e) {}
}
}, 4000);
// ==========================================
// FUNCIONES DEL PORTAL DEDICADO DE PADRES
// ==========================================
+20 -3
View File
@@ -29,23 +29,40 @@ function broadcastEvent(eventType, data) {
for (const client of sseClients) {
try {
client.write(payload);
if (typeof client.flush === 'function') client.flush();
} catch (e) {
sseClients.delete(client);
}
}
}
// SSE stream for real-time soda screen
// SSE stream for real-time soda screen and student apps
app.get('/api/events', (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
res.setHeader('Cache-Control', 'no-cache, no-transform');
res.setHeader('Connection', 'keep-alive');
res.setHeader('X-Accel-Buffering', 'no');
res.flushHeaders();
// Enviar handshake inmediato para confirmar conexión abierta sin buffering
res.write(': connected\n\n');
sseClients.add(res);
req.on('close', () => sseClients.delete(res));
});
// Heartbeat cada 10 segundos para mantener vivas las conexiones móviles / proxies
setInterval(() => {
const ping = ': ping\n\n';
for (const client of sseClients) {
try {
client.write(ping);
if (typeof client.flush === 'function') client.flush();
} catch (e) {
sseClients.delete(client);
}
}
}, 10000);
// ==========================================
// 0. AUTENTICACIÓN Y ROLES (ADMIN, PADRE, ESTUDIANTE)
// ==========================================