Skip to content

Commit

Permalink
Working import: #25
Browse files Browse the repository at this point in the history
Simplify the controller's split of file contents away from header and
footer.  I think that separating on `\r\n` will generally work for files
we create, but I worry somewhat that other files will have `\r\n` on
every line of data.  Add a div to hold notifications of duplicates from
the import (this will look nicer in the next commit.  Consider it
something of a placeholder for now).  Track duplicates in a variable
instead of just outputting them to the console.

Insert the data from each (non-duplicate) file line into a client
object.  I'm a little worried that explicitly naming the column index
for each element is not a portable solution, *but* HMIS explicitly
details the correct format for a compliant CSV file, so maybe it's okay.
What do you think, @kfogel?  (I'm looking at lines 1019-1041 of
index.js, here).

POST the data!  It saves correctly in my DB.
  • Loading branch information
cecilia-donnelly committed Aug 5, 2015
1 parent 77b63ac commit a7df443
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
9 changes: 3 additions & 6 deletions app/controllers/core.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ exports.index = function(req, res) {
// naming this "file_data" because I assume that we will
// only be POST-ing imported files. This may change in the
// future.

// split out the contents from the headers
var csv_body = body.split("Content-Type: text/csv\r\n\r\n");
// and from the footers
var csv_only = csv_body[1].split("\r\n\r\n-----------------------------");
var csv_contents = csv_only[0];
// split out the contents from the headers and footers
var csv_body = body.split("\r\n");
var csv_contents = csv_body[4];
res.send(csv_contents);
});
}
Expand Down
1 change: 1 addition & 0 deletions app/views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ html(lang="en")
form(action="/upload", method="post", enctype="multipart/form-data" id="importForm")
input#import_file(type='file' name="import.csv")
button#importAll(type='button')
div#duplicate_notification
form#searchForm
div.form-group
input#searchField.form-control(name='searchField', type='text', placeholder='search by first or last name')
Expand Down
42 changes: 37 additions & 5 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1003,23 +1003,55 @@ $(function() {
ssn_array.push(dataset[client]['ssn']);
}
// loop through array
var line_counter = 1;
var line_counter = 0;
var duplicate_lines = "";
for (var line in data_array){
if (data_array.hasOwnProperty(line)) {
//skip header row
if (line_counter != 0 && data_array.hasOwnProperty(line)) {
// for each line, check whether ssn exists via API
// if it does, put that record in a list of possible duplicates
if (ssn_array.indexOf(data_array[line][ssn_index]) > 0) {
console.log("DEBUG: line " + line_counter + " already exists");
duplicate_lines += "Line " + line_counter + " has a duplicate SSN<br>";
}
// if it doesn't, POST that record to the API
else {
console.log("DEBUG: ready to import line: " + line_counter);

// get line into correct format for POSTing
var new_client = {};
new_client['personalId'] = data_array[line][1];
new_client['firstName'] = data_array[line][2];
new_client['middleName'] = data_array[line][3];
new_client['lastName'] = data_array[line][4];
new_client['nameSuffix'] = data_array[line][5];
new_client['nameDataQuality'] = data_array[line][6];
new_client['ssn'] = data_array[line][7];
new_client['ssnDataQuality'] = data_array[line][8];
new_client['dob'] = data_array[line][9];
new_client['dobDataQuality'] = data_array[line][10];
new_client['amIndAKNative'] = data_array[line][11];
new_client['asian'] = data_array[line][12];
new_client['blackAfAmerican'] = data_array[line][13];
new_client['nativeHIOtherPacific'] = data_array[line][14];
new_client['white'] = data_array[line][15];
new_client['raceNone'] = data_array[line][16];
new_client['ethnicity'] = data_array[line][17];
new_client['gender'] = data_array[line][18];
new_client['otherGender'] = data_array[line][19];
new_client['veteranStatus'] = data_array[line][20];
new_client['dateCreated'] = data_array[line][21];
new_client['dateUpdated'] = data_array[line][22];
// do the POST!
$.ajax("/clients/", {
method: "POST",
data: new_client,
always: console.log("finished post")
});

}
}
line_counter++;
}
// display the list of possible duplicates to the user
$("#duplicate_notification").text(duplicate_lines);
});
}

Expand Down

1 comment on commit a7df443

@kfogel
Copy link
Member

@kfogel kfogel commented on a7df443 Aug 5, 2015 via email

Choose a reason for hiding this comment

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

Please sign in to comment.