diff --git a/sip-plus-perinatal/controller/sip-plus.ts b/sip-plus-perinatal/controller/sip-plus.ts index a93249a4..06b26866 100644 --- a/sip-plus-perinatal/controller/sip-plus.ts +++ b/sip-plus-perinatal/controller/sip-plus.ts @@ -4,8 +4,10 @@ import { IPaciente } from '../schemas/paciente'; import { getMatching } from '../service/matchPerinatal'; import { getOrganizacionAndes } from '../service/organizacion'; import { IPerinatal, ISnomedConcept } from 'sip-plus-perinatal/schemas/perinatal'; -import { fakeRequest } from '../config.private'; -import { log } from '@andes/log'; +import { fakeRequest, MONGO_HOST } from '../config.private'; +import { msSipPlusPerinatalLog } from '../logger/msSipPlusPerinatal'; +const log = msSipPlusPerinatalLog.startTrace(); + /** * Obtenemos todos los registros de la prestación @@ -136,7 +138,7 @@ export async function completePacienteSP(pacienteSP: IPaciente, paciente: IPacie } } } catch (error) { - log(fakeRequest, 'microservices:integration:sip-plus', { pacienteSP, paciente, registros, fecha, organizacion }, 'sip-plus:completePacienteSP', null, null, error); + log.error('completePacienteSP:error', pacienteSP, error, fakeRequest); } return newPaciente; @@ -228,7 +230,7 @@ async function completePaciente(pacienteSP: any, paciente: IPaciente) { datosPaciente = await completeData(paciente, datosPaciente, newData); } catch (error) { - + log.error('completePacienteSP:error', paciente, error, fakeRequest); } return datosPaciente; } @@ -258,6 +260,7 @@ async function completeData(allData, dataInit = {}, newData) { } }); } catch (error) { + log.error('completeData:error', allData, error, fakeRequest); } return datos; @@ -385,7 +388,7 @@ async function createMatchControl(registros: any[], embActual, newDatosEmb, fech } } } catch (error) { - + log.error('createMatchControl:error', registros, error, fakeRequest); } return newDatosEmb; } diff --git a/sip-plus-perinatal/logger/msSipPlusPerinatal.ts b/sip-plus-perinatal/logger/msSipPlusPerinatal.ts new file mode 100644 index 00000000..9a61ccac --- /dev/null +++ b/sip-plus-perinatal/logger/msSipPlusPerinatal.ts @@ -0,0 +1,15 @@ +import { logDatabase } from '../config.private'; +import { Logger } from '@andes/log'; +import * as mongoose from 'mongoose'; + +let logs: mongoose.Connection = mongoose.createConnection(); +logs = mongoose.createConnection(logDatabase.log.host, logDatabase.log.options); +export const msSipPlusPerinatalLog = new Logger({ + connection: logs, + type: 'msSipPlusPerinatal', + module: 'perinatal', + application: 'andes', + bucketBy: 'h', + bucketSize: 100, + expiredAt: '3 M' +}); \ No newline at end of file diff --git a/sip-plus-perinatal/package-lock.json b/sip-plus-perinatal/package-lock.json index 8131d87f..2e2da330 100644 --- a/sip-plus-perinatal/package-lock.json +++ b/sip-plus-perinatal/package-lock.json @@ -10,7 +10,7 @@ "license": "ISC", "dependencies": { "@andes/bootstrap": "file:../bootstrap", - "@andes/log": "^1.0.11", + "@andes/log": "^2.2.5", "@andes/match": "^1.1.12", "async": "^2.6.3", "html-entities": "^1.2.1", @@ -1038,10 +1038,12 @@ "link": true }, "node_modules/@andes/log": { - "version": "1.1.5", - "resolved": "https://registry.npmjs.org/@andes/log/-/log-1.1.5.tgz", - "integrity": "sha512-ESXmUJIkPzoOciOp9NlW2hOtqan+klYPyp68xfzNkK5hOFuvIE4ptUpKfFC9M1mqqk8RV9Km7RC27xDTO/qL+Q==", + "version": "2.2.5", + "resolved": "https://registry.npmjs.org/@andes/log/-/log-2.2.5.tgz", + "integrity": "sha512-xcBLBtvOJrMVDOFZmwN1NCu1dtEjw8VccLzTYjPoeqhqIm+aY+xp492pGSsJSIQ9HA5MySBNYz0ULBodC4fQCA==", "dependencies": { + "moment": "^2.24.0", + "mongodb": "^3.2.7", "mongoose": "^5.3.11", "url-parse": "^1.1.9", "zone.js": "^0.8.26" @@ -2707,10 +2709,12 @@ } }, "@andes/log": { - "version": "1.1.5", - "resolved": "https://registry.npmjs.org/@andes/log/-/log-1.1.5.tgz", - "integrity": "sha512-ESXmUJIkPzoOciOp9NlW2hOtqan+klYPyp68xfzNkK5hOFuvIE4ptUpKfFC9M1mqqk8RV9Km7RC27xDTO/qL+Q==", + "version": "2.2.5", + "resolved": "https://registry.npmjs.org/@andes/log/-/log-2.2.5.tgz", + "integrity": "sha512-xcBLBtvOJrMVDOFZmwN1NCu1dtEjw8VccLzTYjPoeqhqIm+aY+xp492pGSsJSIQ9HA5MySBNYz0ULBodC4fQCA==", "requires": { + "moment": "^2.24.0", + "mongodb": "^3.2.7", "mongoose": "^5.3.11", "url-parse": "^1.1.9", "zone.js": "^0.8.26" diff --git a/sip-plus-perinatal/package.json b/sip-plus-perinatal/package.json index 4c8dff5d..86ceb428 100644 --- a/sip-plus-perinatal/package.json +++ b/sip-plus-perinatal/package.json @@ -18,7 +18,7 @@ "license": "ISC", "dependencies": { "@andes/bootstrap": "file:../bootstrap", - "@andes/log": "^1.0.11", + "@andes/log": "^2.2.5", "@andes/match": "^1.1.12", "async": "^2.6.3", "html-entities": "^1.2.1", diff --git a/sip-plus-perinatal/service/matchPerinatal.ts b/sip-plus-perinatal/service/matchPerinatal.ts index b0132107..561b63a2 100644 --- a/sip-plus-perinatal/service/matchPerinatal.ts +++ b/sip-plus-perinatal/service/matchPerinatal.ts @@ -1,48 +1,62 @@ import { IPerinatal } from '../schemas/perinatal'; import { QueryMapping } from '../schemas/query_mapping'; - import { fakeRequest, MONGO_HOST } from '../config.private'; -import { log } from '@andes/log'; - import * as mongoose from 'mongoose'; +import { msSipPlusPerinatalLog } from '../logger/msSipPlusPerinatal'; +const log = msSipPlusPerinatalLog.startTrace(); mongoose.connect(MONGO_HOST, { useUnifiedTopology: true, useNewUrlParser: true }). then(() => console.log('Conexion Exitosa BD Mongo')) .catch(err => { - log(fakeRequest, 'microservices:integration:sip-plus', MONGO_HOST, 'Mongo Conexión:error', `${err.message}`); + log.error('Mongo Conexión:error', MONGO_HOST, `${err.message}`, fakeRequest); }); export async function getMatching(tipoMatch = null) { const tipo: string = tipoMatch || 'paciente'; const source = `andes:${tipo}`; + try { const dataMapped = await QueryMapping.find({ source, target: "sip+" }); + const results = dataMapped + .map(elemMap => createDataMap(elemMap, tipo)) + .filter(Boolean); - return dataMapped.map(elemMap => { + return results; + } catch (error) { + log.error('getMatching:error', source, error, fakeRequest); + return []; + } +} - let sipPlus = { - code: elemMap['targetValue']['code'], - type: elemMap['targetValue']['type'] - } - const key = (tipo.includes('snomed')) ? elemMap['sourceValue']['key'] : elemMap['sourceValue']; - - let dataMap: IPerinatal = { key, sipPlus, tipoMatch }; - // si el tipo de mapeo contiene conceptos Snomed, entonces se obtienen sus datos - if (tipo.includes('snomed')) { - dataMap.concepto = elemMap['sourceValue']['concepto']; - if (elemMap['sourceValue']['valor']) { - dataMap.sipPlus.valor = elemMap['targetValue']['valor']; - } - if (elemMap['targetValue']['extra']) { - dataMap.sipPlus.extra = elemMap['targetValue']['extra']; - } +function createDataMap(elemMap: any, tipo: string): IPerinatal | null { + + try { + const sipPlus = { + code: elemMap['targetValue']['code'], + type: elemMap['targetValue']['type'] + }; + + const key = tipo.includes('snomed') + ? elemMap['sourceValue']['key'] + : elemMap['sourceValue']; + + const dataMap: IPerinatal = { key, sipPlus, tipoMatch: tipo }; + + if (tipo.includes('snomed')) { + dataMap.concepto = elemMap['sourceValue']['concepto']; + + if (elemMap['sourceValue']['valor']) { + dataMap.sipPlus.valor = elemMap['targetValue']['valor']; } - return dataMap; - }); + if (elemMap['targetValue']['extra']) { + dataMap.sipPlus.extra = elemMap['targetValue']['extra']; + } + } + return dataMap; } catch (error) { - log(fakeRequest, 'microservices:integration:sip-plus', source, 'getMatching:error', error); + log.error('createDataMap:error', { elemMap, tipo }, error, fakeRequest); + return null; } - return []; } diff --git a/sip-plus-perinatal/service/organizacion.ts b/sip-plus-perinatal/service/organizacion.ts index 0af22fb9..5918c9ad 100644 --- a/sip-plus-perinatal/service/organizacion.ts +++ b/sip-plus-perinatal/service/organizacion.ts @@ -1,8 +1,8 @@ import { ANDES_HOST, ANDES_KEY, fakeRequest } from '../config.private'; +import { msSipPlusPerinatalLog } from '../logger/msSipPlusPerinatal'; +const log = msSipPlusPerinatalLog.startTrace(); const fetch = require('node-fetch'); -import { log } from '@andes/log'; - export async function getOrganizacionAndes(idOrganizacion) { const url = `${ANDES_HOST}/core/tm/organizaciones?ids=${idOrganizacion}`; const options = { @@ -12,15 +12,14 @@ export async function getOrganizacionAndes(idOrganizacion) { Authorization: `JWT ${ANDES_KEY}` } }; - try { - let response = await fetch(url, options); + try { + const response = await fetch(url, options); const responseJson = await response.json(); - return responseJson[0] || null; - } - catch (error) { - log(fakeRequest, 'microservices:integration:sip-plus', idOrganizacion, 'getOrganizacionAndes:error', error); + } catch (error) { + log.error('getOrganizacionAndes:error', { idOrganizacion, options }, error, fakeRequest); + return null; } -} \ No newline at end of file +} diff --git a/sip-plus-perinatal/service/paciente.ts b/sip-plus-perinatal/service/paciente.ts index c5edaf7e..65a54f61 100644 --- a/sip-plus-perinatal/service/paciente.ts +++ b/sip-plus-perinatal/service/paciente.ts @@ -1,8 +1,8 @@ import { ANDES_HOST, ANDES_KEY, fakeRequest } from '../config.private'; +import { msSipPlusPerinatalLog } from '../logger/msSipPlusPerinatal'; +const log = msSipPlusPerinatalLog.startTrace(); const fetch = require('node-fetch'); -import { log } from '@andes/log'; - export async function getPaciente(idPaciente) { const url = `${ANDES_HOST}/core-v2/mpi/pacientes/${idPaciente}`; const options = { @@ -13,16 +13,24 @@ export async function getPaciente(idPaciente) { } }; try { - let response = await fetch(url, options); - const responseJson = await response.json(); - if (responseJson._id) { - return responseJson; - } else { - return null; + const response = await fetch(url, options); + + if (!response.ok) { + throw new Error(`HTTP ${response.status} - ${response.statusText}`); } - } - catch (error) { - log(fakeRequest, 'microservices:integration:sip-plus', idPaciente, 'getPaciente:error', error); + + const responseJson = await response.json(); + + return responseJson?._id ? responseJson : null; + + } catch (error) { + log.error( + 'getPaciente:error', + { idPaciente, options, url }, + error, + fakeRequest + ); + return null; } -} \ No newline at end of file +} diff --git a/sip-plus-perinatal/service/sip-plus.ts b/sip-plus-perinatal/service/sip-plus.ts index 719ad9ad..6fb9461d 100644 --- a/sip-plus-perinatal/service/sip-plus.ts +++ b/sip-plus-perinatal/service/sip-plus.ts @@ -1,6 +1,8 @@ import { SIP_PLUS, fakeRequest } from '../config.private'; +import { msSipPlusPerinatalLog } from '../logger/msSipPlusPerinatal'; +const log = msSipPlusPerinatalLog.startTrace(); const fetch = require('node-fetch'); -import { log } from '@andes/log'; + const url = `${SIP_PLUS.host}/record/AR/DNI/`; @@ -29,32 +31,37 @@ const options = (method = 'GET', body = null) => { export async function getPacienteSP(paciente: any) { const documento = paciente.documento || ''; - if (documento) { - try { - let response = await fetch(`${url}${documento}`, options('GET')); - - if (response.status >= 200 && response.status < 300) { - let responseJson = await response.json(); + if (!documento) { + return { paciente: null }; + } - const keyResponse = Object.keys(responseJson).length || null; - if (keyResponse) { - return { paciente: responseJson }; - } - else { - return { paciente: null }; + try { + const response = await fetch(`${url}${documento}`, options('GET')); - } - } + if (!response.ok) { if (response.status === 404) { - // paciente no encontrado + // Paciente no encontrado return { paciente: null }; } - } catch (error) { - log(fakeRequest, 'microservices:integration:sip-plus', paciente, 'getPacienteSP:error', error); + throw new Error(`HTTP ${response.status}`); } + + const responseJson = await response.json(); + + return responseJson && Object.keys(responseJson).length + ? { paciente: responseJson } + : { paciente: null }; + + } catch (error) { + log.error( + 'getPacienteSP:error', + { documento, url }, + error, + fakeRequest + ); + return { paciente: null }; } - return null; } export async function postPacienteSP(documento: string = '', pacienteSP) { @@ -63,17 +70,21 @@ export async function postPacienteSP(documento: string = '', pacienteSP) { const body = JSON.stringify(pacienteSP); let optionsPost: any = options('POST', body); let response: any = await fetch(`${url}${documento}`, optionsPost); - if (response.status >= 200 && response.status < 300) { - return { paciente: optionsPost.body }; - } - if (response.status === 404) { - // paciente no encontrado - return { paciente: null }; + + if (!response.ok) { + if (response.status === 404) { + // Paciente no encontrado + return { paciente: null }; + } + + throw new Error(`HTTP ${response.status}`); } + return { paciente: optionsPost.body }; + } catch (error) { - log(fakeRequest, 'microservices:integration:sip-plus', pacienteSP, 'postPacienteSP:error', error); + log.error('postPacienteSP:error', { pacienteSP }, error, fakeRequest); } } return null; -} \ No newline at end of file +}