-
Notifications
You must be signed in to change notification settings - Fork 124
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
Add CSV Import File Validations #1211
Changes from all commits
a824103
e0e5f86
53d1ae7
50ecffc
e922cd8
e93623c
3911c0e
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 |
---|---|---|
|
@@ -13,42 +13,64 @@ def initialize(file) | |
end | ||
|
||
def call | ||
CSV.foreach(@file.to_path, headers: true, skip_blanks: true).with_index(1) do |row, index| | ||
# Using Google Form headers | ||
email = row["Email"].downcase | ||
csv_timestamp = Time.parse(row["Timestamp"]) | ||
|
||
person = Person.find_by(email:, organization: @organization) | ||
previously_matched_form_submission = FormSubmission.where(person:, csv_timestamp:) | ||
|
||
if person.nil? | ||
@no_match << [index, email] | ||
elsif previously_matched_form_submission.present? | ||
next | ||
else | ||
latest_form_submission = person.latest_form_submission | ||
ActiveRecord::Base.transaction do | ||
# This checks for the empty form submission that is added when a person is created | ||
if latest_form_submission.csv_timestamp.nil? && latest_form_submission.form_answers.empty? | ||
latest_form_submission.update!(csv_timestamp:) | ||
create_form_answers(latest_form_submission, row) | ||
else | ||
# if the person submits a new/updated form, | ||
# i.e. an additional row in the csv with the same email / different timestamp, | ||
# a new form_submission will be created | ||
create_form_answers(FormSubmission.create!(person:, csv_timestamp:), row) | ||
catch(:halt_import) do | ||
validate_file | ||
|
||
CSV.foreach(@file.to_path, headers: true, skip_blanks: true).with_index(1) do |row, index| | ||
# Using Google Form headers | ||
email = row[@email_header].downcase | ||
csv_timestamp = Time.parse(row["Timestamp"]) | ||
|
||
person = Person.find_by(email:, organization: @organization) | ||
previously_matched_form_submission = FormSubmission.where(person:, csv_timestamp:) | ||
|
||
if person.nil? | ||
@no_match << [index, email] | ||
elsif previously_matched_form_submission.present? | ||
next | ||
else | ||
latest_form_submission = person.latest_form_submission | ||
ActiveRecord::Base.transaction do | ||
# This checks for the empty form submission that is added when a person is created | ||
if latest_form_submission.csv_timestamp.nil? && latest_form_submission.form_answers.empty? | ||
latest_form_submission.update!(csv_timestamp:) | ||
create_form_answers(latest_form_submission, row) | ||
else | ||
# if the person submits a new/updated form, | ||
# i.e. an additional row in the csv with the same email / different timestamp, | ||
# a new form_submission will be created | ||
create_form_answers(FormSubmission.create!(person:, csv_timestamp:), row) | ||
end | ||
@count += 1 | ||
end | ||
@count += 1 | ||
end | ||
rescue => e | ||
@errors << [index, e] | ||
end | ||
rescue => e | ||
@errors << [index, e] | ||
end | ||
Status.new(@errors.empty?, @count, @no_match, @errors) | ||
end | ||
|
||
private | ||
|
||
def validate_file | ||
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. In the vein of single responsibility, we could have separate validation methods for each validation. Might be something to consider if we add more validations. I am fine either way in this case. |
||
raise FileTypeError unless @file.content_type == "text/csv" | ||
|
||
first_row = CSV.foreach(@file.to_path).first | ||
raise FileEmptyError if first_row.nil? | ||
|
||
raise TimestampColumnError unless first_row.include?("Timestamp") | ||
|
||
email_headers = ["Email", "email", "Email Address", "email address"] | ||
email_headers.each do |e| | ||
@email_header = e if first_row.include?(e) | ||
end | ||
Comment on lines
+64
to
+67
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. This is the simplest way I can think of at the moment to allow both "Email" and "email". Since I had to do this change anyways, it is easy enough to add new naming variation's so I added the stakeholders as well. There isn't any extra logic to deal with the scenario of multiple headers having an email name. 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. Yeah I like this. |
||
raise EmailColumnError unless @email_header | ||
rescue FileTypeError, FileEmptyError, TimestampColumnError, EmailColumnError => e | ||
@errors << e | ||
throw :halt_import | ||
end | ||
|
||
def create_form_answers(form_submission, row) | ||
row.each do |col| | ||
next if col[0] == "Email" || col[0] == "Timestamp" | ||
|
@@ -61,6 +83,30 @@ def create_form_answers(form_submission, row) | |
) | ||
end | ||
end | ||
|
||
class EmailColumnError < StandardError | ||
def message | ||
'The column header "Email" was not found in the attached csv' | ||
end | ||
end | ||
|
||
class TimestampColumnError < StandardError | ||
def message | ||
'The column header "Timestamp" was not found in the attached csv' | ||
end | ||
end | ||
|
||
class FileTypeError < StandardError | ||
def message | ||
"Invalid File Type: File type must be CSV" | ||
end | ||
end | ||
|
||
class FileEmptyError < StandardError | ||
def message | ||
"File is empty" | ||
end | ||
end | ||
end | ||
end | ||
end |
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.
Nice work! I like the structure.