Fix internacion lookup, persistence and fallback patient resolution
This commit is contained in:
+15
-3
@@ -1,4 +1,4 @@
|
|||||||
import { MongoClient } from 'mongodb';
|
import { MongoClient, ObjectId } from 'mongodb';
|
||||||
import bcrypt from 'bcryptjs';
|
import bcrypt from 'bcryptjs';
|
||||||
const { compareSync, hashSync } = bcrypt;
|
const { compareSync, hashSync } = bcrypt;
|
||||||
|
|
||||||
@@ -102,6 +102,9 @@ export async function initDb() {
|
|||||||
function cleanDoc(doc) {
|
function cleanDoc(doc) {
|
||||||
if (!doc) return null;
|
if (!doc) return null;
|
||||||
const { _id, ...rest } = doc;
|
const { _id, ...rest } = doc;
|
||||||
|
if (!rest.id && _id) {
|
||||||
|
rest.id = _id.toString();
|
||||||
|
}
|
||||||
return rest;
|
return rest;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,12 +355,21 @@ export async function getAllInternaciones() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getInternacionById(id) {
|
export async function getInternacionById(id) {
|
||||||
|
if (!id) return null;
|
||||||
|
const idStr = String(id).trim();
|
||||||
if (db) {
|
if (db) {
|
||||||
const internacion = await db.collection('internaciones').findOne({ id });
|
let internacion = await db.collection('internaciones').findOne({ id: idStr });
|
||||||
|
if (!internacion && ObjectId.isValid(idStr)) {
|
||||||
|
try {
|
||||||
|
internacion = await db.collection('internaciones').findOne({ _id: new ObjectId(idStr) });
|
||||||
|
} catch {
|
||||||
|
// ignore invalid objectid
|
||||||
|
}
|
||||||
|
}
|
||||||
if (internacion) internacion.activa = !!internacion.activa;
|
if (internacion) internacion.activa = !!internacion.activa;
|
||||||
return cleanDoc(internacion);
|
return cleanDoc(internacion);
|
||||||
}
|
}
|
||||||
const internacion = memStore.internaciones.find(i => i.id === id);
|
const internacion = memStore.internaciones.find(i => i.id === idStr || String(i.id).trim() === idStr);
|
||||||
if (!internacion) return null;
|
if (!internacion) return null;
|
||||||
return { ...internacion, activa: !!internacion.activa };
|
return { ...internacion, activa: !!internacion.activa };
|
||||||
}
|
}
|
||||||
|
|||||||
+44
-8
@@ -131,12 +131,30 @@ function AppContent() {
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
case 'historiaclinica': {
|
case 'historiaclinica': {
|
||||||
const internacionId = store.currentInternacionId || '';
|
const internacionId = store.currentInternacionId || sessionStorage.getItem('hospital_current_internacion_id') || '';
|
||||||
const internacion = store.getInternacionById(internacionId);
|
const internacion = store.getInternacionById(internacionId);
|
||||||
const paciente = internacion ? store.getPacienteById(internacion.pacienteId) : undefined;
|
const paciente = internacion
|
||||||
if (!internacion || !paciente) {
|
? (store.getPacienteById(internacion.pacienteId) || store.pacientes.find(p => p.id === internacion.pacienteId || p.dni === internacion.pacienteId) || {
|
||||||
|
id: internacion.pacienteId || 'paciente-unknown',
|
||||||
|
apellido: 'Paciente',
|
||||||
|
nombre: 'Sin registrar',
|
||||||
|
dni: internacion.pacienteId || 'N/A',
|
||||||
|
fechaNacimiento: '1990-01-01',
|
||||||
|
sexo: 'Otro' as const,
|
||||||
|
fechaRegistro: new Date().toISOString()
|
||||||
|
})
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
if (!internacion) {
|
||||||
|
if (!store.isLoaded) {
|
||||||
|
return (
|
||||||
|
<div className="p-8 text-center text-gray-500">
|
||||||
|
<p>Cargando información de la internación...</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<div className="p-4 text-center">
|
<div className="p-4 text-center space-y-4">
|
||||||
<p className="text-gray-500 dark:text-gray-400">Internación no encontrada (id: {internacionId})</p>
|
<p className="text-gray-500 dark:text-gray-400">Internación no encontrada (id: {internacionId})</p>
|
||||||
<Button onClick={() => store.setVista('internaciones')}>Volver a Internaciones</Button>
|
<Button onClick={() => store.setVista('internaciones')}>Volver a Internaciones</Button>
|
||||||
</div>
|
</div>
|
||||||
@@ -215,12 +233,30 @@ function AppContent() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
case 'editaringreso': {
|
case 'editaringreso': {
|
||||||
const internacionId = store.currentInternacionId || '';
|
const internacionId = store.currentInternacionId || sessionStorage.getItem('hospital_current_internacion_id') || '';
|
||||||
const internacion = store.getInternacionById(internacionId);
|
const internacion = store.getInternacionById(internacionId);
|
||||||
const paciente = internacion ? store.getPacienteById(internacion.pacienteId) : undefined;
|
const paciente = internacion
|
||||||
if (!internacion || !paciente) {
|
? (store.getPacienteById(internacion.pacienteId) || store.pacientes.find(p => p.id === internacion.pacienteId || p.dni === internacion.pacienteId) || {
|
||||||
|
id: internacion.pacienteId || 'paciente-unknown',
|
||||||
|
apellido: 'Paciente',
|
||||||
|
nombre: 'Sin registrar',
|
||||||
|
dni: internacion.pacienteId || 'N/A',
|
||||||
|
fechaNacimiento: '1990-01-01',
|
||||||
|
sexo: 'Otro' as const,
|
||||||
|
fechaRegistro: new Date().toISOString()
|
||||||
|
})
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
if (!internacion) {
|
||||||
|
if (!store.isLoaded) {
|
||||||
|
return (
|
||||||
|
<div className="p-8 text-center text-gray-500">
|
||||||
|
<p>Cargando información de la internación...</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<div className="p-4 text-center">
|
<div className="p-4 text-center space-y-4">
|
||||||
<p className="text-gray-500 dark:text-gray-400">Internación no encontrada (id: {internacionId})</p>
|
<p className="text-gray-500 dark:text-gray-400">Internación no encontrada (id: {internacionId})</p>
|
||||||
<Button onClick={() => store.setVista('internaciones')}>Volver a Internaciones</Button>
|
<Button onClick={() => store.setVista('internaciones')}>Volver a Internaciones</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -94,6 +94,7 @@ export function useHospitalStore() {
|
|||||||
if (mounted && body) {
|
if (mounted && body) {
|
||||||
const defaults = defaultState();
|
const defaults = defaultState();
|
||||||
const storedUser = sessionStorage.getItem('hospital_user');
|
const storedUser = sessionStorage.getItem('hospital_user');
|
||||||
|
const storedInternacionId = sessionStorage.getItem('hospital_current_internacion_id');
|
||||||
const currentUser = storedUser ? JSON.parse(storedUser) : null;
|
const currentUser = storedUser ? JSON.parse(storedUser) : null;
|
||||||
const normalized = {
|
const normalized = {
|
||||||
...defaults,
|
...defaults,
|
||||||
@@ -103,6 +104,7 @@ export function useHospitalStore() {
|
|||||||
atb: body.atb || [],
|
atb: body.atb || [],
|
||||||
glucemias: body.glucemias || [],
|
glucemias: body.glucemias || [],
|
||||||
movimientosIndicaciones: body.movimientosIndicaciones || [],
|
movimientosIndicaciones: body.movimientosIndicaciones || [],
|
||||||
|
currentInternacionId: storedInternacionId || body.currentInternacionId || null,
|
||||||
currentUser,
|
currentUser,
|
||||||
isAuthenticated: !!currentUser,
|
isAuthenticated: !!currentUser,
|
||||||
};
|
};
|
||||||
@@ -1237,15 +1239,24 @@ const actualizarInternacion = useCallback(async (internacionId: string, datos: P
|
|||||||
|
|
||||||
// Funciones de utilidad para consultas
|
// Funciones de utilidad para consultas
|
||||||
const getPacienteById = useCallback((id: string) => {
|
const getPacienteById = useCallback((id: string) => {
|
||||||
return state.pacientes.find(p => p.id === id);
|
if (!id) return undefined;
|
||||||
|
const target = String(id).trim().toLowerCase();
|
||||||
|
return state.pacientes.find(p =>
|
||||||
|
(p.id && String(p.id).trim().toLowerCase() === target) ||
|
||||||
|
(p.dni && String(p.dni).trim().toLowerCase() === target)
|
||||||
|
);
|
||||||
}, [state.pacientes]);
|
}, [state.pacientes]);
|
||||||
|
|
||||||
const getCamaById = useCallback((id: string) => {
|
const getCamaById = useCallback((id: string) => {
|
||||||
return state.camas.find(c => c.id === id);
|
if (!id) return undefined;
|
||||||
|
const target = String(id).trim().toLowerCase();
|
||||||
|
return state.camas.find(c => c.id && String(c.id).trim().toLowerCase() === target);
|
||||||
}, [state.camas]);
|
}, [state.camas]);
|
||||||
|
|
||||||
const getInternacionById = useCallback((id: string) => {
|
const getInternacionById = useCallback((id: string) => {
|
||||||
return state.internaciones.find(i => i.id === id);
|
if (!id) return undefined;
|
||||||
|
const target = String(id).trim().toLowerCase();
|
||||||
|
return state.internaciones.find(i => i.id && String(i.id).trim().toLowerCase() === target);
|
||||||
}, [state.internaciones]);
|
}, [state.internaciones]);
|
||||||
|
|
||||||
const getInternacionActivaByPaciente = useCallback((pacienteId: string) => {
|
const getInternacionActivaByPaciente = useCallback((pacienteId: string) => {
|
||||||
@@ -1309,6 +1320,11 @@ const actualizarInternacion = useCallback(async (internacionId: string, datos: P
|
|||||||
|
|
||||||
// Current internacion selection (for HC page)
|
// Current internacion selection (for HC page)
|
||||||
const setCurrentInternacion = useCallback((id?: string | null) => {
|
const setCurrentInternacion = useCallback((id?: string | null) => {
|
||||||
|
if (id) {
|
||||||
|
sessionStorage.setItem('hospital_current_internacion_id', id);
|
||||||
|
} else {
|
||||||
|
sessionStorage.removeItem('hospital_current_internacion_id');
|
||||||
|
}
|
||||||
setState(prev => ({ ...prev, currentInternacionId: id ?? null }));
|
setState(prev => ({ ...prev, currentInternacionId: id ?? null }));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -1434,5 +1450,6 @@ const actualizarInternacion = useCallback(async (internacionId: string, datos: P
|
|||||||
getCamaGrupoId,
|
getCamaGrupoId,
|
||||||
getInternacionGrupoId,
|
getInternacionGrupoId,
|
||||||
getPacienteGrupoId,
|
getPacienteGrupoId,
|
||||||
|
isLoaded,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user