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:
+61
-6
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user