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

feat(accounts) : importing accounts from csv file #3061

Merged
merged 18 commits into from
Aug 29, 2018

Conversation

mbayopanda
Copy link
Collaborator

@mbayopanda mbayopanda commented Aug 20, 2018

This PR add the feature of importing accounts to use into the application from a csv file.

Close #3076
Close #3034

}

// progress handler
function handleProgress(evt) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

How many places to we use this logic? It looks like we use it in src/modules/inventory/list/modals/import.modal.js as well as src/modules/patients/documents/modals/documents.modal.js.

Since we use it in three place, I propose you make an issue for a directive that would do this calculation and rendering for you. Something like:

<bh-progress-meter current="currentProgress" total="totalProgress">
</bh-progress-meter>

That way, we can remove all this custom logic and just put the directive in the HTML. We can also test it to make sure it works the way we hope it to work.

const params = util.convertStringToNumber(req.query);

if (params.option !== IMPORT_DEFAULT_OHADA_ACCOUNTS && (!req.files || req.files.length === 0)) {
next(new BadRequest('Expected at least one file upload but did not receive any files.'));
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should add a translated error here - it is the second parameter on the BadRequest() constructor.

So you would do something like

new BadRequest('Something broke', 'ERRORS.EVERYTHING_BAD');

and then translate the ERRORS.EVERYTHING_BAD in the i18n files.

dbPromises.push(importAccountFromFile(basicAccountFile, req.session.enterprise.id, params.option));
}

if (params.option === IMPORT_CUSTOM_OHADA_ACCOUNTS || params.option === IMPORT_OTHER_ACCOUNTS) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need params.option === IMPORT_CUSTOM_OHADA_ACCOUNTS twice? Could you leave a comment in the code? It looks like a bug from just reading the code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Twice in this condition or in this line and the line 52 ?

We have three options for loading accounts into the system :

    1. Load default ohada accounts : this option doesn't require to send a csv file, so the system load the file directly from the server side.
    1. Load custom ohada accounts : in this option, the user must add ohada accounts (by uploading a csv file) to default ohada accounts which are already loaded into the system.
    1. Load accounts : this option is more generic, if the user need to load any kind of accounts (not ohada) into the system. the user must upload a csv file of accounts.

In the line 52, we test the first or the second option, in this case we must load ohada default accounts anyway.

In the line 57, we test the second or the third option, in this case we must load accounts from uploaded csv file of accounts.

function importAccountFromFile(filePath, enterpriseId, option) {
let query;
let queryParams;
return formatCsvToJson(filePath)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is an ideal place for an async/await function! You could do this:

async function importAccountFromFile(filePath, enterpriseId, option) {
  const data = await formatCsvToJson(filePath);

  if (!hasValidDataFormat(data)) {
    throw new BadRequest('The given file has a bad data format for accounts', 'ERRORS.BAD_DATA_FORMAT');
  }

  const transaction = db.transaction();

  data.forEach(item => {
    query = 'CALL ImportAccount(?, ?, ?, ?, ?, ?);';
    queryParams = [
      enterpriseId,
      item.account_number,
      item.account_label,
      item.account_type,
      item.account_parent || null,
      option,
    ];
    transaction.addQuery(query, queryParams);
  });
  
  return transaction.execute();
}

* formatCsvToJson
* @param {object} file the csv file sent by the client
*/
function formatCsvToJson(file) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like this function is a copy and paste of controllers/inventory/import/index.js's formatCsvToJson() function. Instead of copying and pasting the code, could we make a library for this and re-use the same code? It would also allow us to unit test this function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it is not totally a copy pasta, the line 120 is different (file.path versus file).

You are right, we can put this function in a library for unit test it.


Returns the account type id of the given account number
*/
CREATE FUNCTION PredictAccountTypeId(accountNumber INT(11))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why does this need to be an SQL function? It doesn't look like it uses the database at all...

Can we do this in JavaScript? SQL functions are a pain to upgrade in production.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The simple reason is this function is just used when we are in the option 1 and the account number has four digit, and the most important we cannot send this value (the predicted account type id) as parameters because it is not needed at anytime we call the ImportAccount procedure

@mbayopanda mbayopanda force-pushed the import-account branch 29 times, most recently from e48d6a1 to 7c2dffe Compare August 24, 2018 11:39
@mbayopanda
Copy link
Collaborator Author

@jniles could you review this PR again please

Copy link
Collaborator

@jniles jniles left a comment

Choose a reason for hiding this comment

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

Looks fine to me!

bors r+

bors bot added a commit that referenced this pull request Aug 29, 2018
3061: feat(accounts) : importing accounts from csv file r=jniles a=mbayopanda

This PR add the feature of importing accounts to use into the application from a csv file.

Close #3076 
Close #3034 

Co-authored-by: mbayopanda <[email protected]>
@bors
Copy link
Contributor

bors bot commented Aug 29, 2018

Build failed

@mbayopanda
Copy link
Collaborator Author

bors r+

bors bot added a commit that referenced this pull request Aug 29, 2018
3061: feat(accounts) : importing accounts from csv file r=mbayopanda a=mbayopanda

This PR add the feature of importing accounts to use into the application from a csv file.

Close #3076 
Close #3034 

Co-authored-by: mbayopanda <[email protected]>
@bors
Copy link
Contributor

bors bot commented Aug 29, 2018

Build succeeded

@bors bors bot merged commit ce2262d into Third-Culture-Software:master Aug 29, 2018
@jniles jniles mentioned this pull request Aug 30, 2018
bors bot added a commit that referenced this pull request Aug 30, 2018
3041: refractor(session service) using $state instead of $location r=jniles a=jeremielodi

closes #3030

3094: fix: migrate to utf8mb4 r=jniles a=jniles

This commit migrates the database to UTF8MB4 in preparation for MySQL 8 which defaults to this.  It also produces better results for diverse character sets.

Closes #2989.

**Overview**
The motivation for this Pull Request is two-fold:
 1. Make sure no one had [break the server](https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-utf8mb4-11761243e434) by putting valid characters that [cannot be represented in MySQL's UTF8 character set](https://stackoverflow.com/questions/30074492/what-is-the-difference-between-utf8mb4-and-utf8-charsets-in-mysql#30074553).
 2. Ensure we are set up for future releases of MySQL and future languages by migrating to UTF8MB4.  MySQL 8 will support this out of the box.

And additional benefit continues the work started by @mbayopanda in his work on Travis CI changes in #3061 to document and standardize the character sets and collations in our database.  Previously, we just relied on having "the right set up", with some of our Stored Procedures in the server's default encoding, functions called in the mysqljs's default encoding, etc...  Now, we define at creation time and connection time the character sets and collations we are working with.

**Notes**
I had to [skip one test](https://github.com/IMA-WorldHealth/bhima-2.X/blob/master/test/end-to-end/verificationLinks/verificationLinks.spec.js#L19).  It was the link between debtor groups and patients.  I'm pretty sure this is due to the fact that the order data was inserted in changed somehow.  Either way, the test is non-specific and should be re-written to reference the group by its name.



Co-authored-by: jeremielodi <[email protected]>
Co-authored-by: Jonathan Niles <[email protected]>
bors bot added a commit that referenced this pull request Aug 30, 2018
3094: fix: migrate to utf8mb4 r=jniles a=jniles

This commit migrates the database to UTF8MB4 in preparation for MySQL 8 which defaults to this.  It also produces better results for diverse character sets.

Closes #2989.

**Overview**
The motivation for this Pull Request is two-fold:
 1. Make sure no one had [break the server](https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-utf8mb4-11761243e434) by putting valid characters that [cannot be represented in MySQL's UTF8 character set](https://stackoverflow.com/questions/30074492/what-is-the-difference-between-utf8mb4-and-utf8-charsets-in-mysql#30074553).
 2. Ensure we are set up for future releases of MySQL and future languages by migrating to UTF8MB4.  MySQL 8 will support this out of the box.

And additional benefit continues the work started by @mbayopanda in his work on Travis CI changes in #3061 to document and standardize the character sets and collations in our database.  Previously, we just relied on having "the right set up", with some of our Stored Procedures in the server's default encoding, functions called in the mysqljs's default encoding, etc...  Now, we define at creation time and connection time the character sets and collations we are working with.

**Notes**
I had to [skip one test](https://github.com/IMA-WorldHealth/bhima-2.X/blob/master/test/end-to-end/verificationLinks/verificationLinks.spec.js#L19).  It was the link between debtor groups and patients.  I'm pretty sure this is due to the fact that the order data was inserted in changed somehow.  Either way, the test is non-specific and should be re-written to reference the group by its name.



3104: fix fiscal year list pagination r=jniles a=mbayopanda

This PR fix the pagination in the fiscal year list module by using the uib-pagination attribute instead of the element.

Co-authored-by: Jonathan Niles <[email protected]>
Co-authored-by: mbayopanda <[email protected]>
bors bot added a commit that referenced this pull request Aug 30, 2018
3094: fix: migrate to utf8mb4 r=jniles a=jniles

This commit migrates the database to UTF8MB4 in preparation for MySQL 8 which defaults to this.  It also produces better results for diverse character sets.

Closes #2989.

**Overview**
The motivation for this Pull Request is two-fold:
 1. Make sure no one had [break the server](https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-utf8mb4-11761243e434) by putting valid characters that [cannot be represented in MySQL's UTF8 character set](https://stackoverflow.com/questions/30074492/what-is-the-difference-between-utf8mb4-and-utf8-charsets-in-mysql#30074553).
 2. Ensure we are set up for future releases of MySQL and future languages by migrating to UTF8MB4.  MySQL 8 will support this out of the box.

And additional benefit continues the work started by @mbayopanda in his work on Travis CI changes in #3061 to document and standardize the character sets and collations in our database.  Previously, we just relied on having "the right set up", with some of our Stored Procedures in the server's default encoding, functions called in the mysqljs's default encoding, etc...  Now, we define at creation time and connection time the character sets and collations we are working with.

**Notes**
I had to [skip one test](https://github.com/IMA-WorldHealth/bhima-2.X/blob/master/test/end-to-end/verificationLinks/verificationLinks.spec.js#L19).  It was the link between debtor groups and patients.  I'm pretty sure this is due to the fact that the order data was inserted in changed somehow.  Either way, the test is non-specific and should be re-written to reference the group by its name.



Co-authored-by: Jonathan Niles <[email protected]>
@mbayopanda mbayopanda deleted the import-account branch September 5, 2018 10:23
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.

2 participants