Skip to content

Commit

Permalink
feat(apis): clean some code ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Mar 13, 2020
1 parent c2eef4d commit 632c538
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/helpers/montaineRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ exports.request = async (api, params) => {
return res;
} catch (err) {
if (err.response && err.response.data) return err.response;
throw new AppError(`Distant server (${api.url}) not reachable.`, { code: 'HELPERS_ERROR' });
throw new AppError(`Distant server (${api.url}) not reachable.`, { code: 'HELPERS_ERROR', details: err });
}
};

Expand Down
8 changes: 4 additions & 4 deletions lib/helpers/montaineTyping.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ const format = (data, action, object) => {
try {
return moment(new Date(data)).format();
} catch (err) {
throw new AppError('Typing : format DATE error', { code: 'HELPERS_ERROR' });
throw new AppError('Typing : format DATE 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);
} catch (err) {
throw new AppError('Typing : format NUMBER error', { code: 'HELPERS_ERROR' });
throw new AppError('Typing : format NUMBER error', { code: 'HELPERS_ERROR', details: err });
}
case 'STRING':
try {
return String(data);
} catch (err) {
throw new AppError('Typing : format STRING error', { code: 'HELPERS_ERROR' });
throw new AppError('Typing : format STRING error', { code: 'HELPERS_ERROR', details: err });
}
case 'HOUR':
try {
Expand All @@ -47,7 +47,7 @@ const format = (data, action, object) => {
if (param) data = `${_.get(object, param)} ${data}`;
return moment(new Date(data)).format();
} catch (err) {
throw new AppError('Typing : format HOURS error', { code: 'HELPERS_ERROR' });
throw new AppError('Typing : format HOURS error', { code: 'HELPERS_ERROR', details: err });
}
default:
throw new AppError('Typing : format unknown', { code: 'HELPERS_ERROR' });
Expand Down
22 changes: 14 additions & 8 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,29 @@ exports.load = async (api) => {
const result = {};
// conf
const params = montaineRequest.setParams(api.params);

// request
const request = await montaineRequest.request(api, params);
result.request = request;

// test data
// Typing
if (result.request.data && result.request.type === 'success' && api.typing && api.typing !== '') {
result.typing = montaineTyping.typing(result.request.data, JSON.parse(api.typing));
if (!result.typing) {
result.type = 'error';
result.message = 'Failed data Typing';
}
}

// test data
// Mapping
if (result.typing && api.mapping && api.mapping !== '') {
result.mapping = montaineMapping.mapping(result.typing, JSON.parse(api.mapping));
if (!result.mapping) {
result.type = 'error';
result.message = 'Failed data Mapping';
}
}


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

0 comments on commit 632c538

Please sign in to comment.