openhim-core
Version:
The OpenHIM core application that provides logging and routing of http requests
162 lines (145 loc) • 5.19 kB
JavaScript
var Client, Q, authorisation, logger, utils;
Client = require('../model/clients').Client;
Q = require('q');
logger = require('winston');
authorisation = require('./authorisation');
utils = require('../utils');
/*
* Adds a client
*/
exports.addClient = function*() {
var client, clientData, e, error, result;
if (!authorisation.inGroup('admin', this.authenticated)) {
utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to addClient denied.", 'info');
return;
}
clientData = this.request.body;
try {
client = new Client(clientData);
result = (yield Q.ninvoke(client, 'save'));
logger.info("User " + this.authenticated.email + " created client with id " + client.id);
this.body = 'Client successfully created';
return this.status = 201;
} catch (error) {
e = error;
logger.error("Could not add a client via the API: " + e.message);
this.body = e.message;
return this.status = 400;
}
};
/*
* Retrieves the details of a specific client
*/
exports.getClient = function*(clientId, property) {
var e, error, projectionRestriction, result;
projectionRestriction = null;
if (typeof property === 'string') {
if (property === 'clientName') {
projectionRestriction = {
_id: 0,
name: 1
};
} else {
utils.logAndSetResponse(this, 404, "The property (" + property + ") you are trying to retrieve is not found.", 'info');
return;
}
} else {
if (!authorisation.inGroup('admin', this.authenticated)) {
utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to findClientById denied.", 'info');
return;
}
}
clientId = unescape(clientId);
try {
result = (yield Client.findById(clientId, projectionRestriction).exec());
if (result === null) {
return utils.logAndSetResponse(this, 404, "Client with id " + clientId + " could not be found.", 'info');
} else {
return this.body = result;
}
} catch (error) {
e = error;
logger.error("Could not find client by id " + clientId + " via the API: " + e.message);
this.body = e.message;
return this.status = 500;
}
};
exports.findClientByDomain = function*(clientDomain) {
var e, error, result;
if (!authorisation.inGroup('admin', this.authenticated)) {
utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to findClientByDomain denied.", 'info');
return;
}
clientDomain = unescape(clientDomain);
try {
result = (yield Client.findOne({
clientDomain: clientDomain
}).exec());
if (result === null) {
return utils.logAndSetResponse(this, 404, "Could not find client with clientDomain " + clientDomain, 'info');
} else {
return this.body = result;
}
} catch (error) {
e = error;
logger.error("Could not find client by client Domain " + clientDomain + " via the API: " + e.message);
this.body = e.message;
return this.status = 500;
}
};
exports.updateClient = function*(clientId) {
var clientData, e, error;
if (!authorisation.inGroup('admin', this.authenticated)) {
utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to updateClient denied.", 'info');
return;
}
clientId = unescape(clientId);
clientData = this.request.body;
if (clientData._id) {
delete clientData._id;
}
try {
(yield Client.findByIdAndUpdate(clientId, clientData).exec());
logger.info("User " + this.authenticated.email + " updated client with id " + clientId);
return this.body = 'Successfully updated client.';
} catch (error) {
e = error;
logger.error("Could not update client by ID " + clientId + " via the API: " + e.message);
this.body = e.message;
return this.status = 500;
}
};
exports.removeClient = function*(clientId) {
var e, error;
if (!authorisation.inGroup('admin', this.authenticated)) {
utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to removeClient denied.", 'info');
return;
}
clientId = unescape(clientId);
try {
(yield Client.findByIdAndRemove(clientId).exec());
this.body = "Successfully removed client with ID " + clientId;
return logger.info("User " + this.authenticated.email + " removed client with id " + clientId);
} catch (error) {
e = error;
logger.error("Could not remove client by ID " + clientId + " via the API: " + e.message);
this.body = e.message;
return this.status = 500;
}
};
exports.getClients = function*() {
var e, error;
if (!authorisation.inGroup('admin', this.authenticated)) {
utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to getClients denied.", 'info');
return;
}
try {
return this.body = (yield Client.find().exec());
} catch (error) {
e = error;
logger.error("Could not fetch all clients via the API: " + e.message);
this.message = e.message;
return this.status = 500;
}
};
//# sourceMappingURL=clients.js.map