openhim-core
Version:
The OpenHIM core application that provides logging and routing of http requests
120 lines (107 loc) • 4.4 kB
JavaScript
var Channel, ContactGroup, Q, authorisation, logger, utils;
ContactGroup = require('../model/contactGroups').ContactGroup;
Q = require('q');
logger = require('winston');
authorisation = require('./authorisation');
Channel = require('../model/channels').Channel;
utils = require("../utils");
exports.addContactGroup = function*() {
var contactGroup, contactGroupData, err, error, result;
if (!authorisation.inGroup('admin', this.authenticated)) {
utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to addContactGroup denied.", 'info');
return;
}
contactGroupData = this.request.body;
try {
contactGroup = new ContactGroup(contactGroupData);
result = (yield Q.ninvoke(contactGroup, 'save'));
return utils.logAndSetResponse(this, 201, "Contact Group successfully created", 'info');
} catch (error) {
err = error;
return utils.logAndSetResponse(this, 400, "Could not add a contact group via the API: " + err, 'error');
}
};
exports.getContactGroup = function*(contactGroupId) {
var err, error, result;
if (!authorisation.inGroup('admin', this.authenticated)) {
utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to getContactGroup denied.", 'info');
return;
}
contactGroupId = unescape(contactGroupId);
try {
result = (yield ContactGroup.findById(contactGroupId).exec());
if (result === null) {
this.body = "Contact Group with id '" + contactGroupId + "' could not be found.";
return this.status = 404;
} else {
return this.body = result;
}
} catch (error) {
err = error;
return utils.logAndSetResponse(this, 500, "Could not find Contact Group by id '" + contactGroupId + "' via the API: " + err, 'error');
}
};
exports.updateContactGroup = function*(contactGroupId) {
var contactGroupData, err, error;
if (!authorisation.inGroup('admin', this.authenticated)) {
utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to updateContactGroup denied.", 'info');
return;
}
contactGroupId = unescape(contactGroupId);
contactGroupData = this.request.body;
if (contactGroupData._id) {
delete contactGroupData._id;
}
try {
(yield ContactGroup.findByIdAndUpdate(contactGroupId, contactGroupData).exec());
this.body = "Successfully updated contact group.";
return logger.info("User " + this.authenticated.email + " updated contact group with id " + contactGroupId);
} catch (error) {
err = error;
return utils.logAndSetResponse(this, 500, "Could not update Contact Group by id " + contactGroupId + " via the API: " + err, 'error');
}
};
exports.removeContactGroup = function*(contactGroupId) {
var err, error, linkedAlerts;
if (!authorisation.inGroup('admin', this.authenticated)) {
utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to removeContactGroup denied.", 'info');
return;
}
contactGroupId = unescape(contactGroupId);
try {
linkedAlerts = (yield Channel.find({
alerts: {
$elemMatch: {
groups: {
$in: [contactGroupId]
}
}
}
}).exec());
if (linkedAlerts.length > 0) {
this.status = 409;
return this.body = linkedAlerts;
} else {
(yield ContactGroup.findByIdAndRemove(contactGroupId).exec());
this.body = "Successfully removed contact group with ID '" + contactGroupId + "'";
return logger.info("User " + this.authenticated.email + " removed contact group with id " + contactGroupId);
}
} catch (error) {
err = error;
return utils.logAndSetResponse(this, 500, "Could not remove Contact Group by id {contactGroupId} via the API: " + err, 'error');
}
};
exports.getContactGroups = function*() {
var err, error;
if (!authorisation.inGroup('admin', this.authenticated)) {
utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to getContactGroups denied.", 'info');
return;
}
try {
return this.body = (yield ContactGroup.find().exec());
} catch (error) {
err = error;
return utils.logAndSetResponse(this, 500, "Could not fetch all Contact Group via the API: " + err, 'error');
}
};
//# sourceMappingURL=contactGroups.js.map