From 6f1af800639220e53592c573c8f0af7d3b64fee5 Mon Sep 17 00:00:00 2001 From: Pierre Brisorgueil Date: Tue, 21 Apr 2020 18:14:50 +0200 Subject: [PATCH] =?UTF-8?q?feat(history):=20add=20get=20function=20?= =?UTF-8?q?=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/historys.controller.js | 31 +++++++++++++++++++ .../history/models/historys.model.mongoose.js | 4 +++ modules/history/models/historys.schema.js | 1 + modules/history/policies/historys.policy.js | 3 ++ .../repositories/historys.repository.js | 20 ++++++++++-- modules/history/routes/historys.routes.js | 7 +++++ modules/history/services/historys.service.js | 11 +++++++ 7 files changed, 75 insertions(+), 2 deletions(-) diff --git a/modules/history/controllers/historys.controller.js b/modules/history/controllers/historys.controller.js index 568810294..8bca52c9b 100644 --- a/modules/history/controllers/historys.controller.js +++ b/modules/history/controllers/historys.controller.js @@ -21,3 +21,34 @@ exports.list = async (req, res) => { responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err); } }; + +/** + * @desc Endpoint to show the current history + * @param {Object} req - Express request object + * @param {Object} res - Express response object + */ +exports.get = (req, res) => { + const history = req.history ? req.history.toJSON() : {}; + responses.success(res, 'history get')(history); +}; + +/** + * @desc MiddleWare to ask the service the history for this id + * @param {Object} req - Express request object + * @param {Object} res - Express response object + * @param {Function} next - Express next middleware function + * @param {String} id - history id + */ +exports.historyByID = async (req, res, next, id) => { + try { + const history = await HistorysService.get(id); + if (!history) responses.error(res, 404, 'Not Found', 'No history with that identifier has been found')(); + else { + req.history = history; + req.isOwner = history.user; // used if we proteck road by isOwner policy + next(); + } + } catch (err) { + next(err); + } +}; diff --git a/modules/history/models/historys.model.mongoose.js b/modules/history/models/historys.model.mongoose.js index c5a6bbc23..61f569cdc 100644 --- a/modules/history/models/historys.model.mongoose.js +++ b/modules/history/models/historys.model.mongoose.js @@ -16,6 +16,10 @@ const HistoryMongoose = new Schema({ type: Schema.ObjectId, ref: 'User', }, + api: { + type: Schema.ObjectId, + ref: 'Api', + }, }, { timestamps: true, }); diff --git a/modules/history/models/historys.schema.js b/modules/history/models/historys.schema.js index bee2a0239..0970ee3f5 100644 --- a/modules/history/models/historys.schema.js +++ b/modules/history/models/historys.schema.js @@ -11,6 +11,7 @@ const HistorySchema = Joi.object().keys({ data: Joi.string().optional(), time: Joi.number().default(0).required(), user: Joi.string().trim().default(''), + api: Joi.string().trim().default(''), }); module.exports = { diff --git a/modules/history/policies/historys.policy.js b/modules/history/policies/historys.policy.js index a27fb5579..8273aa18e 100644 --- a/modules/history/policies/historys.policy.js +++ b/modules/history/policies/historys.policy.js @@ -14,6 +14,9 @@ exports.invokeRolesPolicies = () => { allows: [{ resources: '/api/historys', permissions: ['get'], + }, { + resources: '/api/historys/:historyId', + permissions: ['get'], }], }]); }; diff --git a/modules/history/repositories/historys.repository.js b/modules/history/repositories/historys.repository.js index 1e7387d39..a2691a63d 100644 --- a/modules/history/repositories/historys.repository.js +++ b/modules/history/repositories/historys.repository.js @@ -6,12 +6,18 @@ const mongoose = require('mongoose'); const History = mongoose.model('History'); const Api = mongoose.model('Api'); + +const defaultPopulate = [{ + path: 'api', + select: 'id title', +}]; + /** * @desc Function to get all history in db * @return {Array} All historys */ -exports.list = (user) => History.find({ user: user._id }).sort('-createdAt').limit(100).exec(); - +exports.list = (user) => History.find({ user: user._id }).sort('-createdAt').populate(defaultPopulate).limit(500) + .exec; /** * @desc Function to create a scrap in db * @param {Object} scrap @@ -19,6 +25,16 @@ exports.list = (user) => History.find({ user: user._id }).sort('-createdAt').lim */ exports.create = (history) => new History(history).save(); +/** + * @desc Function to get a history from db + * @param {String} id + * @return {Object} history + */ +exports.get = (id) => { + if (!mongoose.Types.ObjectId.isValid(id)) return null; + return History.findOne({ _id: id }).exec(); +}; + /** * @desc Function to update scrap history in db * @param {Object} scrap diff --git a/modules/history/routes/historys.routes.js b/modules/history/routes/historys.routes.js index 03c565253..db4008e57 100644 --- a/modules/history/routes/historys.routes.js +++ b/modules/history/routes/historys.routes.js @@ -16,4 +16,11 @@ module.exports = (app) => { // list & post app.route('/api/historys').all(passport.authenticate('jwt'), policy.isAllowed) .get(historys.list); // list + + // classic crud + app.route('/api/historys/:historyId').all(passport.authenticate('jwt'), policy.isAllowed) // policy.isOwner available + .get(historys.get); // get + + // Finish by binding the historys middleware + app.param('historyId', historys.historyByID); }; diff --git a/modules/history/services/historys.service.js b/modules/history/services/historys.service.js index fe82caa46..4a8041094 100644 --- a/modules/history/services/historys.service.js +++ b/modules/history/services/historys.service.js @@ -15,6 +15,17 @@ exports.list = async (user) => { return Promise.resolve(result); }; +/** + * @desc Function to ask repository to get an history + * @param {String} id + * @return {Promise} task + */ +exports.get = async (id) => { + const result = await HistorysRepository.get(id); + return Promise.resolve(result); +}; + + /** * @desc Functio to ask repository to add an history * @param {Object} scrap - original scrap