const config = require('../config'); const { createBusRequest } = require('../utils/busAuth'); const { default: axios } = require('axios'); const INDICE_ATENCION_URL = config.bus.documentRegistryUrl; const LOCAL_URL = "https://federador.qa-bus-interoperabilidad.svc.cluster.local:8080/fhir/DocumentReference"; function maskPrivateURL(patientId) { return patientId.replace(LOCAL_URL, INDICE_ATENCION_URL); } async function findDocumentReferenceById(token, id) { const request = createBusRequest(token); const response = await request.get( URL.parse(`${INDICE_ATENCION_URL}/${id}`) ); return response; } /** * Posts a DocumentReference to the document registry * (POST /fhir/DocumentReference). * @param {string} documentRegistryUrl - Base URL of the document registry service. * @param {string} token - Bearer access token. * @param {object} documentReference - FHIR DocumentReference resource to register. * @returns {Promise} The created DocumentReference resource returned by the registry. * @todo Agregar system http://federador.msal.gob.ar/uri para armar el identificador del custodian: http://federador.msal.gob.ar/uri| */ async function createDocumentReference(token, documentReference) { const request = createBusRequest(token); const response = await request.post( URL.parse(INDICE_ATENCION_URL), documentReference ); response.headers['location'] = maskPrivateURL(response.headers['location']); return response; } /** * Searches DocumentReferences in the document registry by patient subject identifier * (GET /fhir/DocumentReference?subject=system|value). * @param {string} documentRegistryUrl - Base URL of the document registry service. * @param {string} token - Bearer access token. * @param {string} subjectSystem - Identifier system of the patient. * @param {string} subjectValue - Identifier value of the patient. * @returns {Promise} FHIR Bundle (searchset) with matching DocumentReferences. */ async function findDocumentReferenceBySubject(token, subjectSystem, subjectValue) { const request = createBusRequest(token); const response = await request.get( URL.parse(INDICE_ATENCION_URL), { params: { subject: `${subjectSystem}|${subjectValue}` }, } ); return response; } /** * Searches DocumentReferences in the document registry by patient internal ID * (GET /fhir/DocumentReference?patient=value). * @param {string} documentRegistryUrl - Base URL of the document registry service. * @param {string} token - Bearer access token. * @param {string} patientId - Internal patient ID in the document registry. * @returns {Promise} FHIR Bundle (searchset) with matching DocumentReferences. */ async function findDocumentReferenceByPatient(token, patientId) { const request = createBusRequest(token); const response = await request.get( URL.parse(INDICE_ATENCION_URL), { params: { subject: patientId }, } ); return response; } /** * Searches DocumentReferences in the document registry filtered by subject, custodian and type * (GET /fhir/DocumentReference?subject=...&custodian=...&type=...). * @param {string} documentRegistryUrl - Base URL of the document registry service. * @param {string} token - Bearer access token. * @param {{ system: string, value: string }} subject - Patient identifier (TokenParam). * @param {{ system: string, value: string }} custodian - Custodian identifier (TokenParam). * @param {{ system: string, value: string }} type - Document type (TokenParam, e.g. { system: 'http://loinc.org', value: '60591-5' }). * @returns {Promise} FHIR Bundle (searchset) with matching DocumentReferences. */ async function searchDocumentReference(token, subject, custodian, type) { const request = createBusRequest(token); const response = await request.get( URL.parse(INDICE_ATENCION_URL), { params: { subject: `${subject.system}|${subject.value}`, custodian: `${custodian.system}|${custodian.value}`, type: `${type.system}|${type.value}`, }, }); return response; } module.exports = { findDocumentReferenceById, createDocumentReference, findDocumentReferenceBySubject, findDocumentReferenceByPatient, searchDocumentReference, };