diff --git a/modules/apis/controllers/apis.controller.js b/modules/apis/controllers/apis.controller.js index ec05a45be..758f0a0d0 100644 --- a/modules/apis/controllers/apis.controller.js +++ b/modules/apis/controllers/apis.controller.js @@ -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 diff --git a/modules/apis/policies/apis.policy.js b/modules/apis/policies/apis.policy.js index 7abbcf403..6871b396c 100644 --- a/modules/apis/policies/apis.policy.js +++ b/modules/apis/policies/apis.policy.js @@ -27,5 +27,11 @@ exports.invokeRolesPolicies = () => { resources: '/api/apis/aggregate/:apiId', permissions: ['post'], }], + }, { + roles: ['guest'], + allows: [{ + resources: '/api/apis/stats', + permissions: ['get'], + }], }]); }; diff --git a/modules/apis/repositories/apis.repository.js b/modules/apis/repositories/apis.repository.js index 614c142e6..5d50516ce 100644 --- a/modules/apis/repositories/apis.repository.js +++ b/modules/apis/repositories/apis.repository.js @@ -77,7 +77,6 @@ exports.import = (apis, filters) => Api.bulkWrite(apis.map((api) => { }; })); - /** * @desc Function to update scrap history in db * @param {Object} scrap @@ -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 diff --git a/modules/apis/routes/apis.routes.js b/modules/apis/routes/apis.routes.js index 0f7a4a63c..a8ea4a6ea 100644 --- a/modules/apis/routes/apis.routes.js +++ b/modules/apis/routes/apis.routes.js @@ -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 diff --git a/modules/apis/services/apis.service.js b/modules/apis/services/apis.service.js index 8c45d78a9..b76f89349 100644 --- a/modules/apis/services/apis.service.js +++ b/modules/apis/services/apis.service.js @@ -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 diff --git a/modules/historys/controllers/historys.controller.js b/modules/historys/controllers/historys.controller.js index 8bca52c9b..330a2c4b3 100644 --- a/modules/historys/controllers/historys.controller.js +++ b/modules/historys/controllers/historys.controller.js @@ -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 diff --git a/modules/historys/policies/historys.policy.js b/modules/historys/policies/historys.policy.js index 8273aa18e..824f438f6 100644 --- a/modules/historys/policies/historys.policy.js +++ b/modules/historys/policies/historys.policy.js @@ -18,5 +18,11 @@ exports.invokeRolesPolicies = () => { resources: '/api/historys/:historyId', permissions: ['get'], }], + }, { + roles: ['guest'], + allows: [{ + resources: '/api/historys/stats', + permissions: ['get'], + }], }]); }; diff --git a/modules/historys/repositories/historys.repository.js b/modules/historys/repositories/historys.repository.js index 6b435c944..20a5fcdaa 100644 --- a/modules/historys/repositories/historys.repository.js +++ b/modules/historys/repositories/historys.repository.js @@ -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(); diff --git a/modules/historys/routes/historys.routes.js b/modules/historys/routes/historys.routes.js index 57297cbbf..1b5ad4fcd 100644 --- a/modules/historys/routes/historys.routes.js +++ b/modules/historys/routes/historys.routes.js @@ -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 diff --git a/modules/historys/services/historys.service.js b/modules/historys/services/historys.service.js index c9af3f8af..650f1f8e3 100644 --- a/modules/historys/services/historys.service.js +++ b/modules/historys/services/historys.service.js @@ -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); +};