Skip to content

Commit

Permalink
External Form Upload: Display import summary / errors (#1208)
Browse files Browse the repository at this point in the history
* Add TODO comment

* get turbo working

* Swap to h1

* Replace with one partial

* Render new frame

* Swap heading and render one frame

* Render turbo frame

* Revert change, save for follow up

* Add newline

* Change to h5

* internationlize

* Add styling

* Fix lint

* Add test coverage

* Fix lint
  • Loading branch information
khiga8 authored Dec 16, 2024
1 parent 2672936 commit 5f8eaae
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,8 @@ def index

def create
authorize! :external_form_upload, context: {organization: Current.organization}

# Only processes single file upload
import = Organizations::Importers::CsvImportService.new(params[:files]).call

if import.success?
# do something
else
# import.errors
end
render turbo_stream: turbo_stream.replace("results", partial: "organizations/staff/external_form_upload/upload_results", locals: {import: import})
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<turbo-frame id="results">
<% if import.success? %>
<div class="alert alert-success">
<h5 class="alert-heading" role="alert"><%= t('.success_heading') %></h5>
<p><%= t('.submissions_added', count: import.count) %></p>
</div>
<% else %>
<div class="alert alert-danger">
<h5 class="alert-heading" role="alert"><%= t('.error_heading', count: import.errors.count) %></h5>
<p><%= t('.submissions_added', count: import.count) %></p>
<%= t('.review_errors', count: import.count) %>
<ul>
<% import.errors.each do |error| %>
<li>
<%= t('.row', number: error[0], message: error[1].message) %>
</li>
<% end %>
</ul>
</div>
<% end %>
</turbo-frame>
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
</div>
</div>
<% end %>
<%= turbo_frame_tag "results" %>
</div>
</div>
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,12 @@ en:
description: "If you collect information from adopters from a third party service, like Google Forms, you can export a CSV file from Google Forms and upload it here. We will import the questions and answers for any adopters who have an account on your Homeward Tails website, providing they use the same email address. Then you will be able to see the information for each adopter when reviewing applications."
header: "Upload CSV File"
csv_header_requirements: "Your CSV file must have column headers of 'Email' and 'Timestamp' for the import to work."
upload_results:
success_heading: "File successfully scanned"
error_heading: "File scanned: %{count} error(s) present"
submissions_added: "%{count} new form submissions were added."
review_errors: "Review the following error(s) to ensure all form submissions are added:"
row: "Row %{number} - %{message}"
adopter_fosterer:
adopter_applications:
create:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,54 @@
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
context "success" do
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

should "Creates new form submission" do
assert_difference "@adopter.person.form_submissions.count" do
post staff_external_form_upload_index_path, params: @params
end
end

should "It does not create form answers for adopter2" do
assert_no_difference "@adopter2.person.form_submissions.count" do
post staff_external_form_upload_index_path, params: @params
end
end

should "shows success feedback" do
post staff_external_form_upload_index_path, params: @params, as: :turbo_stream

test "Creates new form submission" do
assert_difference "@adopter.person.form_submissions.count" do
post staff_external_form_upload_index_path, params: @params
assert_response :success
assert_turbo_stream(action: "replace", count: 1) do
assert_select "h5", text: "File successfully scanned"
end
end
end

test "It does not create form answers for adopter2" do
assert_no_difference "@adopter2.person.form_submissions.count" do
post staff_external_form_upload_index_path, params: @params
context "error" do
setup do
file = fixture_file_upload("google_form_error_sample.csv", "text/csv")
@params = {files: file}
admin = create(:admin)
create(:adopter, email: "[email protected]")
sign_in admin
end

should "shows error feedback" do
post staff_external_form_upload_index_path, params: @params, as: :turbo_stream

assert_response :success
assert_turbo_stream(action: "replace", count: 1) do
assert_select "h5", text: "File scanned: 1 error(s) present"
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/files/google_form_error_sample.csv
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/13/05 7:14:21 p. m. GMT-4,dsf,[email protected],dsfsdf,sdfsdf,ds

0 comments on commit 5f8eaae

Please sign in to comment.