diff --git a/bus-gateway/controllers/iti65.js b/bus-gateway/controllers/iti65.js index ffcc642..5e7b586 100644 --- a/bus-gateway/controllers/iti65.js +++ b/bus-gateway/controllers/iti65.js @@ -3,10 +3,10 @@ const createError = require('http-errors'); const config = require('../config'); const { getBusToken, createBusRequest } = require('../utils/busAuth'); const { findPatient } = require('../services/patient'); -const { createDocumentReference, findDocumentReferenceById, findDocumentReferenceByUrl } = require('../services/documentReference'); +const { createDocumentReference, findDocumentReferenceById } = require('../services/documentReference'); const { logger } = require('../utils/logger'); const { v4: uuidv4 } = require('uuid'); -const { getResourceByUrl } = require('../services/fhir'); +const { getResourceByUrl, processDocumentBundleTransaction } = require('../services/fhir'); const DOCUMENT_REFERENCE_RESOURCE_TYPE = "DocumentReference"; @@ -22,7 +22,7 @@ const MASTER_ID_SYSTEM = 'urn:ietf:rfc:3986' function extractResource(resources, resourceType) { - return resources.map(r => r.data).filter(r => r.resourceType === resourceType)[0]; + return resources.filter(r => r.resourceType === resourceType)[0]; } function extractLocalIdentifier(patient) { @@ -68,6 +68,13 @@ function generateDocumentReferenceResource(subjectReference, bundleUrl) { return documentRefernece; } +async function getResourcesFromTransactionResponse(transactionResponse) { + const promises = transactionResponse.entry.map(async (e) => { + const resource = getResourceByUrl(`${config.fhir.url}/${e.response.location}`); + return resource; + }); + return Promise.all(promises); +} /** * ITI-65: Provide Document Bundle (MHD) @@ -93,28 +100,29 @@ async function provideDocumentBundle(req, res, next) { ); // Paso 1: Ejecuto la transcción en el servidor hapi subyacente - const transactionResponse = await processTransaction(transaction); - const resources = []; - transactionResponse.entry.map(e => e.response); - transactionResponse.entry.map(e => e.response).foreEach(r => { - const resource = getResourceByUr(r.location); - resources.push(resource); + const transactionResponse = await processDocumentBundleTransaction(transaction); + + const resources = await getResourcesFromTransactionResponse(transactionResponse); - }); // Paso 2: Obtengo el paciente local (tal como se guardo en la trasnaccion) const localPatient = extractResource(resources, PATIENT_RESOURCE_TYPE); // Paso 3: Obtengo el documento IPS local (tal como se guardo en la transacción) const localIPSDocument = extractResource(resources, IPS_DOCUMENT_RESOURCE_TYPE) const localPatientIdentifier = extractLocalIdentifier(localPatient); - const patientSearchset = await findPatient(token, { identifier: `${localIdentifier.system}|${localIdentifier.value}` }); + + const patientSearchset = await findPatient(token, { identifier: `${localPatientIdentifier.system}|${localPatientIdentifier.value}` }); if (patientSearchset.total == 0) { throw createError(404, 'Patient does not exists'); } const nationalPatientId = patientSearchset.entry[0].fullUrl; const bundleReference = `${config.baseURL}/fhir/Bundle/${localIPSDocument.id}`; - const createdDocumentReference = await createDocumentReference(token, createdDocumentReference); + + const documentReference = generateDocumentReferenceResource(nationalPatientId, bundleReference); + + const createdDocumentReference = await createDocumentReference(token, documentReference); + return res.status(200).json(createdDocumentReference); } catch (err) { diff --git a/bus-gateway/controllers/iti67.js b/bus-gateway/controllers/iti67.js index 6f5e626..68c1c6b 100644 --- a/bus-gateway/controllers/iti67.js +++ b/bus-gateway/controllers/iti67.js @@ -14,15 +14,8 @@ function extractNationalIdentifier(patient) { return identifiers.find(id => id.system === NATIONAL_ID_SYSTEM) || null; } -async function getPatient(token, identifier) { - const response = await findPatient(token, { identifier: identifier }); - return Promise.resolve(response.data.entry[0].fullUrl); -} -async function getDocumentReferencesForPatient(token, patient) { - const searchset = await findDocumentReferenceByPatient(token, patient) - return Promise.resolve(searchset); -} + /** * ITI-67: Find Document References (MHD) * @@ -52,14 +45,15 @@ async function listDocumentReference(req, res, next) { ].join(',') ); // 1. Obtengo el identificador nacional del paciente en forma de referencia - const patientId = await getPatient(token, localPatientIdentifier); - if (!patientId) { + const patientSearchset = await findPatient(token, { identifier: localPatientIdentifier }); + if (!patientSearchset) { throw createError(422, 'Could not resolve national identifier for the given patient'); } + const patientNationalId = patientSearchset.entry[0].fullUrl // 2. Obtengo el listado de entradas del indice de atenciones asociadada al id de paciente. - const searchset = await getDocumentReferencesForPatient(token, patientId); + const documentReferenceSearchset = await findDocumentReferenceByPatient(token, patientNationalId) - res.status(200).json(searchset); + res.status(200).json(documentReferenceSearchset); } catch (err) { next(err); } diff --git a/bus-gateway/services/fhir.js b/bus-gateway/services/fhir.js index cd6e63c..a6230c2 100644 --- a/bus-gateway/services/fhir.js +++ b/bus-gateway/services/fhir.js @@ -14,14 +14,7 @@ async function processDocumentBundleTransaction(transactionBundle) { const response = await axios.post(HAPI_FHIR_SERVER_URL, transactionBundle, { headers: { 'Content-Type': 'application/fhir+json' }, }); - const processed = response.data; - const resources = []; - const responses = processed.entry.map(e => e.response); - for (i = 0; i < responses.length; i++) { - const resource = await axios.get(`${config.fhir.url}/${responses[i].location}`) - resources.push(resource); - } - return Promise.resolve(resources); + return response.data; }