Skip to content

Commit

Permalink
feat(apis, historys): set stats ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Jul 14, 2020
1 parent 9d69274 commit 5ab49ea
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 1 deletion.
14 changes: 14 additions & 0 deletions modules/apis/controllers/apis.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ exports.delete = async (req, res) => {
}
};

/**
* @desc Endpoint to get stats of apis and return data
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.stats = async (req, res) => {
const data = await ApisService.stats();
if (!data.err) {
responses.success(res, 'apis stats')(data);
} else {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(data.err))(data.err);
}
};

/**
* @desc Endpoint to load the request of the api to the api
* @param {Object} req - Express request object
Expand Down
6 changes: 6 additions & 0 deletions modules/apis/policies/apis.policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,11 @@ exports.invokeRolesPolicies = () => {
resources: '/api/apis/aggregate/:apiId',
permissions: ['post'],
}],
}, {
roles: ['guest'],
allows: [{
resources: '/api/apis/stats',
permissions: ['get'],
}],
}]);
};
7 changes: 6 additions & 1 deletion modules/apis/repositories/apis.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ exports.import = (apis, filters) => Api.bulkWrite(apis.map((api) => {
};
}));


/**
* @desc Function to update scrap history in db
* @param {Object} scrap
Expand All @@ -92,6 +91,12 @@ exports.historize = (api, history) => Api.updateOne(
},
);

/**
* @desc Function to get collection stats
* @return {Object} scrap
*/
exports.stats = () => Api.count();

/**
* @desc Function to insert list of data in db
* @param {Object} collection
Expand Down
4 changes: 4 additions & 0 deletions modules/apis/routes/apis.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const apisSchema = require('../models/apis.schema');
* Routes
*/
module.exports = (app) => {
// stats
app.route('/api/apis/stats').all(policy.isAllowed)
.get(apis.stats);

// list & post
app.route('/api/apis').all(passport.authenticate('jwt'), policy.isAllowed)
.get(apis.list) // list
Expand Down
9 changes: 9 additions & 0 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ exports.delete = async (api) => {
return Promise.resolve(result);
};

/**
* @desc Function to get all stats of db
* @return {Promise} All stats
*/
exports.stats = async () => {
const result = await ApisRepository.stats();
return Promise.resolve(result);
};

/**
* @desc Functio to ask repository to load an api request
* @param {Object} scrap - original scrap
Expand Down
14 changes: 14 additions & 0 deletions modules/historys/controllers/historys.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ exports.get = (req, res) => {
responses.success(res, 'history get')(history);
};

/**
* @desc Endpoint to get stats of historys and return data
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.stats = async (req, res) => {
const data = await HistorysService.stats();
if (!data.err) {
responses.success(res, 'Historys stats')(data);
} else {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(data.err))(data.err);
}
};

/**
* @desc MiddleWare to ask the service the history for this id
* @param {Object} req - Express request object
Expand Down
6 changes: 6 additions & 0 deletions modules/historys/policies/historys.policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@ exports.invokeRolesPolicies = () => {
resources: '/api/historys/:historyId',
permissions: ['get'],
}],
}, {
roles: ['guest'],
allows: [{
resources: '/api/historys/stats',
permissions: ['get'],
}],
}]);
};
6 changes: 6 additions & 0 deletions modules/historys/repositories/historys.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ exports.get = (id) => {
if (!mongoose.Types.ObjectId.isValid(id)) return null;
return History.findOne({ _id: id }).exec();
};

/**
* @desc Function to get collection stats
* @return {Object} scrap
*/
exports.stats = () => History.count();
4 changes: 4 additions & 0 deletions modules/historys/routes/historys.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const historys = require('../controllers/historys.controller');
* Routes
*/
module.exports = (app) => {
// stats
app.route('/api/historys/stats').all(policy.isAllowed)
.get(historys.stats);

// list & post
app.route('/api/historys').all(passport.authenticate('jwt'), policy.isAllowed)
.get(historys.list); // list
Expand Down
9 changes: 9 additions & 0 deletions modules/historys/services/historys.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ exports.historize = async (result, start, api, user) => {
console.log(err);
}
};

/**
* @desc Function to get all stats of db
* @return {Promise} All stats
*/
exports.stats = async () => {
const result = await HistorysRepository.stats();
return Promise.resolve(result);
};

0 comments on commit 5ab49ea

Please sign in to comment.