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

Add CSV Import File Validations #1211

Merged
merged 7 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 73 additions & 27 deletions app/services/organizations/importers/google_csv_import_service.rb
Copy link
Collaborator

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.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

The 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"
Expand All @@ -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
42 changes: 42 additions & 0 deletions test/services/organizations/csv_import_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class CsvImportServiceTest < ActiveSupport::TestCase
Current.organization = adopter.organization

@file = Tempfile.new(["test", ".csv"])
@file.stubs(:content_type).returns("text/csv")

headers = ["Timestamp", "First name", "Last name", "Email", "Address", "Phone number", *Faker::Lorem.questions]

@data = [
Expand Down Expand Up @@ -110,5 +112,45 @@ class CsvImportServiceTest < ActiveSupport::TestCase
refute import.success?
assert_equal "mon out of range", import.errors[0][1].message
end

should "validate file type" do
file = Tempfile.new(["test", ".png"])
file.stubs(:content_type).returns("image/png")
import = Organizations::Importers::GoogleCsvImportService.new(file).call

assert_equal "Invalid File Type: File type must be CSV", import.errors.first.message
end

should "validate empty file" do
file = Tempfile.new(["test", ".csv"])
file.stubs(:content_type).returns("text/csv")
import = Organizations::Importers::GoogleCsvImportService.new(file).call

assert_equal "File is empty", import.errors.first.message
end

should "validate email header" do
file = Tempfile.new(["test", ".csv"])
headers = ["Timestamp", "First name", "Last name", "Address", "Phone number", *Faker::Lorem.questions]
CSV.open(file.path, "wb") do |csv|
csv << headers
end
file.stubs(:content_type).returns("text/csv")
import = Organizations::Importers::GoogleCsvImportService.new(file).call

assert_equal 'The column header "Email" was not found in the attached csv', import.errors.first.message
end

should "validate Timestamp header" do
file = Tempfile.new(["test", ".csv"])
headers = ["Email", "First name", "Last name", "Address", "Phone number", *Faker::Lorem.questions]
CSV.open(file.path, "wb") do |csv|
csv << headers
end
file.stubs(:content_type).returns("text/csv")
import = Organizations::Importers::GoogleCsvImportService.new(file).call

assert_equal 'The column header "Timestamp" was not found in the attached csv', import.errors.first.message
end
end
end
Loading