-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[REVIEW] of fileToReview.js #5
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
|
||
var domain = "bank.local.fr" | ||
const config = { | ||
domain: "bank.local.fr", | ||
defaultHeaders: { | ||
"Content-type": "application/json", | ||
"Accept": "application/json", | ||
} | ||
} | ||
|
||
/** | ||
* @description Fetch transactions recursively | ||
|
@@ -12,62 +17,42 @@ var domain = "bank.local.fr" | |
* @return {Object} All transactions available on the page | ||
*/ | ||
async function fetchTransactions(fromDate, authorization, jws = null, id, page, previousTransactions) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional parameters must be specified at the end of the function definition. |
||
console.log(`--- Fetch Trasactions page n°${page} ---`); | ||
try { | ||
var headers = {"Authorisation": authorization } | ||
console.log(`--- Fetch Trasactions page n°${page} ---`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trasactions --> Transactions |
||
|
||
if (jws) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have factorized the condition. Since just one value is changing inside the IF / ELSE condition, |
||
headers = { | ||
"Authorisation": authorization, | ||
"jws": jws, | ||
"Content-type": "application/json", | ||
"Accept": "application/json" | ||
} | ||
} else { | ||
headers = { | ||
"Authorisation": authorization, | ||
"Content-type": "application/json", | ||
"Accept": "application/json", | ||
} | ||
} | ||
const headers = jws ? { | ||
...config.defaultHeaders, | ||
"jws": jws, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just |
||
"Authorisation": authorization | ||
} : {...config.defaultHeaders, "Authorisation": authorization}; | ||
|
||
const {code, response} = await doRequest('GET', `${config.domain}/accounts/${id}/transactions?page=${page}`, headers); | ||
|
||
var {code, response } = await doRequest('GET', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The url is not clear at all, and can be factorized as : |
||
domain + '/accounts/'+ id + '/transactions?' + `page=${page}`, | ||
headers); | ||
if (code < 200 || code >= 400 || !response?.data) { | ||
console.error(`Bad request received from bankin API : ${code}`, response); | ||
return []; | ||
} | ||
|
||
if (response?.data?.meta && response?.data?.meta?.hasPageSuivante) { | ||
const mouvements = response.data.Mouvements; | ||
const date = mouvements[mouvements.length - 1].dateValeur; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't have checked if you got "mouvements" in the body, if it is an array and if you got an element. |
||
|
||
if (date <= fromDate) { | ||
console.log("FromDate is Reached - we don't need more transaction"); | ||
} else { | ||
|
||
if (!mouvements) | ||
throw new Error("Empty list of transactions ! " + JSON.stringify(previousTransactions)); | ||
|
||
if (response && code == 200 && response.data) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you have lot of condition to validate before doing an action, check the negativity of the condition first to avoid a IF inside another IF etc... The max depth are commonly 2 or 3 IF inside each others before the clarity is impacted |
||
if (response.data.meta) { | ||
if (response.data.meta.hasPageSuivante) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double if can be factorized as : |
||
let mouvements = response.data.Mouvements; | ||
var date = mouvements[mouvements.length -1].dateValeur; | ||
if (date <= fromDate) { | ||
console.log("FromDate is Reached - we don't need more transaction"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see a lot of console log and they also create a lot of extra IF / ELSE in your code. Are they all needed ? |
||
} else { | ||
// if we have mouvements | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comment must be useful and give context to the process. |
||
if (mouvements) { | ||
if (assertTransactions(mouvements)) { | ||
return []; | ||
} else { | ||
console.log(`Push transactions from page ${page}`); | ||
} | ||
} else { | ||
throw new Error("Empty list of transactions ! " + JSON.stringify(previousTransactions)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why throwing everywhere and catch at the end ? And also manage the return value at this moment. |
||
} | ||
let nextPagesTransactions = fetchTransactions(fromDate, authorization, (jws || null), id, page + 1, mouvements); | ||
response.data.Mouvements = mouvements.concat(nextPagesTransactions); | ||
} | ||
} | ||
if (assertTransactions(mouvements)) { | ||
return []; | ||
} else { | ||
console.log(`Push transactions from page ${page}`); | ||
} | ||
return response.data.Mouvements; | ||
} else throw new Error(); | ||
|
||
return []; | ||
} catch (err) { | ||
throw new CustomError({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without saying again that is a bad idea you are throwing a custom error who have always the same statusCode. |
||
function: 'fetchTransactions', | ||
statusCode: 'CRASH', | ||
rawError: e, | ||
}); | ||
} | ||
const nextPagesTransactions = fetchTransactions(fromDate, authorization, (jws || null), id, page + 1, mouvements); | ||
response.data.Mouvements = mouvements.concat(nextPagesTransactions); | ||
} | ||
} | ||
|
||
return response.data.Mouvements; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are fetching the WHOLE list of movements before returning them. This is to avoid in case of lot of data. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use 'const' instead of 'var'. Var have a biggest litter than const and should be avoided if possible. (use const / let instead)
Same for the next code
I have also put it inside a config object for more clarity since i have also merged common headers in this object.
Next step will be to set them in ENV vars for a better management