openhim-core
Version:
The OpenHIM core application that provides logging and routing of http requests
193 lines (172 loc) • 5.95 kB
JavaScript
var Channel, Client, ContactGroup, Keystore, Mediator, Q, User, authorisation, buildResponseObject, collections, getUniqueIdentifierForCollection, handleMetadataPost, logger, removeProperties, utils;
Channel = require('../model/channels').Channel;
Client = require('../model/clients').Client;
Mediator = require('../model/mediators').Mediator;
User = require('../model/users').User;
ContactGroup = require('../model/contactGroups').ContactGroup;
Keystore = require('../model/keystore').Keystore;
Q = require('q');
logger = require('winston');
authorisation = require('./authorisation');
utils = require("../utils");
collections = {
Channels: Channel,
Clients: Client,
Mediators: Mediator,
Users: User,
ContactGroups: ContactGroup,
Keystore: Keystore
};
removeProperties = function(obj) {
var prop, propertyID, propertyV;
propertyID = '_id';
propertyV = '__v';
for (prop in obj) {
if (prop === propertyID || prop === propertyV) {
delete obj[prop];
} else if (typeof obj[prop] === 'object' || obj[prop] instanceof Array) {
removeProperties(obj[prop]);
}
}
return obj;
};
getUniqueIdentifierForCollection = function(collection, doc) {
var returnObj, uid, uidKey;
switch (collection) {
case 'Channels':
uidKey = 'name';
uid = doc.name;
break;
case 'Clients':
uidKey = 'clientID';
uid = doc.clientID;
break;
case 'Mediators':
uidKey = 'urn';
uid = doc.urn;
break;
case 'Users':
uidKey = 'email';
uid = doc.email;
break;
case 'ContactGroups':
uidKey = 'groups';
uid = doc.groups;
}
returnObj = {};
returnObj[uidKey] = uid;
return returnObj;
};
buildResponseObject = function(model, doc, status, message, uid) {
return {
model: model,
record: doc,
status: status,
message: message,
uid: uid
};
};
exports.getMetadata = function*() {
var col, doc, e, error1, exportObject, i, len, params, ref;
if (!authorisation.inGroup('admin', this.authenticated)) {
return utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to getMetadata denied.", 'info');
}
try {
exportObject = {};
params = this.request.query;
for (col in collections) {
exportObject[col] = (yield collections[col].find().lean().exec());
ref = exportObject[col];
for (i = 0, len = ref.length; i < len; i++) {
doc = ref[i];
if (doc._id) {
doc = removeProperties(doc);
}
}
}
this.body = [exportObject];
return this.status = 200;
} catch (error1) {
e = error1;
this.body = e.message;
return utils.logAndSetResponse(this, 500, "Could not fetch specified metadata via the API " + e, 'error');
}
};
handleMetadataPost = function*(action, that) {
var doc, e, error, error1, error2, i, insertDocuments, insertObject, key, len, result, returnObject, status, uid, uidObj;
if (!authorisation.inGroup('admin', that.authenticated)) {
return utils.logAndSetResponse(that, 403, "User " + that.authenticated.email + " is not an admin, API access to importMetadata denied.", 'info');
}
try {
returnObject = [];
insertObject = that.request.body;
for (key in insertObject) {
insertDocuments = insertObject[key];
for (i = 0, len = insertDocuments.length; i < len; i++) {
doc = insertDocuments[i];
try {
if (!(key in collections)) {
throw new Error("Invalid Collection in Import Object");
}
if (key === 'Keystore') {
result = (yield collections[key].find().exec());
uid = '';
} else {
uidObj = getUniqueIdentifierForCollection(key, doc);
uid = uidObj[Object.keys(uidObj)[0]];
result = (yield collections[key].find(uidObj).exec());
}
if (action === 'import') {
if (result && result.length > 0 && result[0]._id) {
if (doc._id) {
delete doc._id;
}
(yield collections[key].findByIdAndUpdate(result[0]._id, doc).exec());
status = 'Updated';
} else {
doc = new collections[key](doc);
result = (yield Q.ninvoke(doc, 'save'));
status = 'Inserted';
}
}
if (action === 'validate') {
if (result && result.length > 0 && result[0]._id) {
status = 'Conflict';
} else {
doc = new collections[key](doc);
error = doc.validateSync();
if (error) {
throw new Error("Document Validation failed: " + error);
}
status = 'Valid';
}
}
logger.info("User " + that.authenticated.email + " performed " + action + " action on " + key + ", got " + status);
returnObject.push(buildResponseObject(key, doc, status, '', uid));
} catch (error1) {
e = error1;
logger.error("Failed to " + action + " " + key + " with unique identifier " + uid + ". " + e.message);
returnObject.push(buildResponseObject(key, doc, 'Error', e.message, uid));
}
}
}
that.body = returnObject;
return that.status = 201;
} catch (error2) {
e = error2;
that.body = e.message;
return utils.logAndSetResponse(that, 500, "Could not import metadata via the API " + e, 'error');
}
};
exports.importMetadata = function() {
return handleMetadataPost('import', this);
};
exports.validateMetadata = function() {
return handleMetadataPost('validate', this);
};
if (process.env.NODE_ENV === "test") {
exports.buildResponseObject = buildResponseObject;
exports.getUniqueIdentifierForCollection = getUniqueIdentifierForCollection;
exports.removeProperties = removeProperties;
}
//# sourceMappingURL=metadata.js.map