feat: bypass administrador en memoria para desarrollo
This commit is contained in:
+90
-6
@@ -34,9 +34,10 @@ export async function initDb() {
|
||||
await db.collection('areas').createIndex({ nombre: 1 }, { unique: true });
|
||||
|
||||
// Check if default admin exists
|
||||
const adminCount = await db.collection('usuarios').countDocuments();
|
||||
if (adminCount === 0) {
|
||||
const adminDni = process.env.ADMIN_DNI || '12345678';
|
||||
const adminDni = process.env.ADMIN_DNI || '12345678';
|
||||
const adminExists = await db.collection('usuarios').findOne({ dni: adminDni });
|
||||
|
||||
if (!adminExists) {
|
||||
const adminPassword = process.env.ADMIN_PASSWORD || 'admin123';
|
||||
const adminHash = hashSync(adminPassword, 10);
|
||||
|
||||
@@ -52,7 +53,7 @@ export async function initDb() {
|
||||
fechaCreacion: new Date().toISOString().split('T')[0]
|
||||
};
|
||||
await db.collection('usuarios').insertOne(adminUser);
|
||||
console.log(`Default admin created: DNI ${adminDni} / Password ${adminPassword}`);
|
||||
console.log(`Default admin created/ensured: DNI ${adminDni} / Password ${adminPassword}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize MongoDB:', error);
|
||||
@@ -73,25 +74,45 @@ function cleanDocs(docs) {
|
||||
|
||||
// ========== USUARIOS ==========
|
||||
export async function getUsuarioByDni(dni) {
|
||||
const user = await db.collection('usuarios').findOne({ dni });
|
||||
const dniStr = String(dni).trim();
|
||||
const adminDni = process.env.ADMIN_DNI || '12345678';
|
||||
|
||||
// Bypass TOTAL de administrador "en memoria" para desarrollo/emergencia
|
||||
if (dniStr === adminDni || dniStr === 'admin') {
|
||||
return {
|
||||
id: 'admin-hardcoded-dev',
|
||||
apellido: 'Administrador',
|
||||
nombre: 'Desarrollo (Memoria)',
|
||||
dni: dniStr,
|
||||
rol: 'admin',
|
||||
fechaCreacion: new Date().toISOString()
|
||||
};
|
||||
}
|
||||
|
||||
if (!db) return null;
|
||||
const user = await db.collection('usuarios').findOne({ dni: dniStr });
|
||||
return cleanDoc(user);
|
||||
}
|
||||
|
||||
export async function getUsuarioById(id) {
|
||||
if (!db) return null;
|
||||
const user = await db.collection('usuarios').findOne({ id });
|
||||
return cleanDoc(user);
|
||||
}
|
||||
|
||||
export async function getAllUsuarios() {
|
||||
if (!db) return [];
|
||||
const users = await db.collection('usuarios').find().sort({ apellido: 1, nombre: 1 }).toArray();
|
||||
return cleanDocs(users);
|
||||
}
|
||||
|
||||
export async function createUsuario(usuario) {
|
||||
if (!db) return;
|
||||
await db.collection('usuarios').insertOne(usuario);
|
||||
}
|
||||
|
||||
export async function updateUsuario(id, datos) {
|
||||
if (!db) return;
|
||||
const updateDoc = {};
|
||||
for (const [key, val] of Object.entries(datos)) {
|
||||
if (val !== undefined) {
|
||||
@@ -104,11 +125,22 @@ export async function updateUsuario(id, datos) {
|
||||
}
|
||||
|
||||
export async function deleteUsuario(id) {
|
||||
if (!db) return;
|
||||
await db.collection('usuarios').deleteOne({ id });
|
||||
}
|
||||
|
||||
export async function verifyPassword(dni, password) {
|
||||
const user = await db.collection('usuarios').findOne({ dni });
|
||||
const dniStr = String(dni).trim();
|
||||
const adminDni = process.env.ADMIN_DNI || '12345678';
|
||||
const adminPassword = process.env.ADMIN_PASSWORD || 'admin123';
|
||||
|
||||
// Bypass de contraseña "en memoria"
|
||||
if ((dniStr === adminDni || dniStr === 'admin') && password === adminPassword) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!db) return false;
|
||||
const user = await db.collection('usuarios').findOne({ dni: dniStr });
|
||||
if (!user) return false;
|
||||
return compareSync(password, user.passwordHash);
|
||||
}
|
||||
@@ -119,20 +151,24 @@ export function hashPassword(password) {
|
||||
|
||||
// ========== PACIENTES ==========
|
||||
export async function getAllPacientes() {
|
||||
if (!db) return [];
|
||||
const pacientes = await db.collection('pacientes').find().toArray();
|
||||
return cleanDocs(pacientes);
|
||||
}
|
||||
|
||||
export async function getPacienteById(id) {
|
||||
if (!db) return null;
|
||||
const paciente = await db.collection('pacientes').findOne({ id });
|
||||
return cleanDoc(paciente);
|
||||
}
|
||||
|
||||
export async function createPaciente(paciente) {
|
||||
if (!db) return;
|
||||
await db.collection('pacientes').insertOne(paciente);
|
||||
}
|
||||
|
||||
export async function updatePaciente(id, datos) {
|
||||
if (!db) return;
|
||||
const updateDoc = {};
|
||||
for (const [key, val] of Object.entries(datos)) {
|
||||
if (key !== 'id' && val !== undefined) {
|
||||
@@ -145,41 +181,49 @@ export async function updatePaciente(id, datos) {
|
||||
}
|
||||
|
||||
export async function deletePaciente(id) {
|
||||
if (!db) return;
|
||||
await db.collection('pacientes').deleteOne({ id });
|
||||
}
|
||||
|
||||
// ========== AREAS ==========
|
||||
export async function getAllAreas() {
|
||||
if (!db) return [];
|
||||
const areas = await db.collection('areas').find().toArray();
|
||||
return cleanDocs(areas);
|
||||
}
|
||||
|
||||
export async function createArea(area) {
|
||||
if (!db) return;
|
||||
await db.collection('areas').insertOne(area);
|
||||
}
|
||||
|
||||
export async function updateArea(id, datos) {
|
||||
if (!db) return;
|
||||
if (datos.nombre !== undefined) {
|
||||
await db.collection('areas').updateOne({ id }, { $set: { nombre: datos.nombre } });
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteArea(id) {
|
||||
if (!db) return;
|
||||
await db.collection('areas').deleteOne({ id });
|
||||
}
|
||||
|
||||
// ========== CAMAS ==========
|
||||
export async function getAllCamas() {
|
||||
if (!db) return [];
|
||||
const camas = await db.collection('camas').find().toArray();
|
||||
return cleanDocs(camas);
|
||||
}
|
||||
|
||||
export async function getCamaById(id) {
|
||||
if (!db) return null;
|
||||
const cama = await db.collection('camas').findOne({ id });
|
||||
return cleanDoc(cama);
|
||||
}
|
||||
|
||||
export async function createCama(cama) {
|
||||
if (!db) return;
|
||||
const id = cama.id || generateUUID();
|
||||
const doc = {
|
||||
id,
|
||||
@@ -194,6 +238,7 @@ export async function createCama(cama) {
|
||||
}
|
||||
|
||||
export async function updateCama(id, updates) {
|
||||
if (!db) return;
|
||||
const updateDoc = {};
|
||||
for (const [key, val] of Object.entries(updates)) {
|
||||
if (val !== undefined) {
|
||||
@@ -206,22 +251,26 @@ export async function updateCama(id, updates) {
|
||||
}
|
||||
|
||||
export async function deleteCama(id) {
|
||||
if (!db) return;
|
||||
await db.collection('camas').deleteOne({ id });
|
||||
}
|
||||
|
||||
// ========== INTERNACIONES ==========
|
||||
export async function getAllInternaciones() {
|
||||
if (!db) return [];
|
||||
const internaciones = await db.collection('internaciones').find().toArray();
|
||||
return cleanDocs(internaciones).map(i => ({ ...i, activa: !!i.activa }));
|
||||
}
|
||||
|
||||
export async function getInternacionById(id) {
|
||||
if (!db) return null;
|
||||
const internacion = await db.collection('internaciones').findOne({ id });
|
||||
if (internacion) internacion.activa = !!internacion.activa;
|
||||
return cleanDoc(internacion);
|
||||
}
|
||||
|
||||
export async function createInternacion(internacion) {
|
||||
if (!db) return;
|
||||
const doc = {
|
||||
id: internacion.id,
|
||||
pacienteId: internacion.pacienteId,
|
||||
@@ -245,6 +294,7 @@ export async function createInternacion(internacion) {
|
||||
}
|
||||
|
||||
export async function updateInternacion(id, datos) {
|
||||
if (!db) return;
|
||||
const updateDoc = {};
|
||||
for (const [key, val] of Object.entries(datos)) {
|
||||
if (val !== undefined) {
|
||||
@@ -261,16 +311,19 @@ export async function updateInternacion(id, datos) {
|
||||
}
|
||||
|
||||
export async function deleteInternacion(id) {
|
||||
if (!db) return;
|
||||
await db.collection('internaciones').deleteOne({ id });
|
||||
}
|
||||
|
||||
// ========== EVOLUCIONES ==========
|
||||
export async function getAllEvoluciones() {
|
||||
if (!db) return [];
|
||||
const evoluciones = await db.collection('evoluciones').find().toArray();
|
||||
return cleanDocs(evoluciones);
|
||||
}
|
||||
|
||||
export async function createEvolucion(evolucion) {
|
||||
if (!db) return;
|
||||
const doc = {
|
||||
id: evolucion.id,
|
||||
internacionId: evolucion.internacionId,
|
||||
@@ -287,6 +340,7 @@ export async function createEvolucion(evolucion) {
|
||||
}
|
||||
|
||||
export async function updateEvolucion(id, datos) {
|
||||
if (!db) return;
|
||||
const updateDoc = {};
|
||||
for (const [key, val] of Object.entries(datos)) {
|
||||
if (val !== undefined) {
|
||||
@@ -305,16 +359,19 @@ export async function updateEvolucion(id, datos) {
|
||||
}
|
||||
|
||||
export async function deleteEvolucion(id) {
|
||||
if (!db) return;
|
||||
await db.collection('evoluciones').deleteOne({ id });
|
||||
}
|
||||
|
||||
// ========== LABORATORIOS ==========
|
||||
export async function getAllLaboratorios() {
|
||||
if (!db) return [];
|
||||
const laboratorios = await db.collection('laboratorios').find().toArray();
|
||||
return cleanDocs(laboratorios);
|
||||
}
|
||||
|
||||
export async function createLaboratorio(laboratorio) {
|
||||
if (!db) return;
|
||||
const doc = {
|
||||
id: laboratorio.id,
|
||||
pacienteId: laboratorio.pacienteId,
|
||||
@@ -330,6 +387,7 @@ export async function createLaboratorio(laboratorio) {
|
||||
}
|
||||
|
||||
export async function updateLaboratorio(id, datos) {
|
||||
if (!db) return;
|
||||
const updateDoc = {};
|
||||
for (const [key, val] of Object.entries(datos)) {
|
||||
if (val !== undefined) {
|
||||
@@ -346,16 +404,19 @@ export async function updateLaboratorio(id, datos) {
|
||||
}
|
||||
|
||||
export async function deleteLaboratorio(id) {
|
||||
if (!db) return;
|
||||
await db.collection('laboratorios').deleteOne({ id });
|
||||
}
|
||||
|
||||
// ========== ACIDOS BASE ==========
|
||||
export async function getAllAcidosBase() {
|
||||
if (!db) return [];
|
||||
const acidos = await db.collection('acidosbase').find().toArray();
|
||||
return cleanDocs(acidos);
|
||||
}
|
||||
|
||||
export async function createAcidoBase(acido) {
|
||||
if (!db) return;
|
||||
const doc = {
|
||||
id: acido.id,
|
||||
pacienteId: acido.pacienteId,
|
||||
@@ -376,16 +437,19 @@ export async function createAcidoBase(acido) {
|
||||
}
|
||||
|
||||
export async function deleteAcidoBase(id) {
|
||||
if (!db) return;
|
||||
await db.collection('acidosbase').deleteOne({ id });
|
||||
}
|
||||
|
||||
// ========== CULTIVOS ==========
|
||||
export async function getAllCultivos() {
|
||||
if (!db) return [];
|
||||
const cultivos = await db.collection('cultivos').find().toArray();
|
||||
return cleanDocs(cultivos);
|
||||
}
|
||||
|
||||
export async function createCultivo(cultivo) {
|
||||
if (!db) return;
|
||||
const doc = {
|
||||
id: cultivo.id,
|
||||
pacienteId: cultivo.pacienteId,
|
||||
@@ -404,6 +468,7 @@ export async function createCultivo(cultivo) {
|
||||
}
|
||||
|
||||
export async function updateCultivo(id, datos) {
|
||||
if (!db) return;
|
||||
const updateDoc = {};
|
||||
for (const [key, val] of Object.entries(datos)) {
|
||||
if (val !== undefined) {
|
||||
@@ -416,16 +481,19 @@ export async function updateCultivo(id, datos) {
|
||||
}
|
||||
|
||||
export async function deleteCultivo(id) {
|
||||
if (!db) return;
|
||||
await db.collection('cultivos').deleteOne({ id });
|
||||
}
|
||||
|
||||
// ========== ESTUDIOS COMPLEMENTARIOS ==========
|
||||
export async function getAllEstudiosComplementarios() {
|
||||
if (!db) return [];
|
||||
const estudios = await db.collection('estudiosComplementarios').find().toArray();
|
||||
return cleanDocs(estudios);
|
||||
}
|
||||
|
||||
export async function createEstudioComplementario(estudio) {
|
||||
if (!db) return;
|
||||
const doc = {
|
||||
id: estudio.id,
|
||||
pacienteId: estudio.pacienteId,
|
||||
@@ -438,16 +506,19 @@ export async function createEstudioComplementario(estudio) {
|
||||
}
|
||||
|
||||
export async function deleteEstudioComplementario(id) {
|
||||
if (!db) return;
|
||||
await db.collection('estudiosComplementarios').deleteOne({ id });
|
||||
}
|
||||
|
||||
// ========== INTERCONSULTAS ==========
|
||||
export async function getAllInterconsultas() {
|
||||
if (!db) return [];
|
||||
const interconsultas = await db.collection('interconsultas').find().toArray();
|
||||
return cleanDocs(interconsultas).map(ic => ({ ...ic, realizada: !!ic.realizada }));
|
||||
}
|
||||
|
||||
export async function createInterconsulta(ic) {
|
||||
if (!db) return;
|
||||
const doc = {
|
||||
id: ic.id,
|
||||
pacienteId: ic.pacienteId,
|
||||
@@ -462,6 +533,7 @@ export async function createInterconsulta(ic) {
|
||||
}
|
||||
|
||||
export async function updateInterconsulta(id, datos) {
|
||||
if (!db) return;
|
||||
const updateDoc = {};
|
||||
for (const [key, val] of Object.entries(datos)) {
|
||||
if (val !== undefined) {
|
||||
@@ -474,16 +546,19 @@ export async function updateInterconsulta(id, datos) {
|
||||
}
|
||||
|
||||
export async function deleteInterconsulta(id) {
|
||||
if (!db) return;
|
||||
await db.collection('interconsultas').deleteOne({ id });
|
||||
}
|
||||
|
||||
// ========== ATB ==========
|
||||
export async function getAllAtb() {
|
||||
if (!db) return [];
|
||||
const atb = await db.collection('atb').find().toArray();
|
||||
return cleanDocs(atb);
|
||||
}
|
||||
|
||||
export async function createAtb(atb) {
|
||||
if (!db) return;
|
||||
const doc = {
|
||||
id: atb.id,
|
||||
pacienteId: atb.pacienteId,
|
||||
@@ -496,16 +571,19 @@ export async function createAtb(atb) {
|
||||
}
|
||||
|
||||
export async function deleteAtb(id) {
|
||||
if (!db) return;
|
||||
await db.collection('atb').deleteOne({ id });
|
||||
}
|
||||
|
||||
// ========== INDICACIONES ==========
|
||||
export async function getAllIndicaciones() {
|
||||
if (!db) return [];
|
||||
const indicaciones = await db.collection('indicaciones').find().toArray();
|
||||
return cleanDocs(indicaciones);
|
||||
}
|
||||
|
||||
export async function createIndicacion(ind) {
|
||||
if (!db) return;
|
||||
const doc = {
|
||||
id: ind.id,
|
||||
internacionId: ind.internacionId,
|
||||
@@ -532,6 +610,7 @@ export async function createIndicacion(ind) {
|
||||
}
|
||||
|
||||
export async function updateIndicacion(id, datos) {
|
||||
if (!db) return;
|
||||
const updateDoc = {};
|
||||
for (const [key, val] of Object.entries(datos)) {
|
||||
if (val !== undefined) {
|
||||
@@ -544,16 +623,19 @@ export async function updateIndicacion(id, datos) {
|
||||
}
|
||||
|
||||
export async function deleteIndicacion(id) {
|
||||
if (!db) return;
|
||||
await db.collection('indicaciones').deleteOne({ id });
|
||||
}
|
||||
|
||||
// ========== MOVIMIENTOS INDICACIONES ==========
|
||||
export async function getAllMovimientosIndicaciones() {
|
||||
if (!db) return [];
|
||||
const movimientos = await db.collection('movimientos_indicaciones').find().toArray();
|
||||
return cleanDocs(movimientos);
|
||||
}
|
||||
|
||||
export async function createMovimientoIndicacion(mov) {
|
||||
if (!db) return;
|
||||
const doc = {
|
||||
id: mov.id,
|
||||
indicacionId: mov.indicacionId,
|
||||
@@ -569,11 +651,13 @@ export async function createMovimientoIndicacion(mov) {
|
||||
|
||||
// ========== KV STORE ==========
|
||||
export async function getValue(key) {
|
||||
if (!db) return null;
|
||||
const doc = await db.collection('kv').findOne({ key });
|
||||
return doc?.value ?? null;
|
||||
}
|
||||
|
||||
export async function setValue(key, value) {
|
||||
if (!db) return;
|
||||
await db.collection('kv').updateOne(
|
||||
{ key },
|
||||
{ $set: { key, value } },
|
||||
|
||||
Reference in New Issue
Block a user