From 0bd4c5f46785bbcac0040cfc54f38933cdb3c410 Mon Sep 17 00:00:00 2001 From: frankljin <52306663+frankljin@users.noreply.github.com> Date: Fri, 15 Apr 2022 00:14:42 -0400 Subject: [PATCH 1/3] add phone_number to volunteer importer --- app/lib/importers/volunteer_importer.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/lib/importers/volunteer_importer.rb b/app/lib/importers/volunteer_importer.rb index 6cfca78f95..4485c3730c 100644 --- a/app/lib/importers/volunteer_importer.rb +++ b/app/lib/importers/volunteer_importer.rb @@ -1,22 +1,24 @@ class VolunteerImporter < FileImporter - IMPORT_HEADER = ["display_name", "email"] + IMPORT_HEADER = ["display_name", "email", "phone_number"] def self.import_volunteers(csv_filespec, org_id) new(csv_filespec, org_id).import_volunteers end def initialize(csv_filespec, org_id) - super(csv_filespec, org_id, "volunteers", ["display_name", "email"]) + super(csv_filespec, org_id, "volunteers", ["display_name", "email", "phone_number"]) end def import_volunteers import do |row| - volunteer_params = row.to_hash.slice(:display_name, :email).compact + volunteer_params = row.to_hash.slice(:display_name, :email, :phone_number).compact unless volunteer_params.key?(:email) raise "Row does not contain an e-mail address." end + volunteer_params[:phone_number] = volunteer_params.key?(:phone_number) ? "+#{volunteer_params[:phone_number]}" : "" + volunteer = Volunteer.find_by(email: volunteer_params[:email]) if volunteer # Volunteer exists try to update it From 837f2376349ed02831de3317eb591ba528837064 Mon Sep 17 00:00:00 2001 From: frankljin <52306663+frankljin@users.noreply.github.com> Date: Fri, 15 Apr 2022 00:15:00 -0400 Subject: [PATCH 2/3] testing for volunteer phone_number imports --- public/volunteers.csv | 8 ++--- spec/fixtures/volunteers.csv | 8 ++--- spec/fixtures/volunteers_without_email.csv | 7 ++-- spec/lib/importers/volunteer_importer_spec.rb | 32 +++++++++++++++++++ 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/public/volunteers.csv b/public/volunteers.csv index 03e5d393ce..7a9506910c 100644 --- a/public/volunteers.csv +++ b/public/volunteers.csv @@ -1,4 +1,4 @@ -display_name,email -Volunteer One,volunteer1@example.net -Volunteer Two,volunteer2@example.net -Volunteer Three,volunteer3@example.net \ No newline at end of file +display_name,email,phone_number +Volunteer One,volunteer1@example.net,11234567890 +Volunteer Two,volunteer2@example.net,11234567891 +Volunteer Three,volunteer3@example.net,11234567892 \ No newline at end of file diff --git a/spec/fixtures/volunteers.csv b/spec/fixtures/volunteers.csv index 03e5d393ce..7a9506910c 100644 --- a/spec/fixtures/volunteers.csv +++ b/spec/fixtures/volunteers.csv @@ -1,4 +1,4 @@ -display_name,email -Volunteer One,volunteer1@example.net -Volunteer Two,volunteer2@example.net -Volunteer Three,volunteer3@example.net \ No newline at end of file +display_name,email,phone_number +Volunteer One,volunteer1@example.net,11234567890 +Volunteer Two,volunteer2@example.net,11234567891 +Volunteer Three,volunteer3@example.net,11234567892 \ No newline at end of file diff --git a/spec/fixtures/volunteers_without_email.csv b/spec/fixtures/volunteers_without_email.csv index 79527f293d..e218d1832a 100644 --- a/spec/fixtures/volunteers_without_email.csv +++ b/spec/fixtures/volunteers_without_email.csv @@ -1,3 +1,4 @@ -display_name,email -Volunteer One,volunteer1@example.net -Volunteer Two, +display_name,email,phone_number +Volunteer One,volunteer1@example.net,11111111111 +Volunteer Two,11111111111 +Volunteer Three,,11111111111 diff --git a/spec/lib/importers/volunteer_importer_spec.rb b/spec/lib/importers/volunteer_importer_spec.rb index 208e8d9f13..8f50c1523e 100644 --- a/spec/lib/importers/volunteer_importer_spec.rb +++ b/spec/lib/importers/volunteer_importer_spec.rb @@ -49,6 +49,13 @@ existing_volunteer.reload }.to change(existing_volunteer, :display_name).to("Volunteer One") end + + it "updates phone number to valid number" do + expect { + volunteer_importer.call + existing_volunteer.reload + }.to change(existing_volunteer, :phone_number).to("+11234567890") + end end context "when row doesn't have e-mail address" do @@ -62,4 +69,29 @@ expect(alert[:exported_rows]).to include("Row does not contain an e-mail address.") end end + + context "when row doesn't have phone number" do + let(:import_file_path) { Rails.root.join("spec", "fixtures", "volunteers_without_phone_numbers.csv") } + + let!(:existing_volunteer_with_number) { create(:volunteer, display_name: "#", email: "volunteer2@example.net", phone_number: "+11111111111") } + + it "updates phone number to be deleted" do + expect { + volunteer_importer.call + existing_volunteer_with_number.reload + }.to change(existing_volunteer_with_number, :phone_number).to("") + end + end + + context "when phone number in row is invalid" do + let(:import_file_path) { Rails.root.join("spec", "fixtures", "volunteers_invalid_phone_numbers.csv") } + + it "returns an error message" do + alert = volunteer_importer.call + + expect(alert[:type]).to eq(:error) + expect(alert[:message]).to eq("Not all rows were imported.") + expect(alert[:exported_rows]).to include("Phone number is not in correct format: 1XXXXXXXXXX") + end + end end From e653a296376a319a21d1aa2ce63d3680f413ab39 Mon Sep 17 00:00:00 2001 From: frankljin <52306663+frankljin@users.noreply.github.com> Date: Fri, 15 Apr 2022 00:23:04 -0400 Subject: [PATCH 3/3] more test files --- spec/fixtures/volunteers_invalid_phone_numbers.csv | 4 ++++ spec/fixtures/volunteers_without_phone_numbers.csv | 3 +++ 2 files changed, 7 insertions(+) create mode 100644 spec/fixtures/volunteers_invalid_phone_numbers.csv create mode 100644 spec/fixtures/volunteers_without_phone_numbers.csv diff --git a/spec/fixtures/volunteers_invalid_phone_numbers.csv b/spec/fixtures/volunteers_invalid_phone_numbers.csv new file mode 100644 index 0000000000..041fb27d87 --- /dev/null +++ b/spec/fixtures/volunteers_invalid_phone_numbers.csv @@ -0,0 +1,4 @@ +display_name,email,phone_number +Volunteer One,volunteer1@example.net,22222222222 +Volunteer Two,volunteer2@example.net,1bc12345678 +Volunteer Three,volunteer3@example.net,1111111111 \ No newline at end of file diff --git a/spec/fixtures/volunteers_without_phone_numbers.csv b/spec/fixtures/volunteers_without_phone_numbers.csv new file mode 100644 index 0000000000..495abe83cc --- /dev/null +++ b/spec/fixtures/volunteers_without_phone_numbers.csv @@ -0,0 +1,3 @@ +display_name,email,phone_number +Volunteer One,volunteer1@example.net,11111111111 +Volunteer Two,volunteer2@example.net, \ No newline at end of file