From 30867958991a602069608310f9387db0d1f9b4bc Mon Sep 17 00:00:00 2001 From: Pierre Brisorgueil Date: Sat, 18 Apr 2020 12:06:57 +0200 Subject: [PATCH] =?UTF-8?q?feat(apis):=20add=20expiration=20&=20renew=20da?= =?UTF-8?q?ta=20if=20aggregate=20expired=20=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/apis/models/apis.model.mongoose.js | 1 + modules/apis/models/apis.schema.js | 1 + modules/apis/services/apis.service.js | 13 ++++++++++--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/modules/apis/models/apis.model.mongoose.js b/modules/apis/models/apis.model.mongoose.js index 03cb52f47..b605c7516 100644 --- a/modules/apis/models/apis.model.mongoose.js +++ b/modules/apis/models/apis.model.mongoose.js @@ -28,6 +28,7 @@ const ApiMongoose = new Schema({ description: String, savedb: Boolean, autoRequest: Boolean, + expiration: Date, user: { type: Schema.ObjectId, ref: 'User', diff --git a/modules/apis/models/apis.schema.js b/modules/apis/models/apis.schema.js index 1fe539e81..fadfb6e76 100644 --- a/modules/apis/models/apis.schema.js +++ b/modules/apis/models/apis.schema.js @@ -27,6 +27,7 @@ const ApiSchema = Joi.object().keys({ history: Joi.array().items(historySchema).optional(), savedb: Joi.boolean().default(false).required(), autoRequest: Joi.boolean().default(false).required(), + expiration: Joi.date().optional(), }); module.exports = { diff --git a/modules/apis/services/apis.service.js b/modules/apis/services/apis.service.js index 687ab2636..a67e7cd82 100644 --- a/modules/apis/services/apis.service.js +++ b/modules/apis/services/apis.service.js @@ -65,6 +65,8 @@ exports.update = async (api, body) => { api.description = body.description; api.savedb = body.savedb; api.autoRequest = body.autoRequest; + if (body.expiration && body.expiration !== '') api.expiration = body.expiration; + else api.expiration = null; if (body.typing && body.typing !== '') api.typing = body.typing; else api.typing = null; if (body.mapping && body.mapping !== '') api.mapping = body.mapping; @@ -215,11 +217,16 @@ exports.getApi = async (api, body) => { * @return {Promise} scrap */ exports.getAggregateApi = async (api, body) => { - const result = await ApisRepository.getAggregateApi(api.slug, body); - // check if no data return, then we probably have no data :) - // ask for it ! + 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()); + } 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()); + result = []; } + return Promise.resolve(result); };