2015-08-31 16:59:56 +03:00
|
|
|
// # Client API
|
|
|
|
// RESTful API for the Client resource
|
2017-09-12 18:31:14 +03:00
|
|
|
var Promise = require('bluebird'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
pipeline = require('../utils/pipeline'),
|
|
|
|
apiUtils = require('./utils'),
|
|
|
|
models = require('../models'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../lib/common'),
|
2017-09-12 18:31:14 +03:00
|
|
|
docName = 'clients',
|
2015-08-31 16:59:56 +03:00
|
|
|
clients;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Clients API Methods
|
|
|
|
*
|
2017-12-11 22:27:09 +03:00
|
|
|
* **See:** [API Methods](events.js.html#api%20methods)
|
2015-08-31 16:59:56 +03:00
|
|
|
*/
|
|
|
|
clients = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Read
|
|
|
|
* @param {{id}} options
|
|
|
|
* @return {Promise<Client>} Client
|
|
|
|
*/
|
|
|
|
read: function read(options) {
|
|
|
|
var attrs = ['id', 'slug'],
|
|
|
|
tasks;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Model Query
|
|
|
|
* Make the call to the Model layer
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function doQuery(options) {
|
|
|
|
// only User Agent (type = `ua`) clients are available at the moment.
|
|
|
|
options.data = _.extend(options.data, {type: 'ua'});
|
2017-09-27 11:31:41 +03:00
|
|
|
|
|
|
|
return models.Client.findOne(options.data, _.omit(options, ['data']))
|
|
|
|
.then(function onModelResponse(model) {
|
|
|
|
if (!model) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.NotFoundError({
|
|
|
|
message: common.i18n.t('common.api.clients.clientNotFound')
|
2017-09-27 11:31:41 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
clients: [model.toJSON(options)]
|
|
|
|
};
|
|
|
|
});
|
2015-08-31 16:59:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
|
|
|
tasks = [
|
2017-09-12 18:31:14 +03:00
|
|
|
apiUtils.validate(docName, {attrs: attrs}),
|
2015-08-31 16:59:56 +03:00
|
|
|
// TODO: add permissions
|
|
|
|
// utils.handlePublicPermissions(docName, 'read'),
|
|
|
|
doQuery
|
|
|
|
];
|
|
|
|
|
|
|
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
2017-09-27 11:31:41 +03:00
|
|
|
return pipeline(tasks, options);
|
2015-08-31 16:59:56 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = clients;
|