Skip to content

Commit

Permalink
feat(history): split from api :) ✨
Browse files Browse the repository at this point in the history
close #4
  • Loading branch information
PierreBrisorgueil committed Apr 21, 2020
1 parent a8307ef commit eda9526
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 80 deletions.
8 changes: 2 additions & 6 deletions modules/apis/models/apis.schema.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/**
* Module dependencies
*/
const joiZxcvbn = require('joi-zxcvbn');
const PlainJoi = require('joi');
const historySchema = require('./history.schema');

const Joi = PlainJoi.extend(joiZxcvbn(PlainJoi));
const Joi = require('joi');

/**
* Data Schema
Expand All @@ -24,7 +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(),
history: Joi.array().items(Joi.string().trim()).optional(),
savedb: Joi.boolean().default(false).required(),
autoRequest: Joi.boolean().default(false).required(),
expiration: Joi.date().optional(),
Expand Down
44 changes: 6 additions & 38 deletions modules/apis/repositories/apis.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,6 @@ exports.get = (id) => {
*/
exports.update = (api) => new Api(api).save().then((a) => a.populate(defaultPopulate).execPopulate());


/**
* @desc Function to update scrap history in db
* @param {Object} scrap
* @param {Object} history
* @return {Object} scrap
*/
exports.historize = (api, history) => Api.updateOne(
{ _id: api._id },
{
$push: { history: history._id },
$set: { status: history.status },
},
);

/**
* @desc Function to delete a api in db
* @param {Object} api
Expand All @@ -71,10 +56,7 @@ exports.delete = (api) => Api.deleteOne({ _id: api.id }).exec();
* @return {Object} locations
*/
exports.import = (collection, items) => {
const _schema = new mongoose.Schema({}, {
collection,
strict: false,
});
const _schema = new mongoose.Schema({}, { collection, strict: false });
let model;
try {
model = mongoose.model(collection);
Expand All @@ -91,18 +73,13 @@ exports.import = (collection, items) => {
})));
};


/**
* @desc Function to get api data from db
* @param {string} colletion name
* @return [{Object}] data
*/
exports.listApi = (collection) => {
const _schema = new mongoose.Schema({}, {
collection,
strict: false,
timestamps: true,
});
const _schema = new mongoose.Schema({}, { collection, strict: false, timestamps: true });
let model;
try {
model = mongoose.model(collection);
Expand All @@ -112,18 +89,13 @@ exports.listApi = (collection) => {
return model.find().sort('-updatedAt').limit(100).exec();
};


/**
* @desc Function to ask for api data in db
* @desc Function to ask for api data in db
* @param {Object} locations
* @return {Object} locations
*/
exports.getApi = (collection, filters) => {
const _schema = new mongoose.Schema({}, {
collection,
strict: false,
timestamps: true,
});
const _schema = new mongoose.Schema({}, { collection, strict: false, timestamps: true });
let model;
try {
model = mongoose.model(collection);
Expand All @@ -134,16 +106,12 @@ exports.getApi = (collection, filters) => {
};

/**
* @desc Function to ask for api data in db
* @desc Function to make aggregate on api data in db
* @param {Object} locations
* @return {Object} locations
*/
exports.getAggregateApi = (collection, request) => {
const _schema = new mongoose.Schema({}, {
collection,
strict: false,
timestamps: true,
});
const _schema = new mongoose.Schema({}, { collection, strict: false, timestamps: true });
let model;
try {
model = mongoose.model(collection);
Expand Down
13 changes: 0 additions & 13 deletions modules/apis/repositories/history.repository.js

This file was deleted.

23 changes: 5 additions & 18 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const montaineMap = require(path.resolve('./lib/helpers/montaineMap'));
const montaineType = require(path.resolve('./lib/helpers/montaineType'));
const montaineRequest = require(path.resolve('./lib/helpers/montaineRequest'));
const montaineSave = require(path.resolve('./lib/helpers/montaineSave'));
const HistorysService = require(path.resolve('./modules/history/services/historys.service'));
const ApisRepository = require('../repositories/apis.repository');
const HistoryRepository = require('../repositories/history.repository');


/**
Expand Down Expand Up @@ -86,19 +86,6 @@ exports.delete = async (api) => {
return Promise.resolve(result);
};


/**
* @desc Functio to ask repository to add an history
* @param {Object} scrap - original scrap
* @return {Promise} scrap
*/
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);
};

/**
* @desc Functio to ask repository to load an api request
* @param {Object} scrap - original scrap
Expand Down Expand Up @@ -138,15 +125,15 @@ exports.load = async (api, user) => {
}

// historize
await this.historize({ request: result.request, mongo: result.mongo, result: result.result }, start, api, user);
await HistorysService.historize({ request: result.request, mongo: result.mongo, result: result.result }, start, api, user);

// return
return Promise.resolve({
api,
result,
});
} catch (err) {
await this.historize(err, start, api, user);
await HistorysService.historize(err, start, api, user);
return Promise.resolve({ err, api });
}
};
Expand Down Expand Up @@ -203,9 +190,9 @@ exports.workerAuto = async (api, body, user) => {
}

// historize
await this.historize(_.clone({ request, result }), start, api, user);
await HistorysService.historize(_.clone({ request, result }), start, api, user);
} catch (err) {
await this.historize(err, start, api, user);
await HistorysService.historize(err, start, api, user);
return Promise.resolve({ err, api });
}
};
Expand Down
23 changes: 23 additions & 0 deletions modules/history/controllers/historys.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Module dependencies
*/
const path = require('path');

const errors = require(path.resolve('./lib/helpers/errors'));
const responses = require(path.resolve('./lib/helpers/responses'));

const HistorysService = require('../services/historys.service');

/**
* @desc Endpoint to ask the service to get the list of historys
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.list = async (req, res) => {
try {
const historys = await HistorysService.list(req.user);
responses.success(res, 'history list')(historys);
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const Schema = mongoose.Schema;
/**
* Data Model Mongoose
*/

const HistoryMongoose = new Schema({
status: Boolean,
data: String,
Expand All @@ -23,8 +22,8 @@ const HistoryMongoose = new Schema({

/**
* @desc Function to add id (+ _id) to all objects
* @param {Object} scrap
* @return {Object} Scrap
* @param {Object} history
* @return {Object} History
*/
function addID() {
return this._id.toHexString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const Joi = require('joi');
/**
* Data Schema
*/
const historySchema = Joi.object().keys({
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 = {
Scrap: historySchema,
History: HistorySchema,
};
19 changes: 19 additions & 0 deletions modules/history/policies/historys.policy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Module dependencies
* */
const path = require('path');

const policy = require(path.resolve('./lib/middlewares/policy'));

/**
* Invoke Historys Permissions
*/
exports.invokeRolesPolicies = () => {
policy.Acl.allow([{
roles: ['user'],
allows: [{
resources: '/api/historys',
permissions: ['get'],
}],
}]);
};
34 changes: 34 additions & 0 deletions modules/history/repositories/historys.repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Module dependencies
*/
const mongoose = require('mongoose');

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

/**
* @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();

/**
* @desc Function to create a scrap in db
* @param {Object} scrap
* @return {Object} scrap
*/
exports.create = (history) => new History(history).save();

/**
* @desc Function to update scrap history in db
* @param {Object} scrap
* @param {Object} history
* @return {Object} scrap
*/
exports.apiHistorize = (api, history) => Api.updateOne(
{ _id: api._id },
{
$push: { history: history._id },
$set: { status: history.status },
},
);
19 changes: 19 additions & 0 deletions modules/history/routes/historys.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Module dependencies
*/
const passport = require('passport');
const path = require('path');

const model = require(path.resolve('./lib/middlewares/model'));
const policy = require(path.resolve('./lib/middlewares/policy'));
const historys = require('../controllers/historys.controller');
const historysSchema = require('../models/historys.schema');

/**
* Routes
*/
module.exports = (app) => {
// list & post
app.route('/api/historys').all(passport.authenticate('jwt'), policy.isAllowed)
.get(historys.list); // list
};
28 changes: 28 additions & 0 deletions modules/history/services/historys.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Module dependencies
*/
const path = require('path');

const montaineRequest = require(path.resolve('./lib/helpers/montaineRequest'));
const HistorysRepository = require('../repositories/historys.repository');

/**
* @desc Function to get all history in db
* @return {Promise} All historys
*/
exports.list = async (user) => {
const result = await HistorysRepository.list(user);
return Promise.resolve(result);
};

/**
* @desc Functio to ask repository to add an history
* @param {Object} scrap - original scrap
* @return {Promise} scrap
*/
exports.historize = async (result, start, api, user) => {
const history = await HistorysRepository.create(montaineRequest.setApiHistory(result, start, user));
await HistorysRepository.apiHistorize(api, history);
api.history.push(history);
return Promise.resolve(api);
};

0 comments on commit eda9526

Please sign in to comment.