110 lines
4.1 KiB
JavaScript
110 lines
4.1 KiB
JavaScript
const fs = require('fs');
|
|
let code = fs.readFileSync('server/db.js', 'utf8');
|
|
|
|
// Add computeSector function
|
|
const computeSectorCode = `
|
|
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';
|
|
}
|
|
`;
|
|
|
|
// Find where to insert computeSector
|
|
code = code.replace("export function createCama(cama) {", computeSectorCode + "\nexport function createCama(cama) {");
|
|
|
|
// Update createCama
|
|
code = code.replace(
|
|
/export function createCama\(cama\) \{\s*const gId = cama.grupoId \|\| cama.areaId \|\| null;\s*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\);\s*\}/,
|
|
`export function createCama(cama) {
|
|
const gId = cama.grupoId || cama.areaId || 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);
|
|
}`
|
|
);
|
|
|
|
// Update updateCama
|
|
code = code.replace(
|
|
/export function updateCama\(id, updates\) \{[\s\S]*?if \(fields\.length > 0\) \{/,
|
|
`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);
|
|
}
|
|
if (updates.pacienteId !== undefined) {
|
|
fields.push('pacienteId = ?');
|
|
values.push(updates.pacienteId);
|
|
}
|
|
if (updates.internacionId !== undefined) {
|
|
fields.push('internacionId = ?');
|
|
values.push(updates.internacionId);
|
|
}
|
|
|
|
if (fields.length > 0) {`
|
|
);
|
|
|
|
// Update table schema
|
|
code = code.replace(
|
|
/CREATE TABLE IF NOT EXISTS camas \(\s*id TEXT PRIMARY KEY,\s*numero TEXT NOT NULL,\s*grupoId TEXT,\s*areaId TEXT,\s*tipo TEXT DEFAULT 'Estándar',\s*estado TEXT DEFAULT 'Disponible',\s*pacienteId TEXT,\s*internacionId TEXT\s*\);/,
|
|
`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',
|
|
pacienteId 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) {}`
|
|
);
|
|
|
|
fs.writeFileSync('server/db.js', code);
|