144 lines
5.0 KiB
JavaScript
144 lines
5.0 KiB
JavaScript
const fs = require('fs');
|
|
|
|
// 1. UPDATE db-mongodb.js
|
|
let code = fs.readFileSync('server/db-mongodb.js', 'utf8');
|
|
|
|
// add memStore
|
|
code = code.replace("laboratorios: [],", "laboratorios: [],\n otrosLaboratorios: [],");
|
|
|
|
// add methods
|
|
const addMethods = `
|
|
export async function getAllOtrosLaboratorios() {
|
|
if (db) {
|
|
const records = await db.collection('otros-laboratorios').find().toArray();
|
|
return cleanDocs(records);
|
|
}
|
|
return [...memStore.otrosLaboratorios];
|
|
}
|
|
|
|
export async function createOtroLaboratorio(record) {
|
|
const doc = {
|
|
id: record.id,
|
|
pacienteId: record.pacienteId,
|
|
internacionId: record.internacionId,
|
|
fecha: record.fecha,
|
|
hora: record.hora,
|
|
observaciones: record.observaciones,
|
|
createdAt: new Date().toISOString()
|
|
};
|
|
|
|
if (db) {
|
|
await db.collection('otros-laboratorios').insertOne(doc);
|
|
return cleanDoc(doc);
|
|
}
|
|
|
|
memStore.otrosLaboratorios.push(doc);
|
|
return doc;
|
|
}
|
|
|
|
export async function updateOtroLaboratorio(id, updates) {
|
|
if (db) {
|
|
await db.collection('otros-laboratorios').updateOne(
|
|
{ id },
|
|
{ $set: { ...updates, updatedAt: new Date().toISOString() } }
|
|
);
|
|
return { success: true };
|
|
}
|
|
|
|
const idx = memStore.otrosLaboratorios.findIndex(x => x.id === id);
|
|
if (idx !== -1) {
|
|
memStore.otrosLaboratorios[idx] = { ...memStore.otrosLaboratorios[idx], ...updates, updatedAt: new Date().toISOString() };
|
|
return { success: true };
|
|
}
|
|
return { success: false };
|
|
}
|
|
|
|
export async function deleteOtroLaboratorio(id) {
|
|
if (db) {
|
|
await db.collection('otros-laboratorios').deleteOne({ id });
|
|
return { success: true };
|
|
}
|
|
|
|
const idx = memStore.otrosLaboratorios.findIndex(x => x.id === id);
|
|
if (idx !== -1) {
|
|
memStore.otrosLaboratorios.splice(idx, 1);
|
|
return { success: true };
|
|
}
|
|
return { success: false };
|
|
}
|
|
`;
|
|
|
|
code = code + "\n" + addMethods;
|
|
|
|
// exportAllData & importAllData
|
|
code = code.replace("laboratorios: await getAllLaboratorios(),", "laboratorios: await getAllLaboratorios(),\n otrosLaboratorios: await getAllOtrosLaboratorios(),");
|
|
code = code.replace("if (data.laboratorios) memStore.laboratorios = data.laboratorios;", "if (data.laboratorios) memStore.laboratorios = data.laboratorios;\n if (data.otrosLaboratorios) memStore.otrosLaboratorios = data.otrosLaboratorios;");
|
|
|
|
fs.writeFileSync('server/db-mongodb.js', code);
|
|
|
|
|
|
// 2. UPDATE api-mongodb.js
|
|
let apiCode = fs.readFileSync('server/api-mongodb.js', 'utf8');
|
|
|
|
apiCode = apiCode.replace("getAllLaboratorios, createLaboratorio, updateLaboratorio, deleteLaboratorio,", "getAllLaboratorios, createLaboratorio, updateLaboratorio, deleteLaboratorio,\n getAllOtrosLaboratorios, createOtroLaboratorio, updateOtroLaboratorio, deleteOtroLaboratorio,");
|
|
|
|
apiCode = apiCode.replace("laboratorios: (await getAllLaboratorios()).map(l => ({ ...l, resultados: typeof l.resultados === 'string' ? JSON.parse(l.resultados) : l.resultados })),", "laboratorios: (await getAllLaboratorios()).map(l => ({ ...l, resultados: typeof l.resultados === 'string' ? JSON.parse(l.resultados) : l.resultados })),\n otrosLaboratorios: await getAllOtrosLaboratorios(),");
|
|
|
|
const apiMethods = `
|
|
// ========== OTROS LABORATORIOS ==========
|
|
app.get('/api/otros-laboratorios', async (req, res) => {
|
|
try {
|
|
const records = await getAllOtrosLaboratorios();
|
|
res.json(records);
|
|
} catch (error) {
|
|
console.error('Error fetching otros-laboratorios:', error);
|
|
res.status(500).json({ error: 'Failed to fetch otros-laboratorios' });
|
|
}
|
|
});
|
|
|
|
app.post('/api/otros-laboratorios', async (req, res) => {
|
|
try {
|
|
const { pacienteId, fecha, observaciones } = req.body;
|
|
if (!pacienteId || !fecha || !observaciones) {
|
|
return res.status(400).json({ error: 'pacienteId, fecha and observaciones are required' });
|
|
}
|
|
|
|
const record = {
|
|
id: generateUUID(),
|
|
...req.body
|
|
};
|
|
|
|
const newRecord = await createOtroLaboratorio(record);
|
|
res.status(201).json(newRecord);
|
|
} catch (error) {
|
|
console.error('Error creating otro laboratorio:', error);
|
|
res.status(500).json({ error: 'Failed to create otro laboratorio' });
|
|
}
|
|
});
|
|
|
|
app.put('/api/otros-laboratorios/:id', async (req, res) => {
|
|
try {
|
|
const result = await updateOtroLaboratorio(req.params.id, req.body);
|
|
if (!result.success) return res.status(404).json({ error: 'Record not found' });
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('Error updating otro laboratorio:', error);
|
|
res.status(500).json({ error: 'Failed to update otro laboratorio' });
|
|
}
|
|
});
|
|
|
|
app.delete('/api/otros-laboratorios/:id', async (req, res) => {
|
|
try {
|
|
const result = await deleteOtroLaboratorio(req.params.id);
|
|
if (!result.success) return res.status(404).json({ error: 'Record not found' });
|
|
res.json({ message: 'Deleted successfully' });
|
|
} catch (error) {
|
|
console.error('Error deleting otro laboratorio:', error);
|
|
res.status(500).json({ error: 'Failed to delete otro laboratorio' });
|
|
}
|
|
});
|
|
`;
|
|
|
|
apiCode = apiCode.replace("// ========== GLUCEMIAS ==========", apiMethods + "\n// ========== GLUCEMIAS ==========");
|
|
fs.writeFileSync('server/api-mongodb.js', apiCode);
|