diff --git a/cda-laboratorios/controller/import-labs.ts b/cda-laboratorios/controller/import-labs.ts index 634706b4..db870a23 100644 --- a/cda-laboratorios/controller/import-labs.ts +++ b/cda-laboratorios/controller/import-labs.ts @@ -1,11 +1,9 @@ -import { conSql } from '../config.private'; +import { conSql, userScheduler } from '../config.private'; import * as moment from 'moment'; import * as sql from 'mssql'; import { Matching } from '@andes/match'; import * as operations from './operations'; -import * as fs from 'fs'; import { InformeLAB } from '../utils/informes/informe-lab'; -import { userScheduler } from '../config.private'; import { msCDALaboratoriosLog } from '../logger/msCDALaboratorios'; const log = msCDALaboratoriosLog.startTrace(); @@ -81,27 +79,23 @@ export async function importarDatos(paciente) { const resultados = await operations.getImpresionResultados(pool, lab.idProtocolo, lab.idEfector); const informe = new InformeLAB(resultados.recordset[0], resultados.recordset, 'Laboratorio'); - fs.readFile((await informe.informe() as string), async (err, data) => { - - if (err) { return false; } - - const file = 'data:application/pdf;base64,' + data.toString('base64'); - - const dto = { - id: lab.idProtocolo, - organizacion: organizacion._id, - fecha: fecha.toDate(), - tipoPrestacion: '4241000179101', - paciente, - confidencialidad: hiv ? 'R' : 'N', - profesional, - cie10: 'Z01.7', - file, - texto: 'Exámen de Laboratorio' - }; - - return await operations.postCDA(dto); - }); + const base64 = await informe.informeBase64(); + const file = 'data:application/pdf;base64,' + base64; + + const dto = { + id: lab.idProtocolo, + organizacion: organizacion._id, + fecha: fecha.toDate(), + tipoPrestacion: '4241000179101', + paciente, + confidencialidad: hiv ? 'R' : 'N', + profesional, + cie10: 'Z01.7', + file, + texto: 'Exámen de Laboratorio' + }; + + return await operations.postCDA(dto); } } catch (e) { diff --git a/cda-laboratorios/utils/model/informe.class.ts b/cda-laboratorios/utils/model/informe.class.ts index 78196ad5..dfd96dd5 100644 --- a/cda-laboratorios/utils/model/informe.class.ts +++ b/cda-laboratorios/utils/model/informe.class.ts @@ -5,7 +5,7 @@ import * as pdf from 'html-pdf'; import { HTMLComponent } from './html-component.class'; export class InformePDF extends HTMLComponent { - + template = ` @@ -39,19 +39,19 @@ export class InformePDF extends HTMLComponent { style: string; stylesUrl: string[]; - - async informe(options: pdf.CreateOptions = null) { + // devuelve buffer + async informeBase64(options: pdf.CreateOptions = null): Promise { const opciones = { ...this.getDefaultOptions(), ...(options || {}) }; + const html = await this.render(); - return new Promise((resolve, reject) => { - pdf.create(html, opciones).toFile((err, file) => { - if (err) { - return reject(err); - } - return resolve(file.filename); + + return new Promise((resolve, reject) => { + pdf.create(html, opciones).toBuffer((err: any, buffer: Buffer) => { + if (err) return reject(err); + resolve(buffer.toString('base64')); }); }); } @@ -73,7 +73,7 @@ export class InformePDF extends HTMLComponent { data.css = this.renderSCSS(); } this.data = data; -} + } private renderSCSS() { const styles = this.stylesUrl.map((file) => { diff --git a/cda-validator/controller/import-cdaValidators.ts b/cda-validator/controller/import-cdaValidators.ts index dd2e7bb5..b6afe9ee 100644 --- a/cda-validator/controller/import-cdaValidators.ts +++ b/cda-validator/controller/import-cdaValidators.ts @@ -1,7 +1,6 @@ import { InformeCDA } from "../utils/informes/informe-cda"; import { getOrganizacion } from './../service/organizaciones.service'; import { postCDA } from './operations'; -import { readFile } from 'fs'; import { userScheduler } from '../config.private'; import { msCDAValidatorLog } from '../logger/msCDAValidator'; const log = msCDAValidatorLog.startTrace(); @@ -13,30 +12,26 @@ export async function importarCDA(datosCDA, paciente) { const organizacion: any = await getOrganizacion(datosEfector.efectorCodigoSisa, 'guardia'); if (organizacion && organizacion._id && organizacion.nombre) { const informe = new InformeCDA(datosCDA, paciente, organizacion); - readFile((await informe.informe() as string), async (err, data) => { - if (err) { - await log.error('guardia:importarCDA:readFile_informe', { err, datosCDA, paciente }, err.message, userScheduler); - return null; - } - const adjunto64 = 'data:application/pdf;base64,' + data.toString('base64'); - const profesional = { - nombre: datosCDA.medicoResp || 'Sin datos', - apellido: ' ' - }; + const base64 = await informe.informeBase64(); + const adjunto64 = 'data:application/pdf;base64,' + base64; - const dto = { - id: datosCDA.id, - organizacion: organizacion._id, - fecha: datosCDA.fechaIngreso, - tipoPrestacion: '50849002', - paciente, - confidencialidad: 'N', - profesional, - cie10: datosCDA.diagnosticoPrincipal || '', - file: adjunto64 - }; - return await postCDA(dto); - }); + const profesional = { + nombre: datosCDA.medicoResp || 'Sin datos', + apellido: ' ' + }; + + const dto = { + id: datosCDA.id, + organizacion: organizacion._id, + fecha: datosCDA.fechaIngreso, + tipoPrestacion: '50849002', + paciente, + confidencialidad: 'N', + profesional, + cie10: datosCDA.diagnosticoPrincipal || '', + file: adjunto64 + }; + return await postCDA(dto); } else { await log.info('guardia:importarCDA:organizacionInvalida', { info: "Organización Andes incorrecta", organizacionSIPS: datosEfector, organizacionAndes: organizacion }, userScheduler); diff --git a/cda-validator/utils/model/informeCDA.class.ts b/cda-validator/utils/model/informeCDA.class.ts index b8bbe560..881dacca 100644 --- a/cda-validator/utils/model/informeCDA.class.ts +++ b/cda-validator/utils/model/informeCDA.class.ts @@ -35,21 +35,23 @@ export class InformeCDAPDF extends HTMLComponent { style: string; stylesUrl: string[]; - - async informe(options: pdf.CreateOptions = null) { + // devuelve buffer + async informeBase64(options: pdf.CreateOptions = null): Promise { const opciones = { ...this.getDefaultOptions(), ...(options || {}) }; + const html = await this.render(); - return new Promise((resolve, reject) => { + + return new Promise((resolve, reject) => { try { - pdf.create(html, opciones).toFile((err, file) => { + pdf.create(html, opciones).toBuffer((err: any, buffer: Buffer) => { if (err) { log.error('guardia:informe:pdf_toFile', { err }, err.message, userScheduler); return reject(err); } - return resolve(file.filename); + resolve(buffer.toString('base64')); }); } catch (err) { log.error('guardia:informe:pdf_create', { err }, err.message, userScheduler);