Actualizar control de glucemias con eje Y secundario para correcciones e integracion de tienda
This commit is contained in:
+298
-28
@@ -1,6 +1,22 @@
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import { getDb, getValue, setValue, getUsuarioByDni, getUsuarioById, getAllUsuarios, createUsuario, updateUsuario, deleteUsuario, verifyPassword, hashPassword, getAllPacientes, getAllAreas, getAllCamas, getAllInternaciones, getAllEvoluciones, getAllLaboratorios, getAllAcidosBase, getAllCultivos, getAllEstudiosComplementarios, getAllInterconsultas, getAllAtb, getAllIndicaciones, getAllMovimientosIndicaciones } from './db.js';
|
||||
import {
|
||||
getDb, getValue, setValue, getUsuarioByDni, getUsuarioById, getAllUsuarios, createUsuario, updateUsuario, deleteUsuario, verifyPassword, hashPassword,
|
||||
getAllPacientes, createPaciente, updatePaciente, deletePaciente,
|
||||
getAllAreas, createArea, updateArea, deleteArea,
|
||||
getAllCamas, createCama, updateCama, deleteCama,
|
||||
getAllInternaciones, createInternacion, updateInternacion, deleteInternacion,
|
||||
getAllEvoluciones, createEvolucion, updateEvolucion, deleteEvolucion,
|
||||
getAllLaboratorios, createLaboratorio, updateLaboratorio, deleteLaboratorio,
|
||||
getAllGlucemias, createGlucemia, updateGlucemia, deleteGlucemia,
|
||||
getAllAcidosBase, createAcidoBase, updateAcidoBase, deleteAcidoBase,
|
||||
getAllCultivos, createCultivo, updateCultivo, deleteCultivo,
|
||||
getAllEstudiosComplementarios, createEstudioComplementario, updateEstudioComplementario, deleteEstudioComplementario,
|
||||
getAllInterconsultas, createInterconsulta, updateInterconsulta, deleteInterconsulta,
|
||||
getAllAtb, createAtb, updateAtb, deleteAtb,
|
||||
getAllIndicaciones, createIndicacion, updateIndicacion, deleteIndicacion,
|
||||
getAllMovimientosIndicaciones, createMovimientoIndicacion
|
||||
} from './db.js';
|
||||
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
@@ -18,6 +34,7 @@ app.get('/api/state', (req, res) => {
|
||||
internaciones: getAllInternaciones(),
|
||||
evoluciones: getAllEvoluciones(),
|
||||
laboratorios: getAllLaboratorios(),
|
||||
glucemias: getAllGlucemias(),
|
||||
acidosBase: getAllAcidosBase(),
|
||||
cultivos: getAllCultivos(),
|
||||
estudiosComplementarios: getAllEstudiosComplementarios(),
|
||||
@@ -46,6 +63,7 @@ app.put('/api/state', (req, res) => {
|
||||
db.prepare('DELETE FROM acidosbase').run();
|
||||
db.prepare('DELETE FROM cultivos').run();
|
||||
db.prepare('DELETE FROM laboratorios').run();
|
||||
db.prepare('DELETE FROM glucemias').run();
|
||||
db.prepare('DELETE FROM internaciones').run();
|
||||
db.prepare('DELETE FROM camas').run();
|
||||
db.prepare('DELETE FROM areas').run();
|
||||
@@ -98,6 +116,13 @@ app.put('/api/state', (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (state.glucemias?.length) {
|
||||
const stmt = db.prepare('INSERT INTO glucemias (id, pacienteId, internacionId, fecha, hora, valor, correccion) VALUES (?, ?, ?, ?, ?, ?, ?)');
|
||||
for (const g of state.glucemias) {
|
||||
stmt.run(g.id, g.pacienteId, g.internacionId || null, g.fecha, g.hora || null, g.valor, g.correccion);
|
||||
}
|
||||
}
|
||||
|
||||
if (state.acidosBase?.length) {
|
||||
const stmt = db.prepare('INSERT INTO acidosbase (id, pacienteId, fecha, hora, ph, pco2, po2, hco3, be, sato2, lactato, interpretacion, fio2) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
|
||||
for (const a of state.acidosBase) {
|
||||
@@ -226,9 +251,7 @@ app.put('/api/state/partial', (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server running on port ${PORT}`);
|
||||
});
|
||||
export default app;
|
||||
|
||||
// === AUTENTICACIÓN ===
|
||||
|
||||
@@ -328,25 +351,26 @@ app.post('/api/usuarios', (req, res) => {
|
||||
try {
|
||||
const { apellido, nombre, dni, fechaNacimiento, email, rol, matriculaProfesional, password, areaId } = req.body;
|
||||
|
||||
if (!apellido || !nombre || !dni || !fechaNacimiento || !email || !rol || !password) {
|
||||
return res.status(400).json({ error: 'Todos los campos obligatorios deben estar presentes' });
|
||||
if (!apellido?.trim() || !nombre?.trim() || !dni?.trim() || !rol) {
|
||||
return res.status(400).json({ error: 'Apellido, nombre, DNI y rol son obligatorios' });
|
||||
}
|
||||
|
||||
const existente = getUsuarioByDni(dni);
|
||||
const existente = getUsuarioByDni(dni.trim());
|
||||
if (existente) {
|
||||
return res.status(400).json({ error: 'Ya existe un usuario con ese DNI' });
|
||||
}
|
||||
|
||||
const passwordHash = hashPassword(password);
|
||||
const passwordFinal = (password && password.trim()) ? password.trim() : (dni.trim() || '123456');
|
||||
const passwordHash = hashPassword(passwordFinal);
|
||||
const usuario = {
|
||||
id: generateUUID(),
|
||||
apellido,
|
||||
nombre,
|
||||
dni,
|
||||
fechaNacimiento,
|
||||
email,
|
||||
apellido: apellido.trim(),
|
||||
nombre: nombre.trim(),
|
||||
dni: dni.trim(),
|
||||
fechaNacimiento: fechaNacimiento?.trim() || null,
|
||||
email: email?.trim() || null,
|
||||
rol,
|
||||
matriculaProfesional: matriculaProfesional || null,
|
||||
matriculaProfesional: matriculaProfesional?.trim() || null,
|
||||
passwordHash,
|
||||
areaId: areaId || null,
|
||||
fechaCreacion: new Date().toISOString().split('T')[0]
|
||||
@@ -355,7 +379,7 @@ app.post('/api/usuarios', (req, res) => {
|
||||
createUsuario(usuario);
|
||||
res.json({ ...usuario, passwordHash: undefined });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
console.error('Error al crear usuario:', err);
|
||||
res.status(500).json({ error: 'Error al crear usuario' });
|
||||
}
|
||||
});
|
||||
@@ -363,29 +387,37 @@ app.post('/api/usuarios', (req, res) => {
|
||||
app.put('/api/usuarios/:id', (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { apellido, nombre, fechaNacimiento, email, rol, matriculaProfesional, areaId, password } = req.body;
|
||||
const { apellido, nombre, dni, fechaNacimiento, email, rol, matriculaProfesional, areaId, password } = req.body;
|
||||
|
||||
const usuario = getUsuarioById(id);
|
||||
if (!usuario) {
|
||||
return res.status(404).json({ error: 'Usuario no encontrado' });
|
||||
}
|
||||
|
||||
if (dni && dni.trim() !== usuario.dni) {
|
||||
const existente = getUsuarioByDni(dni.trim());
|
||||
if (existente && existente.id !== id) {
|
||||
return res.status(400).json({ error: 'Ya existe otro usuario con ese DNI' });
|
||||
}
|
||||
}
|
||||
|
||||
const datos = {};
|
||||
if (apellido !== undefined) datos.apellido = apellido;
|
||||
if (nombre !== undefined) datos.nombre = nombre;
|
||||
if (fechaNacimiento !== undefined) datos.fechaNacimiento = fechaNacimiento;
|
||||
if (email !== undefined) datos.email = email;
|
||||
if (apellido !== undefined) datos.apellido = apellido.trim();
|
||||
if (nombre !== undefined) datos.nombre = nombre.trim();
|
||||
if (dni !== undefined) datos.dni = dni.trim();
|
||||
if (fechaNacimiento !== undefined) datos.fechaNacimiento = fechaNacimiento?.trim() || null;
|
||||
if (email !== undefined) datos.email = email?.trim() || null;
|
||||
if (rol !== undefined) datos.rol = rol;
|
||||
if (matriculaProfesional !== undefined) datos.matriculaProfesional = matriculaProfesional;
|
||||
if (areaId !== undefined) datos.areaId = areaId;
|
||||
if (password) {
|
||||
datos.passwordHash = hashPassword(password);
|
||||
if (matriculaProfesional !== undefined) datos.matriculaProfesional = matriculaProfesional?.trim() || null;
|
||||
if (areaId !== undefined) datos.areaId = areaId || null;
|
||||
if (password && password.trim()) {
|
||||
datos.passwordHash = hashPassword(password.trim());
|
||||
}
|
||||
|
||||
updateUsuario(id, datos);
|
||||
res.json({ ok: true });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
console.error('Error al actualizar usuario:', err);
|
||||
res.status(500).json({ error: 'Error al actualizar usuario' });
|
||||
}
|
||||
});
|
||||
@@ -404,9 +436,7 @@ app.delete('/api/usuarios/:id', (req, res) => {
|
||||
app.put('/api/camas/:id', (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { estado } = req.body;
|
||||
const db = getDb();
|
||||
db.prepare('UPDATE camas SET estado = ? WHERE id = ?').run(estado, id);
|
||||
updateCama(id, req.body);
|
||||
res.json({ ok: true });
|
||||
} catch (err) {
|
||||
console.error('Error updating cama:', err.message);
|
||||
@@ -414,6 +444,246 @@ app.put('/api/camas/:id', (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// ========== PACIENTES ==========
|
||||
app.get('/api/pacientes', (req, res) => {
|
||||
try { res.json(getAllPacientes()); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.post('/api/pacientes', (req, res) => {
|
||||
try {
|
||||
const paciente = { ...req.body, id: req.body.id || generateUUID() };
|
||||
createPaciente(paciente);
|
||||
res.json(paciente);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.put('/api/pacientes/:id', (req, res) => {
|
||||
try { updatePaciente(req.params.id, req.body); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.delete('/api/pacientes/:id', (req, res) => {
|
||||
try { deletePaciente(req.params.id); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// ========== AREAS ==========
|
||||
app.post('/api/areas', (req, res) => {
|
||||
try {
|
||||
const area = { ...req.body, id: req.body.id || generateUUID() };
|
||||
createArea(area);
|
||||
res.json(area);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.put('/api/areas/:id', (req, res) => {
|
||||
try { updateArea(req.params.id, req.body); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.delete('/api/areas/:id', (req, res) => {
|
||||
try { deleteArea(req.params.id); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// ========== CAMAS ==========
|
||||
app.get('/api/camas', (req, res) => {
|
||||
try { res.json(getAllCamas()); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.post('/api/camas', (req, res) => {
|
||||
try {
|
||||
const cama = { ...req.body, id: req.body.id || generateUUID() };
|
||||
createCama(cama);
|
||||
res.json(cama);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.delete('/api/camas/:id', (req, res) => {
|
||||
try { deleteCama(req.params.id); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// ========== INTERNACIONES ==========
|
||||
app.get('/api/internaciones', (req, res) => {
|
||||
try { res.json(getAllInternaciones()); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.post('/api/internaciones', (req, res) => {
|
||||
try {
|
||||
const internacion = { ...req.body, id: req.body.id || generateUUID(), activa: true };
|
||||
createInternacion(internacion);
|
||||
res.json(internacion);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.put('/api/internaciones/:id', (req, res) => {
|
||||
try { updateInternacion(req.params.id, req.body); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.delete('/api/internaciones/:id', (req, res) => {
|
||||
try { deleteInternacion(req.params.id); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// ========== EVOLUCIONES ==========
|
||||
app.get('/api/evoluciones', (req, res) => {
|
||||
try { res.json(getAllEvoluciones()); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.post('/api/evoluciones', (req, res) => {
|
||||
try {
|
||||
const evolucion = { ...req.body, id: req.body.id || generateUUID() };
|
||||
createEvolucion(evolucion);
|
||||
res.json(evolucion);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.put('/api/evoluciones/:id', (req, res) => {
|
||||
try { updateEvolucion(req.params.id, req.body); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.delete('/api/evoluciones/:id', (req, res) => {
|
||||
try { deleteEvolucion(req.params.id); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// ========== LABORATORIOS ==========
|
||||
app.get('/api/laboratorios', (req, res) => {
|
||||
try { res.json(getAllLaboratorios()); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.post('/api/laboratorios', (req, res) => {
|
||||
try {
|
||||
const laboratorio = { ...req.body, id: req.body.id || generateUUID() };
|
||||
createLaboratorio(laboratorio);
|
||||
res.json(laboratorio);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.put('/api/laboratorios/:id', (req, res) => {
|
||||
try { updateLaboratorio(req.params.id, req.body); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.delete('/api/laboratorios/:id', (req, res) => {
|
||||
try { deleteLaboratorio(req.params.id); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// ========== GLUCEMIAS ==========
|
||||
app.get('/api/glucemias', (req, res) => {
|
||||
try { res.json(getAllGlucemias()); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.post('/api/glucemias', (req, res) => {
|
||||
try {
|
||||
const glucemia = { ...req.body, id: req.body.id || generateUUID() };
|
||||
createGlucemia(glucemia);
|
||||
res.json(glucemia);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.put('/api/glucemias/:id', (req, res) => {
|
||||
try { updateGlucemia(req.params.id, req.body); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.delete('/api/glucemias/:id', (req, res) => {
|
||||
try { deleteGlucemia(req.params.id); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// ========== ACIDOS BASE ==========
|
||||
app.get('/api/acid-os-base', (req, res) => {
|
||||
try { res.json(getAllAcidosBase()); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.post('/api/acid-os-base', (req, res) => {
|
||||
try {
|
||||
const acido = { ...req.body, id: req.body.id || generateUUID() };
|
||||
createAcidoBase(acido);
|
||||
res.json(acido);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.put('/api/acid-os-base/:id', (req, res) => {
|
||||
try { updateAcidoBase(req.params.id, req.body); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.delete('/api/acid-os-base/:id', (req, res) => {
|
||||
try { deleteAcidoBase(req.params.id); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// ========== CULTIVOS ==========
|
||||
app.get('/api/cultivos', (req, res) => {
|
||||
try { res.json(getAllCultivos()); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.post('/api/cultivos', (req, res) => {
|
||||
try {
|
||||
const cultivo = { ...req.body, id: req.body.id || generateUUID() };
|
||||
createCultivo(cultivo);
|
||||
res.json(cultivo);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.put('/api/cultivos/:id', (req, res) => {
|
||||
try { updateCultivo(req.params.id, req.body); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.delete('/api/cultivos/:id', (req, res) => {
|
||||
try { deleteCultivo(req.params.id); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// ========== ESTUDIOS COMPLEMENTARIOS ==========
|
||||
app.get('/api/estudios-complementarios', (req, res) => {
|
||||
try { res.json(getAllEstudiosComplementarios()); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.post('/api/estudios-complementarios', (req, res) => {
|
||||
try {
|
||||
const estudio = { ...req.body, id: req.body.id || generateUUID() };
|
||||
createEstudioComplementario(estudio);
|
||||
res.json(estudio);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.put('/api/estudios-complementarios/:id', (req, res) => {
|
||||
try { updateEstudioComplementario(req.params.id, req.body); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.delete('/api/estudios-complementarios/:id', (req, res) => {
|
||||
try { deleteEstudioComplementario(req.params.id); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// ========== INTERCONSULTAS ==========
|
||||
app.get('/api/interconsultas', (req, res) => {
|
||||
try { res.json(getAllInterconsultas()); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.post('/api/interconsultas', (req, res) => {
|
||||
try {
|
||||
const interconsulta = { ...req.body, id: req.body.id || generateUUID() };
|
||||
createInterconsulta(interconsulta);
|
||||
res.json(interconsulta);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.put('/api/interconsultas/:id', (req, res) => {
|
||||
try { updateInterconsulta(req.params.id, req.body); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.delete('/api/interconsultas/:id', (req, res) => {
|
||||
try { deleteInterconsulta(req.params.id); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// ========== ATB ==========
|
||||
app.get('/api/atb', (req, res) => {
|
||||
try { res.json(getAllAtb()); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.post('/api/atb', (req, res) => {
|
||||
try {
|
||||
const atb = { ...req.body, id: req.body.id || generateUUID() };
|
||||
createAtb(atb);
|
||||
res.json(atb);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.put('/api/atb/:id', (req, res) => {
|
||||
try { updateAtb(req.params.id, req.body); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.delete('/api/atb/:id', (req, res) => {
|
||||
try { deleteAtb(req.params.id); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// ========== INDICACIONES ==========
|
||||
app.get('/api/indicaciones', (req, res) => {
|
||||
try { res.json(getAllIndicaciones()); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.post('/api/indicaciones', (req, res) => {
|
||||
try {
|
||||
const indicacion = { ...req.body, id: req.body.id || generateUUID() };
|
||||
createIndicacion(indicacion);
|
||||
res.json(indicacion);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.put('/api/indicaciones/:id', (req, res) => {
|
||||
try { updateIndicacion(req.params.id, req.body); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.delete('/api/indicaciones/:id', (req, res) => {
|
||||
try { deleteIndicacion(req.params.id); res.json({ ok: true }); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// ========== MOVIMIENTOS INDICACIONES ==========
|
||||
app.get('/api/movimientos-indicaciones', (req, res) => {
|
||||
try { res.json(getAllMovimientosIndicaciones()); } catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
app.post('/api/movimientos-indicaciones', (req, res) => {
|
||||
try {
|
||||
const movimiento = { ...req.body, id: req.body.id || generateUUID() };
|
||||
createMovimientoIndicacion(movimiento);
|
||||
res.json(movimiento);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// Helper para actualizar por DNI
|
||||
function updateUsuarioByDni(dni, datos) {
|
||||
const usuario = getUsuarioByDni(dni);
|
||||
|
||||
Reference in New Issue
Block a user