Skip to content

Commit c1fec92

Browse files
authored
Display errors taking place during import partner process (#4707)
* Remove duplicate validation errors form PartnerCreateService * Allow Importable to display error message based on each model's import_csv implementation * Add request tests * Display import errors with correct flash styling * Update test * Update logic for 'import_csv' methods * Refactor code
1 parent 64d20c1 commit c1fec92

File tree

8 files changed

+54
-18
lines changed

8 files changed

+54
-18
lines changed

app/controllers/concerns/importable.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ def import_csv
2727
data = File.read(params[:file].path, encoding: "BOM|UTF-8")
2828
csv = CSV.parse(data, headers: true).reject { |row| row.to_hash.values.any?(&:nil?) }
2929
if csv.count.positive? && csv.first.headers.all? { |header| !header.nil? }
30-
resource_model.import_csv(csv, current_organization.id)
31-
flash[:notice] = "#{resource_model_humanized} were imported successfully!"
30+
errors = resource_model.import_csv(csv, current_organization.id)
31+
if errors.empty?
32+
flash[:notice] = "#{resource_model_humanized} were imported successfully!"
33+
else
34+
flash[:error] = "The following #{resource_model_humanized} did not import successfully:\n#{errors.join("\n")}"
35+
end
3236
else
3337
flash[:error] = "Check headers in file!"
3438
end

app/models/concerns/provideable.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def self.import_csv(csv, organization)
1919

2020
loc.save!
2121
end
22+
[]
2223
end
2324

2425
def self.csv_export_headers

app/models/donation_site.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def self.import_csv(csv, organization)
4646
loc.organization_id = organization
4747
loc.save!
4848
end
49+
[]
4950
end
5051

5152
def self.csv_export_headers

app/models/partner.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,19 @@ def approvable?
158158

159159
# better to extract this outside of the model
160160
def self.import_csv(csv, organization_id)
161+
errors = []
161162
organization = Organization.find(organization_id)
162163

163164
csv.each do |row|
164165
hash_rows = Hash[row.to_hash.map { |k, v| [k.downcase, v] }]
165166

166167
svc = PartnerCreateService.new(organization: organization, partner_attrs: hash_rows)
167168
svc.call
169+
if svc.errors.present?
170+
errors << "#{svc.partner.name}: #{svc.partner.errors.full_messages.to_sentence}"
171+
end
168172
end
173+
errors
169174
end
170175

171176
def self.csv_export_headers

app/models/storage_location.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def self.import_csv(csv, organization)
121121
loc.organization_id = organization
122122
loc.save!
123123
end
124+
[]
124125
end
125126

126127
# NOTE: We should generalize this elsewhere -- Importable concern?

app/services/partner_create_service.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@ def initialize(organization:, partner_attrs:)
1111
def call
1212
@partner = organization.partners.build(partner_attrs)
1313

14-
unless @partner.valid?
14+
if @partner.valid?
15+
ActiveRecord::Base.transaction do
16+
@partner.save!
17+
18+
Partners::Profile.create!({
19+
partner_id: @partner.id,
20+
name: @partner.name,
21+
enable_child_based_requests: organization.enable_child_based_requests,
22+
enable_individual_requests: organization.enable_individual_requests,
23+
enable_quantity_based_requests: organization.enable_quantity_based_requests
24+
})
25+
rescue StandardError => e
26+
errors.add(:base, e.message)
27+
raise ActiveRecord::Rollback
28+
end
29+
else
1530
@partner.errors.each do |error|
1631
errors.add(error.attribute, error.message)
1732
end
1833
end
1934

20-
ActiveRecord::Base.transaction do
21-
@partner.save!
22-
23-
Partners::Profile.create!({
24-
partner_id: @partner.id,
25-
name: @partner.name,
26-
enable_child_based_requests: organization.enable_child_based_requests,
27-
enable_individual_requests: organization.enable_individual_requests,
28-
enable_quantity_based_requests: organization.enable_quantity_based_requests
29-
})
30-
rescue StandardError => e
31-
errors.add(:base, e.message)
32-
raise ActiveRecord::Rollback
33-
end
34-
3535
self
3636
end
3737

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name,email
2+
3+
Partner 2,this is not an email address
4+

spec/requests/partners_requests_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,26 @@
222222
expect(response).to have_error "Check headers in file!"
223223
end
224224
end
225+
226+
context "csv file with invalid email address" do
227+
let(:file) { fixture_file_upload("partners_with_invalid_email.csv", "text/csv") }
228+
subject { post import_csv_partners_path, params: { file: file } }
229+
230+
it "invokes .import_csv" do
231+
expect(model_class).to respond_to(:import_csv).with(2).arguments
232+
end
233+
234+
it "redirects to :index" do
235+
subject
236+
expect(response).to be_redirect
237+
end
238+
239+
it "presents a flash notice message displaying the import errors" do
240+
subject
241+
expect(response).to have_error(/The following #{model_class.name.underscore.humanize.pluralize} did not import successfully:/)
242+
expect(response).to have_error(/Partner 2: Email is invalid/)
243+
end
244+
end
225245
end
226246

227247
describe "POST #create" do

0 commit comments

Comments
 (0)