Skip to content

Commit

Permalink
feat(apis): add expiration & renew data if aggregate expired ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Apr 18, 2020
1 parent 43e46d1 commit 3086795
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions modules/apis/models/apis.model.mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const ApiMongoose = new Schema({
description: String,
savedb: Boolean,
autoRequest: Boolean,
expiration: Date,
user: {
type: Schema.ObjectId,
ref: 'User',
Expand Down
1 change: 1 addition & 0 deletions modules/apis/models/apis.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
13 changes: 10 additions & 3 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};

0 comments on commit 3086795

Please sign in to comment.