-
Notifications
You must be signed in to change notification settings - Fork 107
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
1033 external form upload #1038
base: main
Are you sure you want to change the base?
Changes from 4 commits
27a9557
0a3b161
93f379b
e8c7720
06737d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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? | ||
|
||
ActiveRecord::Base.transaction do | ||
form_submission = FormSubmission.create!(person:, csv_timestamp:) | ||
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 | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require "test_helper" | ||
|
||
class Organizations::Staff::ExternalFormUploadControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@organization = ActsAsTenant.current_tenant | ||
@file = fixture_file_upload("google_form_sample.csv", "text/csv") | ||
@params = {files: [@file]} | ||
@user = create(:admin, email: "[email protected]") | ||
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. We should be creating an There are also some changes happening in this PR #997
This means the asserstions on lines 20 and 21 will become unnecessary. We'll want to just test two cases
I think the best thing to do, here, is wait for that PR to get merged, then finish these tests off. Thoughts? 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. Ok, I understand lines 20 and 21 can be removed. On the other assertions I'm verifying that FormSubmissions and FormAnswers are being created after the upload csv. Where we have 4 answers and questions/cells and 1 form was uploaded so one FormSubmission was created. As I understood we may need to add a test for when a csv file matches an existing csv_timestamp entry... Nothing should happen. But all of these are subject to change after PR #997 Then I will remove the unnecessary assertions, address controller comments, and wait for your instructions on the tests. |
||
@user2 = create(:admin) | ||
sign_in @user | ||
end | ||
|
||
test "Import from google_form_sample.csv" do | ||
# FomAnswers and FormSubmissions should be created | ||
assert_difference "FormAnswer.count", 4 do | ||
assert_difference "FormSubmission.count", 1 do | ||
post staff_external_form_upload_index_path, params: @params | ||
end | ||
end | ||
# There should be a form submission for @user but not for @user2 | ||
# assert FormSubmission.exists?(person_id: @user.id) | ||
assert_not FormSubmission.exists?(person_id: @user2.id) | ||
end | ||
end |
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 |
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.
I think we should only really allow 1 upload here...it doesn't make sense for more than one file.
Given we are using
app/views/organizations/staff/shared/_attachment_form.html.erb
partial, we could make the value for themultiple
attribute an argument that gets passed to the partial. So in this case, it would befalse
and in the other place the partial is used, on the pet, it would betrue
. Thoughts?Then we can get rid of this loop here.
Also, curious of the benefit of checking
file.is_a?(ActionDispatch::Http::UploadedFile)
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.
file.is_a?(ActionDispatch::Http::UploadedFile)
It is because the received parameter contains other things like""
. Maybe It will be unnecessary when using just one file.I don't understand completely the use case due to a lack of background knowledge of the project. If you think a single file is the way to go, I will implement the suggested changes.
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.
Yes this import service is making the assumption there is only one third party form, and therefore one CSV to upload. It doesn’t currently handle the case where adopter data lives across multiple forms. So it would be good to only allow one file for now.