Skip to content

Commit

Permalink
MONIT-60: ABM de restricción por paciente
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanIRamirez authored and negro89 committed Jul 19, 2024
1 parent e13a6fc commit fee1856
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 5 deletions.
19 changes: 14 additions & 5 deletions auth/schemas/authUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ export interface IAuthUsers {
_id: ObjectId;
createdAt: Date;
}[];
pacienteRestringido: Object;
pacienteRestringido: {
idPaciente: string;
archivos: [any];
observaciones: string;
createdBy: any;
createdAt: Date;
}[];
}

export type IAuthUsersDoc = AndesDocWithAudit<IAuthUsers>;
Expand Down Expand Up @@ -85,10 +91,13 @@ export const AuthUsersSchema = new mongoose.Schema({
default: {}
},
disclaimers: [{ createdAt: Date, _id: { type: mongoose.Schema.Types.ObjectId, ref: 'dislaimer' } }],
pacienteRestringido: {
type: Object,
default: null
}
pacienteRestringido: [{
idPaciente: String,
observaciones: String,
archivos: [mongoose.SchemaTypes.Mixed],
createdBy: mongoose.SchemaTypes.Mixed,
createdAt: Date
}]
});

AuthUsersSchema.pre('save', function (next) {
Expand Down
1 change: 1 addition & 0 deletions initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export function initAPI(app: Express) {
app.use('/api/modules', require('./modules/dispositivo/').DispositivoRouter);
app.use('/api/modules', require('./modules/semaforo/').SemaforoRouter);
app.use('/api/modules', require('./modules/constantes').ConstantesRouter);
app.use('/api/modules/restriccion-huds', require('./modules/restriccion-huds').restriccionHudsRouter);

if (configPrivate.hosts.BI_QUERY) {
app.use(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { make_Fs } from '../schemas/restriccionHudsStore';
import { Types } from 'mongoose';
import * as base64_stream from 'base64-stream';
import * as stream from 'stream';

const base64RegExp = /data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,(.*)/;

export async function storeFile(base64, metadata) {
try {
const match = base64.match(base64RegExp);
const mime = match[1];
const data = match[2];
const writePromise = new Promise((resolve) => {
const uniqueId = new Types.ObjectId();
const input = new stream.PassThrough();
const decoder64 = base64_stream.decode();
const ImageFiles = make_Fs();
ImageFiles.writeFile(
{
_id: uniqueId,
filename: uniqueId + '.' + mime.split('/')[1],
contentType: mime,
metadata
}, input.pipe(decoder64),
(error, createdFile) => {
resolve(createdFile);
}
);
input.end(data);
});
const result = await writePromise;
return result;
} catch (e) {
return e;
}
}

export async function readFile(id) {
try {
const ImageFiles = make_Fs();
const idFile = new Types.ObjectId(id);
const contexto = await ImageFiles.findOne({ _id: idFile });
const imageStream = await ImageFiles.readFile({ _id: idFile });
return {
file: contexto,
stream: imageStream
};
} catch (e) {
return e;
}
}

export async function deleteFile(id) {
try {
const ImageFiles = make_Fs();
const idFile = new Types.ObjectId(id);
await ImageFiles.unlink(idFile, (error) => { });
return {
file: idFile
};
} catch (e) {
return e;
}
}
12 changes: 12 additions & 0 deletions modules/restriccion-huds/archivos/schemas/restriccionHudsStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as mongoose from 'mongoose';
const { createBucket } = require('mongoose-gridfs');

export function make_Fs() {
const CDAFilesSchema = createBucket({
collectionName: 'RestriccionHUDSStore',
bucketName: 'RestriccionHUDSStore',
mongooseConnection: mongoose.connection
});

return CDAFilesSchema;
}
2 changes: 2 additions & 0 deletions modules/restriccion-huds/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export { restriccionHudsRouter } from './../restriccion-huds/restriccion-huds.routes';
46 changes: 46 additions & 0 deletions modules/restriccion-huds/restriccion-huds.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { MongoQuery, ResourceBase } from '@andes/core';
import { restriccionHuds } from './restriccion-huds.schema';
import { readFile, storeFile, deleteFile } from './archivos/controller/restriccionHudsStore';

class restriccionHudsResource extends ResourceBase {
Model = restriccionHuds;
resourceName = 'restriccionHuds';
routesEnable = ['get', 'post', 'delete'];
searchFileds = {
id: {
field: '_id',
fn: MongoQuery.equalMatch
},
};
}

export const restriccionHudsCtr = new restriccionHudsResource({});
export const restriccionHudsRouter = restriccionHudsCtr.makeRoutes();

restriccionHudsRouter.get('/store/:id', (req, res, next) => {
readFile(req.params.id).then((data: any) => {
res.contentType(data.file.contentType);
data.stream.on('data', (data2) => {
res.write(data2);
});
data.stream.on('end', () => {
res.end();
});
}).catch(next);
});

restriccionHudsRouter.post('/store', (req, res, next) => {
const file = req.body.file;
const metadata = req.body.metadata;
storeFile(file, metadata).then((data) => {
res.json(data);
}).catch(next);
});

restriccionHudsRouter.delete('/store/:id', async (req, res, next) => {
const id = req.params.id;
deleteFile(id).then((data) => {
res.json(data);
}).catch(next);
});

16 changes: 16 additions & 0 deletions modules/restriccion-huds/restriccion-huds.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as mongoose from 'mongoose';
import { ModuloSchema } from '../../core/tm/schemas/modulos.schema';

export const restriccionHudsSchema = new mongoose.Schema({
id: mongoose.Schema.Types.ObjectId,
fecha: { type: Date, default: Date.now },
titulo: String,
descripcion: String,
modulo: {
type: ModuloSchema
},
imagenes: mongoose.Schema.Types.Mixed,
activa: Boolean
});

export const restriccionHuds = mongoose.model('restriccionHuds', restriccionHudsSchema, 'restriccionHuds');

0 comments on commit fee1856

Please sign in to comment.