fume-fhir-converter
Version:
FHIR-Utilized Mapping Engine - Community
118 lines • 5.28 kB
JavaScript
;
/**
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.valueSetExpandDictionary = exports.codeSystemDictionary = exports.getValueSet = exports.getCodeSystem = exports.getTable = void 0;
const tslib_1 = require("tslib");
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const config_1 = tslib_1.__importDefault(require("../../config"));
const constants_1 = require("../../constants");
const fhirServer_1 = require("../fhirServer");
const jsonataExpression_1 = tslib_1.__importDefault(require("../jsonataExpression"));
const logger_1 = require("../logger");
const loadFhirPackageIndex_1 = require("./loadFhirPackageIndex");
const getTable = async (tableId) => {
if (tableId === undefined || tableId.trim() === '') {
// exit if no id provided
throw new Error('First argument to function getTable must be a table id, url or name');
}
;
const err = `Failed to fetch ConceptMap whose id, url or name is: '${tableId}'`;
let response;
try {
// try to fetch by id
response = await (0, fhirServer_1.getFhirClient)().read('ConceptMap/' + tableId);
}
catch {
// not found by id
try {
// try by url
response = await (0, fhirServer_1.getFhirClient)().search('ConceptMap', { url: tableId });
if (typeof response === 'object' && typeof response.total === 'number' && response.total !== 1) {
// try by name
response = await (0, fhirServer_1.getFhirClient)().search('ConceptMap', { name: tableId });
if (typeof response === 'object' && typeof response.total === 'number' && response.total !== 1) {
// coudn't find
(0, logger_1.getLogger)().error(err);
throw new Error(err);
}
}
}
catch {
(0, logger_1.getLogger)().error(err);
throw new Error(err);
}
}
;
const table = await jsonataExpression_1.default.conceptMapToTable.evaluate(response);
return table;
};
exports.getTable = getTable;
const getCodeSystem = async (codeSystemId) => {
const packageIndex = (0, loadFhirPackageIndex_1.getFhirPackageIndex)()[config_1.default.getFhirVersionMinor()];
const indexed = packageIndex.codeSystems.byUrl[codeSystemId] ??
packageIndex.codeSystems.byId[codeSystemId] ??
packageIndex.codeSystems.byName[codeSystemId];
if (!indexed) { // if not indexed, throw warning and return nothing
const msg = 'CodeSystem "' + codeSystemId + '" not found!';
(0, logger_1.getLogger)().warn(msg);
return undefined;
}
else if (Array.isArray(indexed)) {
const error = new Error(`Found multiple CodeSystem resources with the same id "${codeSystemId}"!`);
(0, logger_1.getLogger)().error(error);
throw (error);
}
else {
const path = indexed;
const resource = JSON.parse(fs_extra_1.default.readFileSync(path).toString()); // load file
return resource;
}
;
};
exports.getCodeSystem = getCodeSystem;
const getValueSet = async (valueSetId) => {
const packageIndex = (0, loadFhirPackageIndex_1.getFhirPackageIndex)()[config_1.default.getFhirVersionMinor()];
const indexed = packageIndex.valueSets.byUrl[valueSetId] ??
packageIndex.valueSets.byId[valueSetId] ??
packageIndex.valueSets.byName[valueSetId];
if (!indexed) { // if not indexed, throw warning and return nothing
const msg = 'ValueSet "' + valueSetId + '" not found!';
(0, logger_1.getLogger)().warn(msg);
return undefined;
}
else if (Array.isArray(indexed)) {
const error = new Error(`Found multiple ValueSet resources with the same id "${valueSetId}"!`);
(0, logger_1.getLogger)().error(error);
throw (error);
}
else {
const path = indexed;
const resource = JSON.parse(fs_extra_1.default.readFileSync(path).toString()); // load file
return resource;
}
;
};
exports.getValueSet = getValueSet;
const codeSystemDictionary = async (codeSystemId) => {
if (codeSystemId === 'urn:iso:std:iso:3166')
return constants_1.iso3166;
const resource = await (0, exports.getCodeSystem)(codeSystemId);
const csContent = resource?.content;
if (csContent === 'complete') {
return await jsonataExpression_1.default.codeSystemToDictionary.evaluate(resource);
}
else {
(0, logger_1.getLogger)().warn(`CodeSystem resource '${codeSystemId}' does not contain the full list of codes. Codes in this system cannot be validated`);
return undefined;
}
};
exports.codeSystemDictionary = codeSystemDictionary;
const valueSetExpandDictionary = async (valueSetId) => {
const resource = await (0, exports.getValueSet)(valueSetId);
return await jsonataExpression_1.default.valueSetExpandDictionary.evaluate({}, { vs: resource, valueSetExpand: exports.valueSetExpandDictionary, codeSystemDictionary: exports.codeSystemDictionary });
};
exports.valueSetExpandDictionary = valueSetExpandDictionary;
//# sourceMappingURL=conformance.js.map