Skip to content

Commit

Permalink
AM - Renovación de matricula Foto y Firma
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio-Ramirez committed Dec 26, 2024
1 parent 3d11d3d commit 0dc8661
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 43 deletions.
141 changes: 132 additions & 9 deletions core/tm/controller/profesional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import * as stream from 'stream';
import { userScheduler } from '../../../config.private';
import * as turno from '../../../modules/matriculaciones/schemas/turno';
import { turnoSolicitado } from '../../../modules/matriculaciones/schemas/turnoSolicitado';
import { makeFs } from '../schemas/imagenes';
import { makeFsFirmaAdmin } from '../schemas/firmaAdmin';
import { makeFsFirma } from '../schemas/firmaProf';
import { makeFsFirmaOnline } from '../schemas/firmaRenovacionOnline';
import { makeFsFirmaAdminOnline } from '../schemas/firmaAdminRenovacionOnline';
import { makeFsImagenOnline } from '../schemas/imagenRenovacionOnline';
import { Profesional } from '../schemas/profesional';
import { Auth } from './../../../auth/auth.class';
import { findUsersByUsername } from './../../../auth/auth.controller';
Expand Down Expand Up @@ -192,23 +196,49 @@ export async function saveFirma(data, admin = false) {
const input = new stream.PassThrough();
let firma;
let metadataFind;
let metadataFindOnline;
let metadataWrite;

if (admin) {
firma = makeFsFirmaAdmin();
metadataFind = { 'metadata.idSupervisor': data.idSupervisor };
metadataWrite = {
idSupervisor: data.idSupervisor,
administracion: data.nombreCompleto
};
if (data.option === 'renovacionOnline') {
if (admin) {
firma = makeFsFirmaAdminOnline();
metadataFind = {
'metadata.idProfesional': data.idProfesional,
'metadata.matricula': data.matricula
};
metadataWrite = {
idProfesional: data.idProfesional,
matricula: data.matricula
};
} else {
firma = makeFsFirmaOnline();

metadataFind = {
'metadata.idProfesional': data.idProfesional,
'metadata.matricula': data.matricula
};
metadataWrite = {
idProfesional: data.idProfesional,
matricula: data.matricula
};
}
} else {
firma = makeFsFirma();

metadataFindOnline = {
'metadata.idProfesional': data.idProfesional,
'metadata.matricula': data.matricula
};
metadataFind = { 'metadata.idProfesional': data.idProfesional };
metadataWrite = { idProfesional: data.idProfesional };
if (admin) {
firma = makeFsFirmaAdmin();
} else {
firma = makeFsFirma();
}
}

// Remueve la firma anterior antes de insertar la nueva
const fileFirma = await firma.findOne(metadataFind);

if (fileFirma?._id) {
await firma.unlink(fileFirma._id, (error) => { });
}
Expand All @@ -231,6 +261,99 @@ export async function saveFirma(data, admin = false) {
});
}

export async function deleteFirmaFotoTemporal(idProfesional, matricula, next) {

const metadataFind = {
'metadata.idProfesional': idProfesional,
'metadata.matricula': matricula
};

try {
const firma = makeFsFirmaOnline();

// Remueve la firma anterior antes de insertar la nueva
const fileFirma = await firma.findOne(metadataFind);
if (fileFirma?._id) {
await new Promise<void>((resolve, reject) => {
firma.unlink(fileFirma._id, (error) => {
if (error) {
return reject(error);
}
resolve();
});
});
}

const imagen = makeFsImagenOnline();
// Remueve la imagen anterior antes de insertar la nueva
const fileImagen = await imagen.findOne(metadataFind);
if (fileImagen?._id) {
await new Promise<void>((resolve, reject) => {
imagen.unlink(fileImagen._id, (error) => {
if (error) {
return reject(error);
}
resolve();
});
});
}
} catch (error) {
return next(error);
}

}

export async function saveImage(data) {

const _base64 = data.img;
const decoder = base64.decode();
const input = new stream.PassThrough();
let fotoProf;
let metadataFind;
let metadataFindOnline;
let metadataWrite;

if (data.option === 'renovacionOnline') {
fotoProf = makeFsImagenOnline();

metadataFind = {
'metadata.idProfesional': data.idProfesional,
'metadata.matricula': data.matricula
};
metadataWrite = {
idProfesional: data.idProfesional,
matricula: data.matricula
};
} else {
metadataFind = { 'metadata.idProfesional': data.idProfesional };
metadataWrite = { idProfesional: data.idProfesional };
fotoProf = makeFs();
}

const file = await fotoProf.findOne(metadataFind);

if (file?._id) {
await fotoProf.unlink(file._id, (error) => { });
}
// Inserta en la bd en files y chunks
return new Promise((resolve, reject) => {
fotoProf.writeFile(
{
filename: 'foto.png',
contentType: 'image/png',
metadata: metadataWrite
}, input.pipe(decoder),
(error, createdFile) => {
if (error) {
reject(error);
}
resolve(createdFile);
}
);
input.end(_base64);
});
}

export async function filtrarProfesionalesPorPrestacion(profesionales, prestaciones, organizacionId) {
const usuarios = await findUsersByUsername(profesionales.map(p => p.documento));

Expand Down
94 changes: 60 additions & 34 deletions core/tm/routes/profesional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import { Auth } from '../../../auth/auth.class';
import { sendSms } from '../../../utils/roboSender/sendSms';
import { makePattern, toArray } from '../../../utils/utils';
import { streamToBase64 } from '../controller/file-storage';
import { formacionCero, matriculaCero, migrarTurnos, saveFirma, filtrarProfesionalesPorPrestacion } from '../controller/profesional';
import { formacionCero, matriculaCero, migrarTurnos, saveFirma, filtrarProfesionalesPorPrestacion, saveImage, deleteFirmaFotoTemporal } from '../controller/profesional';
import { makeFsFirmaAdmin } from '../schemas/firmaAdmin';
import { makeFsFirma } from '../schemas/firmaProf';
import { makeFsFirmaOnline } from '../schemas/firmaRenovacionOnline';
import { makeFsImagenOnline } from '../schemas/imagenRenovacionOnline';
import { makeFs } from '../schemas/imagenes';
import { Organizacion } from '../schemas/organizacion';
import { Profesional } from '../schemas/profesional';
Expand Down Expand Up @@ -250,26 +252,59 @@ router.get('/profesionales/foto/:id*?', Auth.authenticate(), async (req: any, re
const img = '/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCACgAKADAREAAhEBAxEB/8QAHQABAAEFAQEBAAAAAAAAAAAAAAkBBAUHCAoGA//EAD4QAAEEAQMBBQMHCwQDAAAAAAEAAgMEBQYHEQgJEiExQRNRYSIyQlJxgZEUFRYZI1ZigpWh0xeDo7MlM3L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s();
const file = await fotoProf.findOne({ 'metadata.idProfesional': id });
if (file) {
const readStream = await fotoProf.readFile({ _id: file._id });
const _img = await streamToBase64(readStream);
return res.json(_img);
if (req.query.id) {
const idProf = req.query.id;
let fotoProf;
let metadataFind;

if (req.query.matricula) {
const matricula = parseInt(req.query.matricula, 10);
fotoProf = makeFsImagenOnline();
metadataFind = {
'metadata.idProfesional': idProf,
'metadata.matricula': matricula
};
} else {
fotoProf = makeFs();
metadataFind = {
'metadata.idProfesional': idProf
};
}

const file = await fotoProf.findOne(metadataFind);
if (file) {
const readStream = await fotoProf.readFile({ _id: file._id });
const _img = await streamToBase64(readStream);
return res.json(_img);
}
return res.json(img);
}
return res.json(img);

} catch (ex) {
return next(ex);
}

});
router.get('/profesionales/firma', Auth.authenticate(), async (req: any, res, next) => {

try {
if (req.query.id) {
const id = req.query.id;
const fotoProf = makeFsFirma();
const file = await fotoProf.findOne({ 'metadata.idProfesional': id });
let fotoProf;
let metadataFind;

if (req.query.matricula) {
const matricula = parseInt(req.query.matricula, 10);
fotoProf = makeFsFirmaOnline();
metadataFind = {
'metadata.idProfesional': id,
'metadata.matricula': matricula
};
} else {
fotoProf = makeFsFirma();
metadataFind = {
'metadata.idProfesional': id
};
}
const file = await fotoProf.findOne(metadataFind);
if (file) {
const readStream = await fotoProf.readFile({ _id: file._id });
const firma = await streamToBase64(readStream);
Expand Down Expand Up @@ -970,28 +1005,8 @@ router.patch('/profesionales/update/:id?', Auth.authenticate(), async (req, res,
try {

if (req.body.imagen) {
const _base64 = req.body.imagen.img;
const decoder = base64.decode();
const input = new stream.PassThrough();
const fotoProf = makeFs();
// remove la foto vieja antes de insertar la nueva
const file = await fotoProf.findOne({
'metadata.idProfesional': req.body.imagen.idProfesional
});
if (file?._id) {
await fotoProf.unlink(file._id, (error) => { });
}
// inserta en la bd en files y chucks
fotoProf.writeFile({
filename: 'foto.png',
contentType: 'image/png',
metadata: {
idProfesional: req.body.imagen.idProfesional,
}
}, input.pipe(decoder), (error, createdFile) => {
res.json(createdFile);
});
input.end(_base64);
const response = await saveImage(req.body.imagen);
res.json(response);
}
if (req.body.firma) {
const response = await saveFirma(req.body.firma);
Expand Down Expand Up @@ -1175,6 +1190,17 @@ router.patch('/profesionales/:id?', Auth.authenticate(), async (req, res, next)
if (req.body.foto) {
resultado.foto = req.body.foto;
}
if (req.body.matricula) {

if (req.body.firmaP) {
await saveFirma(req.body);
}
if (req.body.img) {
await saveImage(req.body);
}
await deleteFirmaFotoTemporal(req.params.id, req.body.matricula, next);

}
}
for (const key in req.body) {
resultado[key] = req.body[key];
Expand Down
13 changes: 13 additions & 0 deletions core/tm/schemas/firmaAdminRenovacionOnline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

import * as mongoose from 'mongoose';
const { createBucket } = require('mongoose-gridfs');

export function makeFsFirmaAdminOnline() {
const ProfesionalesFirmaAdminRenovacionOnlineSchema = createBucket({
bucketName: 'ProfesionalesAdminFirmaRenovacionOnline',
collectionName: 'ProfesionalesAdminFirmaRenovacionOnline',
mongooseConnection: mongoose.connection
});
// obtain a model
return ProfesionalesFirmaAdminRenovacionOnlineSchema;
}
12 changes: 12 additions & 0 deletions core/tm/schemas/firmaRenovacionOnline.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 makeFsFirmaOnline() {
const ProfesionalesFirmaRenovacionOnlineSchema = createBucket({
bucketName: 'ProfesionalesFirmaRenovacionOnline',
collectionName: 'ProfesionalesFirmaRenovacionOnline',
mongooseConnection: mongoose.connection
});
return ProfesionalesFirmaRenovacionOnlineSchema;
}
12 changes: 12 additions & 0 deletions core/tm/schemas/imagenRenovacionOnline.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 makeFsImagenOnline() {
const ProfesionalesImagenRenovacionOnlineSchema = createBucket({
bucketName: 'ProfesionalesImagenRenovacionOnline',
collectionName: 'ProfesionalesImagenRenovacionOnline',
mongooseConnection: mongoose.connection
});
return ProfesionalesImagenRenovacionOnlineSchema;
}

0 comments on commit 0dc8661

Please sign in to comment.