Skip to content

Commit

Permalink
feat(apis): add user dimension in history ✨
Browse files Browse the repository at this point in the history
clsoe #3
  • Loading branch information
PierreBrisorgueil committed Apr 21, 2020
1 parent 4e6873d commit f3be4a2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/helpers/montaineRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

/**
Expand Down
4 changes: 2 additions & 2 deletions modules/apis/controllers/apis.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions modules/apis/models/history.model.mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const HistoryMongoose = new Schema({
status: Boolean,
data: String,
time: Number,
user: {
type: Schema.ObjectId,
ref: 'User',
},
}, {
timestamps: true,
});
Expand Down
1 change: 1 addition & 0 deletions modules/apis/models/history.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
22 changes: 11 additions & 11 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = {};
Expand Down Expand Up @@ -138,15 +138,15 @@ 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({
api,
result,
});
} catch (err) {
await this.historize(err, start, api);
await this.historize(err, start, api, user);
return Promise.resolve({ err, api });
}
};
Expand All @@ -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 = {};
Expand Down Expand Up @@ -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 });
}
};
Expand Down Expand Up @@ -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 = [];
}

Expand Down

0 comments on commit f3be4a2

Please sign in to comment.