-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adds route and controller method to use the importer to import external csv forms with google_csv_import_service * adds test for external_form_upload#create * Solve linter and test issues * Adds multiple param to partial Modify external form controller to support just one file upload correct controller test * Add new test and change to nested modules * add latest_form_submission * - refactors google_csv_import_service - Updates users Factorie to create the person - fix external_form_upload_controller_tests * Add csv_import_service_tests tests * Apply Dry principle adding method * Improve naming * admin variable
- Loading branch information
1 parent
389ed26
commit c4a3c32
Showing
13 changed files
with
133 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
app/services/organizations/importers/google_csv_import_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require "csv" | ||
|
||
module Organizations | ||
module Importers | ||
class GoogleCsvImportService | ||
def initialize(file) | ||
@file = file | ||
@organization = Current.organization | ||
end | ||
|
||
def call | ||
CSV.foreach(@file.to_path, headers: true, skip_blanks: true) do |row| | ||
# Using Google Form headers | ||
email = row["Email"].downcase | ||
csv_timestamp = Time.parse(row["Timestamp"]) | ||
|
||
person = Person.find_by(email:, organization: @organization) | ||
previous = FormSubmission.where(person:, csv_timestamp:) | ||
next unless person && previous.empty? | ||
|
||
latest_form_submission = person.latest_form_submission | ||
|
||
if latest_form_submission.form_answers.empty? | ||
create_form_answers(latest_form_submission, row) | ||
else | ||
create_form_answers(FormSubmission.create!(person:, csv_timestamp:), row) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def create_form_answers(form_submission, row) | ||
ActiveRecord::Base.transaction do | ||
row.each do |col| | ||
next if col[0] == "Email" || col[0] == "Timestamp" | ||
|
||
FormAnswer.create!(form_submission:, | ||
question_snapshot: col[0], value: col[1]) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<div class="mb-4"> | ||
<%= render "organizations/staff/shared/attachment_form", instance: @pet, title: 'Files', url: attach_files_staff_pet_path(@pet), attachment_type: 'files' %> | ||
<%= render "organizations/staff/shared/attachment_form", multiple: true, instance: @pet, title: 'Files', url: attach_files_staff_pet_path(@pet), attachment_type: 'files' %> | ||
<%= render "organizations/shared/file_attachment_table", pet: @pet %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<div class="mb-4"> | ||
<%= render "organizations/staff/shared/attachment_form", instance: @pet, title: 'Photos', url: attach_images_staff_pet_path(@pet), attachment_type: 'images' %> | ||
<%= render "organizations/staff/shared/attachment_form", multiple: true, instance: @pet, title: 'Photos', url: attach_images_staff_pet_path(@pet), attachment_type: 'images' %> | ||
<%= render ImageAttachmentTableComponent.new(images: @pet.images) %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
test/controllers/organizations/staff/external_form_upload_controller_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require "test_helper" | ||
|
||
module Organizations | ||
module Staff | ||
class ExternalFormUploadControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
file = fixture_file_upload("google_form_sample.csv", "text/csv") | ||
@params = {files: [file]} | ||
admin = create(:admin) | ||
@adopter = create(:adopter, email: "[email protected]") | ||
@adopter2 = create(:adopter, email: "[email protected]") | ||
sign_in admin | ||
end | ||
|
||
test "Creates form answers for adopter in its latest form submission" do | ||
assert_changes -> { @adopter.latest_form_submission.form_answers.count } do | ||
post staff_external_form_upload_index_path, params: @params | ||
end | ||
end | ||
|
||
test "It does not create form answers for adopter2" do | ||
assert_no_difference -> { @adopter2.latest_form_submission.form_answers.count } do | ||
post staff_external_form_upload_index_path, params: @params | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Timestamp,Nombre,Email,Dirección,Número de teléfono,Comentarios | ||
2024/10/05 7:14:21 p. m. GMT-4,dsf,[email protected],dsfsdf,sdfsdf,ds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,22 +4,24 @@ | |
module Organizations | ||
class CsvImportServiceTest < ActiveSupport::TestCase | ||
setup do | ||
person = create(:person) | ||
Current.organization = person.organization | ||
adopter = create(:adopter) | ||
Current.organization = adopter.organization | ||
|
||
@file = Tempfile.new(["test", ".csv"]) | ||
headers = ["Timestamp", "First name", "Last name", "Email", "Address", "Phone number", *Faker::Lorem.questions] | ||
|
||
@data = [ | ||
Time.now, | ||
person.first_name, | ||
person.last_name, | ||
person.email, | ||
"2024-10-02 12:45:37.000000000 +0000", | ||
adopter.first_name, | ||
adopter.last_name, | ||
adopter.email, | ||
Faker::Address.full_address, | ||
Faker::PhoneNumber.phone_number, | ||
*Faker::Lorem.sentences | ||
] | ||
|
||
@adopter = adopter | ||
|
||
CSV.open(@file.path, "wb") do |csv| | ||
csv << headers | ||
end | ||
|
@@ -29,26 +31,26 @@ class CsvImportServiceTest < ActiveSupport::TestCase | |
@file.unlink | ||
end | ||
|
||
should "add row information to database if person exists" do | ||
should "add row information to database if adopter exists" do | ||
CSV.open(@file.path, "ab") do |csv| | ||
csv << @data | ||
end | ||
|
||
assert_difference "FormSubmission.count" do | ||
assert_no_difference "FormSubmission.count" do | ||
assert_difference("FormAnswer.count", + 7) do | ||
Organizations::CsvImportService.new(@file).call | ||
Organizations::Importers::GoogleCsvImportService.new(@file).call | ||
end | ||
end | ||
end | ||
|
||
should "skip row if person with email does not exist" do | ||
should "skip row if adopter with email does not exist" do | ||
@data[3] = "[email protected]" | ||
CSV.open(@file.path, "ab") do |csv| | ||
csv << @data | ||
end | ||
|
||
assert_no_difference "FormSubmission.count" do | ||
Organizations::CsvImportService.new(@file).call | ||
Organizations::Importers::GoogleCsvImportService.new(@file).call | ||
end | ||
end | ||
|
||
|
@@ -58,7 +60,32 @@ class CsvImportServiceTest < ActiveSupport::TestCase | |
csv << @data | ||
end | ||
assert_difference "FormSubmission.count" do | ||
Organizations::CsvImportService.new(@file).call | ||
Organizations::Importers::GoogleCsvImportService.new(@file).call | ||
end | ||
end | ||
|
||
should "skip if the user exists and the timestamp matches that on the FormSubmisson" do | ||
CSV.open(@file.path, "ab") do |csv| | ||
csv << @data | ||
end | ||
@adopter.latest_form_submission.update(csv_timestamp: @data[0]) | ||
|
||
assert_no_difference -> { @adopter.latest_form_submission.form_answers.count } do | ||
Organizations::Importers::GoogleCsvImportService.new(@file).call | ||
end | ||
end | ||
|
||
should "creates a new form submission and adds the form answers if there is no 'empty' form submission and the timestamp is different" do | ||
CSV.open(@file.path, "ab") do |csv| | ||
csv << @data | ||
end | ||
Organizations::Importers::GoogleCsvImportService.new(@file).call | ||
@adopter.latest_form_submission.update(csv_timestamp: "2024-10-03 12:45:37.000000000 +0000") | ||
|
||
assert_difference -> { @adopter.person.form_submissions.count } do | ||
assert_difference -> { @adopter.person.form_answers.count }, 7 do | ||
Organizations::Importers::GoogleCsvImportService.new(@file).call | ||
end | ||
end | ||
end | ||
end | ||
|