Alejandro Gomez Auad d32c802c48 Revert ITI-67 patient subject back to fullUrl reference
DocumentReference.subject must be a literal reference (min=1 on
.reference per the DocumentReferenceOrigenAR profile), not an
identifier token: the Bus rejects subject.identifier. Go back to
using the Patient entry's fullUrl as the reference in both ITI-65 and
ITI-67 (ITI-65 was never committed with the identifier-based version,
so only ITI-67 needed the revert here), dropping the now-unused
extractNationalIdentifier helper.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-16 23:32:44 +00:00

67 lines
2.9 KiB
JavaScript

const createError = require('http-errors');
const config = require('../config');
const { getBusToken } = require('../utils/busAuth');
const { findPatient } = require('../services/patient');
const { findDocumentReferenceByPatient } = require('../services/documentReference');
/**
* ITI-67: Find Document References (MHD)
*
* GET /fhir/DocumentReference?subject=:localId
* GET /fhir/DocumentReference?patient.identifier=:localId
* GET /fhir/DocumentReference?subject=:localId&type=:type
*
* Flow:
* 1. Fetch the Patient from the Bus by local ID to obtain the national identifier.
* 2. Search DocumentReferences in the Bus by that national identifier
* (opcionalmente filtrando por tipo de documento).
* 3. Return the resulting Bundle.
*
* Required query parameter (one of):
* subject — local Patient ID in the Bus.
* patient.identifier — same semantics, alternate FHIR parameter name.
*
* Optional query parameter:
* type — token FHIR de DocumentReference.type (`system|code`, `|code` o `code`),
* ej: `http://loinc.org|60591-5` (IPS), `http://loinc.org|56445-0` (MeOW),
* `http://loinc.org|57133-1` (IT). Se pasa tal cual al Bus, sin traducir
* alias propios, para no apartarse de la semántica estándar de búsqueda
* por token de FHIR. Si se omite, devuelve documentos de cualquier tipo.
*/
async function listDocumentReference(req, res, next) {
try {
const localPatientIdentifier = req.query.subject ?? req.query['patient.identifier'];
if (!localPatientIdentifier) {
throw createError(400, 'Missing required query parameter: subject or patient.identifier');
}
const documentType = req.query.type;
const token = await getBusToken(
config.bus.url,
config.bus.jwtSecret,
config.bus.issuer,
[
config.bus.mpiScope,
config.bus.documentRegistryScope
].join(',')
);
// 1. Obtengo el identificador nacional del paciente en forma de referencia
// (fullUrl del recurso Patient en el Bus).
const patientSearchset = await findPatient(token, { identifier: localPatientIdentifier });
if (!patientSearchset || !Array.isArray(patientSearchset.entry) || patientSearchset.entry.length === 0) {
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
// (y, si se pidió, al tipo de documento).
const documentReferenceSearchset = await findDocumentReferenceByPatient(token, patientNationalId, documentType)
res.status(200).json(documentReferenceSearchset);
} catch (err) {
next(err);
}
}
module.exports = { listDocumentReference };