add meta/models controller to get public models paths

This commit is contained in:
Matteo Pagliazzi
2016-02-29 00:09:09 +01:00
parent 5e68589ac8
commit 3512233966
6 changed files with 101 additions and 14 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ import {
let api = {};
/**
* @api {post} /unsubscribe Unsubscribe an email or user from email notifications
* @api {post} /email/unsubscribe Unsubscribe an email or user from email notifications
* @apiVersion 3.0.0
* @apiName UnsubscribeEmail
* @apiGroup Unsubscribe
@@ -0,0 +1,39 @@
import mongoose from 'mongoose';
let api = {};
let tasksModels = ['habit', 'daily', 'todo', 'reward'];
let allModels = ['user', 'tag', 'challenge', 'group'].concat(tasksModels);
/**
* @api {get} /meta/models/:model/paths Get all paths for the specified model. Doesn't require authentication
* @apiVersion 3.0.0
* @apiName GetUserModelPaths
* @apiGroup Meta
*
* @apiParam {string="user","group","challenge","tag","habit","daily","todo","reward"} model The name of the model
*
* @apiSuccess {object} paths A key-value object made of fieldPath: fieldType (like {'field.nested': Boolean})
*/
api.getModelPaths = {
method: 'GET',
url: '/meta/models/:model/paths',
async handler (req, res) {
req.checkParams('model', res.t('modelNotFound')).notEmpty().isIn(allModels);
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
let model = req.params.model;
// tasks models are lowercase, the others have the first letter uppercase (User, Group)
if (tasksModels.indexOf(model) === -1) {
model = model.charAt(0).toUpperCase() + model.slice(1);
}
model = mongoose.model(model);
res.respond(200, model.getModelPaths());
},
};
export default api;