Implementar lógica de cálculo automático del sector (En Área / Fuera de Área) para Camas según su numeración y almacenar en la BD

This commit is contained in:
2026-08-09 20:17:44 +00:00
parent dc108a9699
commit 4ae43e713a
12 changed files with 319 additions and 47 deletions
+61 -6
View File
@@ -4,12 +4,12 @@ import { fileURLToPath } from 'url';
import bcrypt from 'bcryptjs';
import { existsSync, mkdirSync } from 'fs';
const __dirname = dirname(fileURLToPath(import.meta.url));
const DB_PATH = __dirname + '/data/hospital.db';
const currentDir = typeof currentDir !== 'undefined' ? currentDir : dirname(fileURLToPath(import.meta.url));
const DB_PATH = currentDir + '/data/hospital.db';
// Ensure data directory exists
if (!existsSync(__dirname + '/data')) {
mkdirSync(__dirname + '/data', { recursive: true });
if (!existsSync(currentDir + '/data')) {
mkdirSync(currentDir + '/data', { recursive: true });
}
// Create persistent database connection with WAL mode and proper settings
@@ -80,8 +80,27 @@ export function initDb() {
tipo TEXT DEFAULT 'Estándar',
estado TEXT DEFAULT 'Disponible',
pacienteId TEXT,
internacionId TEXT
internacionId TEXT,
sector TEXT DEFAULT 'En Área'
);
try {
db.prepare("ALTER TABLE camas ADD COLUMN sector TEXT DEFAULT 'En Área'").run();
} catch(e) {}
try {
// Initialize existing beds with their sector
const rows = db.prepare('SELECT id, numero FROM camas WHERE sector IS NULL OR sector = "En Área"').all();
for (const row of rows) {
if (!row.numero) continue;
const salaStr = String(row.numero).trim().split('-')[0].trim();
const salaNum = parseInt(salaStr, 10);
let s = 'En Área';
if (!isNaN(salaNum)) {
if (salaNum >= 300 && salaNum < 400 && salaNum % 2 !== 0) s = 'Fuera de Área';
else if (salaNum >= 400 && salaNum < 500) s = 'Fuera de Área';
}
db.prepare('UPDATE camas SET sector = ? WHERE id = ?').run(s, row.id);
}
} catch(e) {}
CREATE TABLE IF NOT EXISTS internaciones (
id TEXT PRIMARY KEY,
@@ -408,6 +427,23 @@ export function getAllCamas() {
export function updateCama(id, updates) {
const fields = [];
const values = [];
if (updates.numero !== undefined) {
fields.push('numero = ?');
values.push(updates.numero);
fields.push('sector = ?');
values.push(computeSector(updates.numero));
}
if (updates.tipo !== undefined) {
fields.push('tipo = ?');
values.push(updates.tipo);
}
if (updates.grupoId !== undefined) {
fields.push('grupoId = ?');
values.push(updates.grupoId);
fields.push('areaId = ?');
values.push(updates.grupoId);
}
if (updates.estado !== undefined) {
fields.push('estado = ?');
values.push(updates.estado);
@@ -420,6 +456,7 @@ export function updateCama(id, updates) {
fields.push('internacionId = ?');
values.push(updates.internacionId);
}
if (fields.length > 0) {
values.push(id);
db.prepare(`UPDATE camas SET ${fields.join(', ')} WHERE id = ?`).run(values);
@@ -544,9 +581,27 @@ export function deleteArea(id) {
}
// Camas
function computeSector(numero) {
if (!numero) return 'En Área';
const parts = String(numero).trim().split('-');
if (parts.length === 0) return 'En Área';
const salaStr = parts[0].trim();
const salaNum = parseInt(salaStr, 10);
if (isNaN(salaNum)) return 'En Área';
if (salaNum >= 300 && salaNum < 400 && salaNum % 2 !== 0) {
return 'Fuera de Área';
}
if (salaNum >= 400 && salaNum < 500) {
return 'Fuera de Área';
}
return 'En Área';
}
export function createCama(cama) {
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);
const sector = computeSector(cama.numero);
db.prepare('INSERT INTO camas (id, numero, grupoId, areaId, tipo, estado, pacienteId, internacionId, sector) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)').run(cama.id, cama.numero, gId, gId, cama.tipo || 'Estándar', cama.estado || 'Disponible', cama.pacienteId || null, cama.internacionId || null, sector);
}
export function deleteCama(id) {
+6 -4
View File
@@ -95,10 +95,11 @@ app.put('/api/state', (req, res) => {
}
if (state.camas?.length) {
const stmt = db.prepare('INSERT OR IGNORE INTO camas (id, numero, grupoId, areaId, tipo, estado) VALUES (?, ?, ?, ?, ?, ?)');
const stmt = db.prepare('INSERT OR IGNORE INTO camas (id, numero, grupoId, areaId, tipo, estado, sector) VALUES (?, ?, ?, ?, ?, ?, ?)');
for (const c of state.camas) {
const gId = c.grupoId || c.areaId || null;
stmt.run(c.id, c.numero, gId, gId, c.tipo, c.estado);
const sector = computeSector(c.numero);
stmt.run(c.id, c.numero, gId, gId, c.tipo, c.estado, sector);
}
}
@@ -224,9 +225,10 @@ app.put('/api/state/partial', (req, res) => {
if (updates.camas) {
db.prepare('DELETE FROM camas').run();
if (updates.camas.length > 0) {
const stmt = db.prepare('INSERT INTO camas (id, numero, areaId, tipo, estado) VALUES (?, ?, ?, ?, ?)');
const stmt = db.prepare('INSERT INTO camas (id, numero, grupoId, areaId, tipo, estado, sector) VALUES (?, ?, ?, ?, ?, ?, ?)');
for (const c of updates.camas) {
stmt.run(c.id, c.numero, c.areaId, c.tipo, c.estado);
const sector = computeSector(c.numero);
stmt.run(c.id, c.numero, c.grupoId || c.areaId || null, c.grupoId || c.areaId || null, c.tipo, c.estado, sector);
}
}
}