UNPKG

openhim-core

Version:

The OpenHIM core application that provides logging and routing of http requests

265 lines (246 loc) 8.72 kB
var Channel, Mediator, Q, authorisation, constructError, logger, saveDefaultChannelConfig, semver, utils, validateConfig; Channel = require('../model/channels').Channel; Mediator = require('../model/mediators').Mediator; Q = require('q'); logger = require('winston'); authorisation = require('./authorisation'); semver = require('semver'); utils = require("../utils"); exports.getAllMediators = function*() { var err; if (!authorisation.inGroup('admin', this.authenticated)) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to getAllMediators denied.", 'info'); return; } try { return this.body = (yield Mediator.find().exec()); } catch (_error) { err = _error; return logAndSetResponse(this, 500, "Could not fetch mediators via the API: " + err, 'error'); } }; exports.getMediator = function*(mediatorURN) { var err, result, urn; if (!authorisation.inGroup('admin', this.authenticated)) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to getMediator denied.", 'info'); return; } urn = unescape(mediatorURN); try { result = (yield Mediator.findOne({ "urn": urn }).exec()); if (result === null) { return this.status = 404; } else { return this.body = result; } } catch (_error) { err = _error; return logAndSetResponse(this, 500, "Could not fetch mediator using UUID " + urn + " via the API: " + err, 'error'); } }; saveDefaultChannelConfig = function(config) { var channel, i, len, results; results = []; for (i = 0, len = config.length; i < len; i++) { channel = config[i]; results.push(new Channel(channel).save()); } return results; }; constructError = function(message, name) { var err; err = new Error(message); err.name = name; return err; }; exports.addMediator = function*() { var err, existing, mediator, param, ref, val; if (!authorisation.inGroup('admin', this.authenticated)) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to addMediator denied.", 'info'); return; } try { mediator = this.request.body; if (!mediator.urn) { throw constructError('URN is required', 'ValidationError'); } if (!mediator.version || !semver.valid(mediator.version)) { throw constructError('Version is required. Must be in SemVer form x.y.z', 'ValidationError'); } if (mediator.config != null) { validateConfig(mediator.configDefs, mediator.config); } existing = (yield Mediator.findOne({ urn: mediator.urn }).exec()); if (typeof existing !== 'undefined' && existing !== null) { if (semver.gt(mediator.version, existing.version)) { if ((mediator.config != null) && (existing.config != null)) { ref = mediator.config; for (param in ref) { val = ref[param]; if (existing.config[param] != null) { mediator.config[param] = existing.config[param]; } } } (yield Mediator.findByIdAndUpdate(existing._id, mediator).exec()); } } else { if (!mediator.endpoints || mediator.endpoints.length < 1) { throw constructError('At least 1 endpoint is required', 'ValidationError'); } (yield Q.ninvoke(new Mediator(mediator), 'save')); if (mediator.defaultChannelConfig) { (yield saveDefaultChannelConfig(mediator.defaultChannelConfig)); } } this.status = 201; return logger.info("User " + this.authenticated.email + " created mediator with urn " + mediator.urn); } catch (_error) { err = _error; if (err.name === 'ValidationError') { return utils.logAndSetResponse(this, 400, "Could not add Mediator via the API: " + err, 'error'); } else { return utils.logAndSetResponse(this, 500, "Could not add Mediator via the API: " + err, 'error'); } } }; exports.removeMediator = function*(urn) { var err; if (!authorisation.inGroup('admin', this.authenticated)) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to removeMediator denied.", 'info'); return; } urn = unescape(urn); try { (yield Mediator.findOneAndRemove({ urn: urn }).exec()); this.body = "Mediator with urn " + urn + " has been successfully removed by " + this.authenticated.email; return logger.info("Mediator with urn " + urn + " has been successfully removed by " + this.authenticated.email); } catch (_error) { err = _error; return utils.logAndSetResponse(this, 500, "Could not remove Mediator by urn " + urn + " via the API: " + err, 'error'); } }; exports.heartbeat = function*(urn) { var err, heartbeat, mediator, update; if (!authorisation.inGroup('admin', this.authenticated)) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to removeMediator denied.", 'info'); return; } urn = unescape(urn); try { mediator = (yield Mediator.findOne({ urn: urn }).exec()); if (mediator == null) { this.status = 404; return; } heartbeat = this.request.body; if ((heartbeat != null ? heartbeat.uptime : void 0) == null) { this.status = 400; return; } if (mediator._configModifiedTS > mediator._lastHeartbeat || (heartbeat != null ? heartbeat.config : void 0) === true) { this.body = mediator.config; } else { this.body = ""; } if (heartbeat != null) { update = { _lastHeartbeat: new Date(), _uptime: heartbeat.uptime }; (yield Mediator.findByIdAndUpdate(mediator._id, update).exec()); } return this.status = 200; } catch (_error) { err = _error; return utils.logAndSetResponse(this, 500, "Could not process mediator heartbeat (urn: " + urn + "): " + err, 'error'); } }; validateConfig = function(configDef, config) { return Object.keys(config).every(function(param) { var matchingDefs; matchingDefs = configDef.filter(function(def) { return def.param === param; }); if (matchingDefs.length === 0) { throw constructError("No config definition found for parameter " + param, 'ValidationError'); } return matchingDefs.map(function(def) { switch (def.type) { case 'string': if (typeof config[param] !== 'string') { throw constructError("Expected config param " + param + " to be a string.", 'ValidationError'); } break; case 'bigstring': if (typeof config[param] !== 'string') { throw constructError("Expected config param " + param + " to be a large string.", 'ValidationError'); } break; case 'number': if (typeof config[param] !== 'number') { throw constructError("Expected config param " + param + " to be a number.", 'ValidationError'); } break; case 'bool': if (typeof config[param] !== 'boolean') { throw constructError("Expected config param " + param + " to be a boolean.", 'ValidationError'); } break; case 'option': if ((def.values.indexOf(config[param])) === -1) { throw constructError("Expected config param " + param + " to be one of " + def.values, 'ValidationError'); } } }); }); }; if (process.env.NODE_ENV === "test") { exports.validateConfig = validateConfig; } exports.setConfig = function*(urn) { var config, err, mediator; if (!authorisation.inGroup('admin', this.authenticated)) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to removeMediator denied.", 'info'); return; } urn = unescape(urn); config = this.request.body; try { mediator = (yield Mediator.findOne({ urn: urn }).exec()); if (mediator == null) { this.status = 404; this.body = 'No mediator found for this urn.'; return; } try { validateConfig(mediator.configDefs, config); } catch (_error) { err = _error; this.status = 400; this.body = err.message; return; } (yield Mediator.findOneAndUpdate({ urn: urn }, { config: this.request.body, _configModifiedTS: new Date() }).exec()); return this.status = 200; } catch (_error) { err = _error; return utils.logAndSetResponse(this, 500, "Could not set mediator config (urn: " + urn + "): " + err, 'error'); } }; //# sourceMappingURL=mediators.js.map