Renombrar 'Areas' a 'Grupos' y actualizar dependencias

This commit is contained in:
2026-08-09 20:05:08 +00:00
parent 91cde0e5d9
commit dc108a9699
12 changed files with 421 additions and 342 deletions
+74 -11
View File
@@ -67,9 +67,15 @@ export function initDb() {
nombre TEXT NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS grupos (
id TEXT PRIMARY KEY,
nombre TEXT NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS camas (
id TEXT PRIMARY KEY,
numero TEXT NOT NULL,
grupoId TEXT,
areaId TEXT,
tipo TEXT DEFAULT 'Estándar',
estado TEXT DEFAULT 'Disponible',
@@ -81,6 +87,7 @@ export function initDb() {
id TEXT PRIMARY KEY,
pacienteId TEXT NOT NULL,
camaId TEXT,
grupoId TEXT,
areaId TEXT,
fechaIngresoHospital TEXT,
fechaIngresoClinica TEXT,
@@ -233,6 +240,36 @@ export function initDb() {
);
`);
// Migration helpers for columns if existing DB
try { db.exec('ALTER TABLE usuarios ADD COLUMN grupoId TEXT;'); } catch (_) {}
try { db.exec('ALTER TABLE camas ADD COLUMN grupoId TEXT;'); } catch (_) {}
try { db.exec('ALTER TABLE internaciones ADD COLUMN grupoId TEXT;'); } catch (_) {}
try { db.exec('ALTER TABLE usuarios ADD COLUMN areaId TEXT;'); } catch (_) {}
try { db.exec('ALTER TABLE camas ADD COLUMN areaId TEXT;'); } catch (_) {}
try { db.exec('ALTER TABLE internaciones ADD COLUMN areaId TEXT;'); } catch (_) {}
// Seed default 4 grupos if empty
try {
const defaultGrupos = [
{ id: 'patron-fondo', nombre: 'Patron Fondo' },
{ id: 'patron-adelante', nombre: 'Patron Adelante' },
{ id: 'segundo-acassuso', nombre: 'Segundo Acassuso' },
{ id: 'tercero-acassuso', nombre: 'Tercero Acassuso' }
];
const grupoCount = db.prepare('SELECT COUNT(*) as count FROM grupos').get();
if (!grupoCount || grupoCount.count === 0) {
const stmtG = db.prepare('INSERT OR IGNORE INTO grupos (id, nombre) VALUES (?, ?)');
const stmtA = db.prepare('INSERT OR IGNORE INTO areas (id, nombre) VALUES (?, ?)');
for (const g of defaultGrupos) {
stmtG.run(g.id, g.nombre);
stmtA.run(g.id, g.nombre);
}
}
} catch (err) {
console.error('Error seeding default grupos:', err);
}
// Create default admin if no users exist
try {
const userCount = db.prepare('SELECT COUNT(*) as count FROM usuarios').get();
@@ -351,11 +388,18 @@ export function getAllPacientes() {
return rows;
}
export function getAllAreas() {
const rows = db.prepare('SELECT * FROM areas').all();
export function getAllGrupos() {
const rows = db.prepare('SELECT * FROM grupos').all();
if (!rows || rows.length === 0) {
return db.prepare('SELECT * FROM areas').all();
}
return rows;
}
export function getAllAreas() {
return getAllGrupos();
}
export function getAllCamas() {
const rows = db.prepare('SELECT * FROM camas').all();
return rows;
@@ -467,24 +511,42 @@ export function deletePaciente(id) {
db.prepare('DELETE FROM pacientes WHERE id = ?').run(id);
}
// Areas
export function createArea(area) {
db.prepare('INSERT INTO areas (id, nombre) VALUES (?, ?)').run(area.id, area.nombre);
// Grupos & Areas
export function createGrupo(grupo) {
const stmtG = db.prepare('INSERT INTO grupos (id, nombre) VALUES (?, ?)');
const stmtA = db.prepare('INSERT INTO areas (id, nombre) VALUES (?, ?)');
stmtG.run(grupo.id, grupo.nombre);
try { stmtA.run(grupo.id, grupo.nombre); } catch (_) {}
}
export function updateArea(id, datos) {
export function updateGrupo(id, datos) {
if (datos.nombre) {
db.prepare('UPDATE areas SET nombre = ? WHERE id = ?').run(datos.nombre, id);
db.prepare('UPDATE grupos SET nombre = ? WHERE id = ?').run(datos.nombre, id);
try { db.prepare('UPDATE areas SET nombre = ? WHERE id = ?').run(datos.nombre, id); } catch (_) {}
}
}
export function deleteGrupo(id) {
db.prepare('DELETE FROM grupos WHERE id = ?').run(id);
try { db.prepare('DELETE FROM areas WHERE id = ?').run(id); } catch (_) {}
}
export function createArea(area) {
createGrupo(area);
}
export function updateArea(id, datos) {
updateGrupo(id, datos);
}
export function deleteArea(id) {
db.prepare('DELETE FROM areas WHERE id = ?').run(id);
deleteGrupo(id);
}
// Camas
export function createCama(cama) {
db.prepare('INSERT INTO camas (id, numero, areaId, tipo, estado, pacienteId, internacionId) VALUES (?, ?, ?, ?, ?, ?, ?)').run(cama.id, cama.numero, cama.areaId || null, cama.tipo || 'Estándar', cama.estado || 'Disponible', cama.pacienteId || null, cama.internacionId || null);
const gId = cama.grupoId || cama.areaId || null;
db.prepare('INSERT INTO camas (id, numero, grupoId, areaId, tipo, estado, pacienteId, internacionId) VALUES (?, ?, ?, ?, ?, ?, ?, ?)').run(cama.id, cama.numero, gId, gId, cama.tipo || 'Estándar', cama.estado || 'Disponible', cama.pacienteId || null, cama.internacionId || null);
}
export function deleteCama(id) {
@@ -493,8 +555,9 @@ export function deleteCama(id) {
// Internaciones
export function createInternacion(i) {
const stmt = db.prepare('INSERT INTO internaciones (id, pacienteId, camaId, areaId, fechaIngresoHospital, fechaIngresoClinica, fechaEgreso, diagnosticoIngreso, motivoConsulta, enfermedadActual, antecedentesEnfermedadActual, diagnosticoEgreso, medicoIngresante, motivoEgreso, activa, apache, derivacion) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
stmt.run(i.id, i.pacienteId, i.camaId || null, i.areaId || null, i.fechaIngresoHospital || null, i.fechaIngresoClinica || null, i.fechaEgreso || null, i.diagnosticoIngreso || null, i.motivoConsulta || null, i.enfermedadActual || null, i.antecedentesEnfermedadActual || null, i.diagnosticoEgreso || null, i.medicoIngresante || null, i.motivoEgreso || null, i.activa ? 1 : 0, i.apache || null, i.derivacion || null);
const gId = i.grupoId || i.areaId || null;
const stmt = db.prepare('INSERT INTO internaciones (id, pacienteId, camaId, grupoId, areaId, fechaIngresoHospital, fechaIngresoClinica, fechaEgreso, diagnosticoIngreso, motivoConsulta, enfermedadActual, antecedentesEnfermedadActual, diagnosticoEgreso, medicoIngresante, motivoEgreso, activa, apache, derivacion) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
stmt.run(i.id, i.pacienteId, i.camaId || null, gId, gId, i.fechaIngresoHospital || null, i.fechaIngresoClinica || null, i.fechaEgreso || null, i.diagnosticoIngreso || null, i.motivoConsulta || null, i.enfermedadActual || null, i.antecedentesEnfermedadActual || null, i.diagnosticoEgreso || null, i.medicoIngresante || null, i.motivoEgreso || null, i.activa ? 1 : 0, i.apache || null, i.derivacion || null);
}
export function updateInternacion(id, datos) {