Skip to content

Commit

Permalink
feat(apis): clean typing for meteo ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Mar 20, 2020
1 parent 751adc9 commit aef85b3
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 54 deletions.
14 changes: 4 additions & 10 deletions lib/helpers/montaineRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,18 @@ exports.request = async (api, params) => {
if (api.auth === 'lou' && res.data) return res.data;
return res;
} catch (err) {
if (err.response && err.response.data) return err.response;
if (api.auth === 'lou' && err.response && err.response.data) throw new AppError('Lou server dailed.', { code: 'HELPERS_ERROR', details: err.response.data });
throw new AppError(`Distant server (${api.url}) not reachable.`, { code: 'HELPERS_ERROR', details: err });
}
};


/**
* @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,
exports.setApiHistory = (status, err, start) => ({
status,
err: err || null,
time: new Date() - start,
});
39 changes: 16 additions & 23 deletions lib/helpers/montaineType.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/**
* Module dependencies
*/
const moment = require('moment');
const path = require('path');
const _ = require('lodash');

const AppError = require(path.resolve('./lib/helpers/AppError'));
const tricks = require(path.resolve('./lib/helpers/tricks'));
const typingDates = require(path.resolve('./lib/helpers/typing/dates'));
const typingTypes = require(path.resolve('./lib/helpers/typing/types'));

/**
* @desc Function to format data
Expand All @@ -17,7 +18,7 @@ const tricks = require(path.resolve('./lib/helpers/tricks'));
*/
const format = (data, action, object) => {
if (!data) throw new AppError('Typing : format data not defined.', { code: 'HELPERS_ERROR' });
if (!action) throw new AppError('Typing : format action not defined.', { code: 'HELPERS_ERROR' });
if (!action) return data;
if (!object) throw new AppError('Typing : format object not defined.', { code: 'HELPERS_ERROR' });

const func = action.split('(')[0];
Expand All @@ -26,38 +27,31 @@ const format = (data, action, object) => {
switch (func) {
case 'DATE':
try {
let date = '';
date = moment(data).format();
// fixs
if (date === 'Invalid date') {
const dateSplit = data.split('-');
if (dateSplit.length === 3) date = moment(`${dateSplit[0]}-${dateSplit[1]}`).add(Number(dateSplit[2]) - 1, 'days').format(); // Hot Fix if date containe more dats than a month
else throw new AppError(`Typing : date invalid ${data}`, { code: 'HELPERS_ERROR', details: date });
}
return date;
return typingDates.DATE(data);
} catch (err) {
throw new AppError('Typing : format DATE error', { code: 'HELPERS_ERROR', details: err });
}
case 'DATE_NEXT_DAY':
try {
return typingDates.DATE_NEXT_DAY(data);
} catch (err) {
throw new AppError('Typing : format DATE_NEXT_DAY error', { code: 'HELPERS_ERROR', details: err });
}
case 'NUMBER':
try {
data = String(data).replace(/,/g, '.'); // switch , to .
data = String(data).replace(/[^\d.-]/g, ''); // remove if it's not letters
return Number(data);
return typingTypes.NUMBER(data);
} catch (err) {
throw new AppError('Typing : format NUMBER error', { code: 'HELPERS_ERROR', details: err });
}
case 'STRING':
try {
return String(data);
return typingTypes.STRING(data);
} catch (err) {
throw new AppError('Typing : format STRING error', { code: 'HELPERS_ERROR', details: err });
}
case 'HOUR':
try {
data = String(data).replace(/h/g, ':');
data = String(data).replace(/s/g, ':');
if (param) data = `${_.get(object, param)} ${data}`;
return moment(new Date(data)).format();
return typingDates.HOUR(data, param, object);
} catch (err) {
throw new AppError('Typing : format HOURS error', { code: 'HELPERS_ERROR', details: err });
}
Expand All @@ -73,23 +67,22 @@ const format = (data, action, object) => {
* @return {object} object - new object generated
*/
const prepareFormat = (object, schema) => {
const result = {};
const keys = tricks.objectDeepKeys(object); // get all keys
keys.forEach((k) => {
const value = _.get(object, k);
if (!Array.isArray(value) && typeof value !== 'object') {
const p = k.split('.');
let func = '';
let func;
if (/^\d+$/.test(p[p.length - 1])) { // array
func = _.get(schema, p.slice(0, -1).join('.'))[0];
} else if (/^\d+$/.test(p[p.length - 2])) { // object
p[p.length - 2] = 0;
func = _.get(schema, p.join('.'));
} else func = _.get(schema, k);
_.set(result, k, format(value, func, object));
_.set(object, k, format(value, func, object));
}
});
return result;
return object;
};

/**
Expand Down
41 changes: 41 additions & 0 deletions lib/helpers/typing/dates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const moment = require('moment');
const _ = require('lodash');
const path = require('path');

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

/**
* @desc _DATE
*/
exports.DATE = (data) => {
let date = moment(data).format();
// fixs
if (date === 'Invalid date') {
const dateSplit = data.split('-');
if (dateSplit.length === 3) date = moment(`${dateSplit[0]}-${dateSplit[1]}`).add(Number(dateSplit[2]) - 1, 'days').format(); // Hot Fix if date containe more dats than a month
else throw new AppError(`Typing : date invalid ${data}`, { code: 'HELPERS_ERROR', details: date });
}
return date;
};

/**
* @desc _DATE_NEXT_DAY
*/
exports.DATE_NEXT_DAY = (data) => {
let date;
const target = Number(data);
const today = Number(moment().date());
if (target >= today) date = moment().add(target - today, 'days').format();
else date = moment().add(1, 'months').subtract(today - target, 'days').format();
return date;
};

/**
* @desc HOUR
*/
exports.HOUR = (data, param, object) => {
if (param) {
return moment(_.get(object, param)).set('hour', Number(data)).format();
}
return moment(_.get(object, param)).format();
};
13 changes: 13 additions & 0 deletions lib/helpers/typing/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @desc NUMBER
*/
exports.NUMBER = (data) => {
data = String(data).replace(/,/g, '.'); // switch , to .
data = String(data).replace(/[^\d.-]/g, ''); // remove if it's not digit
return Number(data);
};

/**
* @desc STRING
*/
exports.STRING = (data) => String(data);
6 changes: 5 additions & 1 deletion modules/apis/controllers/apis.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ 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 start = new Date();
try {
const data = await ApisService.load(req.api);
const data = await ApisService.load(req.api, start);

responses.success(res, 'api loaded')(data);
await ApisService.historize(true, null, start, req.api);
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
await ApisService.historize(false, err, start, req.api);
}
};

Expand Down
3 changes: 1 addition & 2 deletions modules/apis/models/history.model.mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ const Schema = mongoose.Schema;
*/

const HistoryMongoose = new Schema({
apiId: String,
result: {},
status: Boolean,
err: {},
time: Number,
}, {
timestamps: true,
Expand Down
5 changes: 2 additions & 3 deletions modules/apis/models/history.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ 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(),
err: Joi.object({}).unknown().optional(),
time: Joi.number().default(0).required(),
});

module.exports = {
Expand Down
31 changes: 16 additions & 15 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,25 @@ exports.delete = async (api) => {
};


/**
* @desc Functio to ask repository to add an history
* @param {Object} scrap - original scrap
* @return {Promise} scrap
*/
exports.historize = async (status, err, start, api) => {
const history = await HistoryRepository.create(montaineRequest.setApiHistory(status, err, start));
api.history.push(history._id);
api.status = history.status;
api = await ApisRepository.update(api);
return Promise.resolve(api);
};

/**
* @desc Functio to ask repository to load an api request
* @param {Object} scrap - original scrap
* @return {Promise} scrap
*/
exports.load = async (api) => {
const start = new Date();
exports.load = async (api, start) => {
const result = {};
// conf
const params = montaineRequest.setParams(api.params);
Expand All @@ -100,19 +112,12 @@ exports.load = async (api) => {
if (result.result && api.mapping && api.mapping !== '') {
result.mapping = montaineMap.map(result.result, JSON.parse(api.mapping));
result.result = result.mapping;
if (!result.mapping) {
result.type = 'error';
result.message = 'Failed data Mapping';
}
}

// Typing
if (result.result && result.request.type === 'success' && api.typing && api.typing !== '') {
if (result.result && api.typing && api.typing !== '') {
result.typing = montaineType.type(result.result, JSON.parse(api.typing));
result.result = result.typing;
if (!result.typing) {
result.type = 'error';
result.message = 'Failed data Typing';
}
}
// prepare for save
if (result.result) {
Expand All @@ -122,10 +127,6 @@ exports.load = async (api) => {
if (api.savedb) result.result = await ApisRepository.import(api.slug, result.result);
}

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

0 comments on commit aef85b3

Please sign in to comment.