const config = require("../config"); const axios = require('axios'); const HAPI_FHIR_SERVER_URL = config.fhir.url; /** * Procesa una transacción y devuelve los recursos persistidos en el servidor HAPI FHIR * @param {*} transactionBundle * @returns */ 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); } async function getResourceByUrl(url) { const response = await axios.get( URL.parse(url, HAPI_FHIR_SERVER_URL) ); return response.data; } async function getDocumentBundleByUrl(url) { const response = await axios.get(parsedUrl.toString(), { responseType: 'arraybuffer', headers: { Accept: req.headers['accept'] || '*/*', }, }); return response.data } module.exports = { processDocumentBundleTransaction, getResourceByUrl }