All files / src/server/metadata metadata.service.js

86.04% Statements 37/43
72.41% Branches 21/29
87.5% Functions 7/8
86.04% Lines 37/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 1275x 5x 5x 5x 5x   5x         5x 5x 5x                           5x           7x 7x     7x 7x   56x               56x     7x             7x     7x         7x         7x 7x       7x 56x 56x                           56x     7x         7x 7x 56x 8x   48x   56x       56x 56x       7x             5x      
const generateInteractions = require('./metadata.interactions.js');
const conformanceTemplate = require('./capability.template.js');
const deprecate = require('../utils/deprecation.notice.js');
const errors = require('../utils/error.utils.js');
const { container } = require('../winston.js');
 
let logger = container.get('default');
 
/**
 * Load the correct statement generators for the right version
 */
let getStatementGenerators = (base_version) => {
  if (base_version) {
    return require(`./capability.${base_version}`);
  } else E{
    return require('./capability.4_0_0');
  }
};
 
/**
 * @function generateCapabilityStatement
 * @description Assemble the capability statement based on the current profiles
 * @param {Object} args - Arguments for the endpoint
 * @param {Object} profiles - List of profile services we are using
 * @param {Winston} logger - Instance of Winston's logger
 * @return {Promise<Object>} - Return the capability statement
 */
let generateCapabilityStatement = ({
  fhirVersion,
  profiles,
  security,
  statementGenerator = getStatementGenerators,
}) =>
  new Promise((resolve, reject) => {
    logger.info('Metadata.generateCapabilityStatement');
 
    // create profile list
    let keys = Object.keys(profiles);
    let active_profiles = keys
      .map((profile_name) => {
        return {
          key: profile_name,
          makeResource: conformanceTemplate.resource,
          versions: profiles[profile_name] && profiles[profile_name].versions,
          service: profiles[profile_name] && profiles[profile_name].serviceModule,
          metadata: profiles[profile_name] && profiles[profile_name].metadata,
        };
      })
      .filter((profile) => profile.versions.indexOf(fhirVersion) !== -1);
 
    // TODO: REMOVE: Logger deprecatedLogger
    let deprecatedLogger = deprecate(
      container.get('default'),
      'Using the logger this way is deprecated. Please see the documentation on ' +
        'BREAKING CHANGES in version 2.0.0 for instructions on how to upgrade.'
    );
 
    // Get the necessary functions to generate statements
    let { makeStatement, securityStatement } = statementGenerator(fhirVersion, deprecatedLogger);
 
    // If we do not have these functions, we cannot generate a new statement
    Iif (!makeStatement || !securityStatement) {
      return reject(errors.internal('Unable to generate metadata for this FHIR specification.'));
    }
 
    // Let's start building our confromance/capability statement
    const serverStatement = {
      mode: 'server',
    };
 
    // Add security information if available
    Eif (security) {
      serverStatement.security = securityStatement(security);
    }
 
    // Add operations to resource if they exist.
    let operations = keys.reduce((ops, profile_name) => {
      const opsInProfile = profiles[profile_name].operation;
      Iif (opsInProfile && opsInProfile.length) {
        opsInProfile.forEach((opInProfile) => {
          const op = {
            name: opInProfile.name,
            definition: {
              reference: opInProfile.reference
                ? opInProfile.reference
                : `/OperationOutcome/${opInProfile.name}`,
            },
          };
          ops.push(op);
        });
      }
 
      return ops;
    }, []);
 
    Iif (operations && operations.length) {
      serverStatement.operation = operations;
    }
 
    // Make the resource and give it the version so it can only include valid search params
    let customMakeResource = null;
    serverStatement.resource = active_profiles.map((profile) => {
      if (profile.metadata) {
        customMakeResource = require(profile.metadata).makeResource;
      } else {
        customMakeResource = profile.service.makeResource;
      }
      let resource = customMakeResource
        ? customMakeResource(Object.assign(fhirVersion, { key: profile.key }), deprecatedLogger)
        : profile.makeResource(fhirVersion, profile.key);
      // Determine the interactions we need to list for this profile
      resource.interaction = generateInteractions(profile.service, resource.type);
      return resource;
    });
 
    // Add the server statement to the main statement
    return resolve(makeStatement(serverStatement));
  });
 
/**
 * @name exports
 * @summary Metadata service
 */
module.exports = {
  generateCapabilityStatement,
};