From f3be4a27cad32f55910d2c23978b70f1262b6cb2 Mon Sep 17 00:00:00 2001 From: Pierre Brisorgueil Date: Tue, 21 Apr 2020 12:10:29 +0200 Subject: [PATCH] =?UTF-8?q?feat(apis):=20add=20user=20dimension=20in=20his?= =?UTF-8?q?tory=20=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit clsoe #3 --- lib/helpers/montaineRequest.js | 3 ++- modules/apis/controllers/apis.controller.js | 4 ++-- modules/apis/models/history.model.mongoose.js | 4 ++++ modules/apis/models/history.schema.js | 1 + modules/apis/services/apis.service.js | 22 +++++++++---------- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/helpers/montaineRequest.js b/lib/helpers/montaineRequest.js index fd1d05d74..4453061f5 100644 --- a/lib/helpers/montaineRequest.js +++ b/lib/helpers/montaineRequest.js @@ -79,10 +79,11 @@ exports.request = async (api, params) => { * @param {Object} data - scraping result * @return {Object} mail status */ -exports.setApiHistory = (result, start) => ({ +exports.setApiHistory = (result, start, user) => ({ status: !!((result.request && result.request.type === 'success')), data: JSON.stringify(result, null, 2) || null, time: new Date() - start, + user: user.id || null, }); /** diff --git a/modules/apis/controllers/apis.controller.js b/modules/apis/controllers/apis.controller.js index e12b48456..d817bcb69 100644 --- a/modules/apis/controllers/apis.controller.js +++ b/modules/apis/controllers/apis.controller.js @@ -83,7 +83,7 @@ exports.delete = async (req, res) => { */ exports.load = async (req, res) => { // TODO if (req.scrap && req.user && req.scrap.user && req.scrap.user.id === req.user.id) next(); - const data = await ApisService.load(req.api); + const data = await ApisService.load(req.api, req.user); if (!data.err) { responses.success(res, 'api loaded')(data); } else { @@ -130,7 +130,7 @@ exports.getApi = async (req, res) => { exports.getAggregateApi = async (req, res) => { // TODO if (req.scrap && req.user && req.scrap.user && req.scrap.user.id === req.user.id) next(); try { - const data = await ApisService.getAggregateApi(req.api, req.body); + const data = await ApisService.getAggregateApi(req.api, req.body, req.user); responses.success(res, 'api getData')(data); } catch (err) { responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err); diff --git a/modules/apis/models/history.model.mongoose.js b/modules/apis/models/history.model.mongoose.js index 0ce2c757c..31b74352a 100644 --- a/modules/apis/models/history.model.mongoose.js +++ b/modules/apis/models/history.model.mongoose.js @@ -13,6 +13,10 @@ const HistoryMongoose = new Schema({ status: Boolean, data: String, time: Number, + user: { + type: Schema.ObjectId, + ref: 'User', + }, }, { timestamps: true, }); diff --git a/modules/apis/models/history.schema.js b/modules/apis/models/history.schema.js index 7f52bb1e6..6e12286ff 100644 --- a/modules/apis/models/history.schema.js +++ b/modules/apis/models/history.schema.js @@ -10,6 +10,7 @@ const historySchema = Joi.object().keys({ status: Joi.boolean().default(false).required(), data: Joi.string().optional(), time: Joi.number().default(0).required(), + user: Joi.string().trim().default(''), }); module.exports = { diff --git a/modules/apis/services/apis.service.js b/modules/apis/services/apis.service.js index 0cf2e6593..a6f46202c 100644 --- a/modules/apis/services/apis.service.js +++ b/modules/apis/services/apis.service.js @@ -92,8 +92,8 @@ exports.delete = async (api) => { * @param {Object} scrap - original scrap * @return {Promise} scrap */ -exports.historize = async (result, start, api) => { - const history = await HistoryRepository.create(montaineRequest.setApiHistory(result, start)); +exports.historize = async (result, start, api, user) => { + const history = await HistoryRepository.create(montaineRequest.setApiHistory(result, start, user)); await ApisRepository.historize(api, history); api.history.push(history); return Promise.resolve(api); @@ -104,7 +104,7 @@ exports.historize = async (result, start, api) => { * @param {Object} scrap - original scrap * @return {Promise} scrap */ -exports.load = async (api) => { +exports.load = async (api, user) => { const start = new Date(); try { const result = {}; @@ -138,7 +138,7 @@ exports.load = async (api) => { } // historize - await this.historize({ request: result.request, mongo: result.mongo, result: result.result }, start, api); + await this.historize({ request: result.request, mongo: result.mongo, result: result.result }, start, api, user); // return return Promise.resolve({ @@ -146,7 +146,7 @@ exports.load = async (api) => { result, }); } catch (err) { - await this.historize(err, start, api); + await this.historize(err, start, api, user); return Promise.resolve({ err, api }); } }; @@ -156,7 +156,7 @@ exports.load = async (api) => { * @param {Object} scrap - original scrap * @return {Promise} scrap */ -exports.workerAuto = async (api, body) => { +exports.workerAuto = async (api, body, user) => { const start = new Date(); try { const result = {}; @@ -203,9 +203,9 @@ exports.workerAuto = async (api, body) => { } // historize - await this.historize(_.clone({ request, result }), start, api); + await this.historize(_.clone({ request, result }), start, api, user); } catch (err) { - await this.historize(err, start, api); + await this.historize(err, start, api, user); return Promise.resolve({ err, api }); } }; @@ -236,15 +236,15 @@ exports.getApi = async (api, body) => { * @param {Object} scrap - original scrap * @return {Promise} scrap */ -exports.getAggregateApi = async (api, body) => { +exports.getAggregateApi = async (api, body, user) => { let result = await ApisRepository.getAggregateApi(api.slug, body); if (result.length === 0 && api.autoRequest) { // check if no data return, then we probably have no data :) ask for it ! - this.workerAuto(api, body, new Date()); + this.workerAuto(api, body, user); } else if (Date.now() - Date.parse(result[0]._updatedAt) > Date.parse(api.expiration)) { // check if data but data expired, ask for refresh ! - this.workerAuto(api, body, new Date()); + this.workerAuto(api, body, user); result = []; }