Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AurelienConte
Copy link

No description provided.

@@ -1,5 +1,10 @@

var domain = "bank.local.fr"
Copy link
Author

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


if (jws) {
Copy link
Author

Choose a reason for hiding this comment

The 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,
I have extracted common values inside the "config" object and create a ternary (be careful with ternary, the condition must remain clear, otherwise use a common IF / ELSE, in this case it's readable)


var {code, response } = await doRequest('GET',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The url is not clear at all, and can be factorized as : ${config.domain}/accounts/${id}/transactions?page=${page}


if (response && code == 200 && response.data) {
Copy link
Author

Choose a reason for hiding this comment

The 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 && code == 200 && response.data) {
if (response.data.meta) {
if (response.data.meta.hasPageSuivante) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double if can be factorized as : if (response?.data?.meta && response?.data?.meta?.hasPageSuivante) {

}
}

return response.data.Mouvements;
Copy link
Author

Choose a reason for hiding this comment

The 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.
It's a better practice the process by batch to avoid a huge allocated memory.


if (response?.data?.meta && response?.data?.meta?.hasPageSuivante) {
const mouvements = response.data.Mouvements;
const date = mouvements[mouvements.length - 1].dateValeur;
Copy link
Author

Choose a reason for hiding this comment

The 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.

@@ -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) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional parameters must be specified at the end of the function definition.
If you don't have the JWS parameter you must put NULL yourself in this case

}
const headers = jws ? {
...config.defaultHeaders,
"jws": jws,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just { jws } can be used, the property name will have the same name as the variable.

console.log(`--- Fetch Trasactions page n°${page} ---`);
try {
var headers = {"Authorisation": authorization }
console.log(`--- Fetch Trasactions page n°${page} ---`);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trasactions --> Transactions

@AurelienConte AurelienConte changed the title [REVIEW] Add changes to fileToReview.js [REVIEW] of fileToReview.js Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant