Skip to content

Commit

Permalink
feat(apis): init history ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Mar 10, 2020
1 parent 6b3fad1 commit 683b33f
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 7 deletions.
19 changes: 18 additions & 1 deletion lib/helpers/montaine.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ exports.request = async (api) => {
});
return res;
} catch (err) {
return err.response.data;
if (err.response && err.response.data) return err.response.data;
return { data: 'distant server not reachable' };
}
};

/**
* @desc setScrapHistory
* @param {Object} data - scraping result
* @return {Object} mail status
*/
exports.setScrapHistory = (result, api, start) => ({
status: result.type === 'success',
apiId: api.id,
result: {
type: result.type || null,
message: result.message || null,
},
ip: result.ip || null,
time: new Date() - start,
});
4 changes: 4 additions & 0 deletions modules/apis/models/apis.model.mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const ApiMongoose = new Schema({
type: Schema.ObjectId,
ref: 'User',
},
history: [{
type: Schema.ObjectId,
ref: 'History',
}],
}, {
timestamps: true,
});
Expand Down
2 changes: 2 additions & 0 deletions modules/apis/models/apis.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
const joiZxcvbn = require('joi-zxcvbn');
const PlainJoi = require('joi');
const historySchema = require('./history.schema');

const Joi = PlainJoi.extend(joiZxcvbn(PlainJoi));

Expand All @@ -19,6 +20,7 @@ const ApiSchema = Joi.object().keys({
.optional(),
description: Joi.string().allow('').default('').optional(),
user: Joi.string().trim().default(''),
history: Joi.array().items(historySchema).optional(),
});

module.exports = {
Expand Down
39 changes: 39 additions & 0 deletions modules/apis/models/history.model.mongoose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Module dependencies
*/
const mongoose = require('mongoose');

const Schema = mongoose.Schema;

/**
* Data Model Mongoose
*/

const HistoryMongoose = new Schema({
apiId: String,
result: {},
status: Boolean,
time: Number,
}, {
timestamps: true,
});

/**
* @desc Function to add id (+ _id) to all objects
* @param {Object} scrap
* @return {Object} Scrap
*/
function addID() {
return this._id.toHexString();
}

/**
* Model configuration
*/
HistoryMongoose.virtual('id').get(addID);
// Ensure virtual fields are serialised.
HistoryMongoose.set('toJSON', {
virtuals: true,
});

mongoose.model('History', HistoryMongoose);
18 changes: 18 additions & 0 deletions modules/apis/models/history.schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Module dependencies
*/
const Joi = require('joi');

/**
* Data Schema
*/
const historySchema = Joi.object().keys({
apiId: Joi.string().trim().required(),
result: Joi.object({}).unknown().optional(),
time: Joi.number().default(0).required(),
status: Joi.boolean().default(false).required(),
});

module.exports = {
Scrap: historySchema,
};
4 changes: 2 additions & 2 deletions modules/apis/repositories/apis.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ exports.create = (api) => new Api(api).save();
*/
exports.get = (id) => {
if (!mongoose.Types.ObjectId.isValid(id)) return null;
return Api.findOne({ _id: id }).exec();
return Api.findOne({ _id: id }).populate('history').exec();
};

/**
* @desc Function to update a api in db
* @param {Object} api
* @return {Object} api
*/
exports.update = (api) => new Api(api).save();
exports.update = (api) => new Api(api).save().then((a) => a.populate('history').execPopulate());

/**
* @desc Function to delete a api in db
Expand Down
13 changes: 13 additions & 0 deletions modules/apis/repositories/history.repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Module dependencies
*/
const mongoose = require('mongoose');

const History = mongoose.model('History');

/**
* @desc Function to create a scrap in db
* @param {Object} scrap
* @return {Object} scrap
*/
exports.create = (history) => new History(history).save();
26 changes: 22 additions & 4 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('path');
const UserService = require(path.resolve('./modules/users/services/user.service.js'));
const montaine = require(path.resolve('./lib/helpers/montaine'));
const ApisRepository = require('../repositories/apis.repository');
const HistoryRepository = require('../repositories/history.repository');


/**
Expand Down Expand Up @@ -76,9 +77,26 @@ exports.delete = async (api) => {
* @return {Promise} scrap
*/
exports.load = async (api) => {
console.log('load');
const result = await montaine.request(api);
console.log('result', result.data);
const start = new Date();

let result = await montaine.request(api);
result = result.data;

const history = await HistoryRepository.create(montaine.setScrapHistory(result, api, start));
api.status = result.type === 'success';
api.history.push(history._id);
api = await ApisRepository.update(api);
// return
return Promise.resolve(result.data);
return Promise.resolve({
result,
api,
});
};


// const historySchema = Joi.object().keys({
// apiId: Joi.string().trim().required(),
// result: Joi.object({}).unknown().optional(),
// time: Joi.number().default(0).required(),
// status: Joi.boolean().default(false).required(),
// });

0 comments on commit 683b33f

Please sign in to comment.