Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .allow_skipping_tests
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ services/create_all_casa_admin_service.rb
services/create_casa_admin_service.rb
services/mileage_export_csv_service.rb
validators/court_report_validator.rb
validators/user_validator.rb
values/all_casa_admin_parameters.rb
values/casa_admin_parameters.rb
values/supervisor_parameters.rb
Expand Down
8 changes: 5 additions & 3 deletions app/lib/importers/supervisor_importer.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
class SupervisorImporter < FileImporter
IMPORT_HEADER = ["email", "display_name", "supervisor_volunteers"]
IMPORT_HEADER = ["email", "display_name", "supervisor_volunteers", "phone_number"]

def self.import_supervisors(csv_filespec, org_id)
new(csv_filespec, org_id).import_supervisors
end

def initialize(csv_filespec, org_id)
super(csv_filespec, org_id, "supervisors", ["email", "display_name", "supervisor_volunteers"])
super(csv_filespec, org_id, "supervisors", ["email", "display_name", "supervisor_volunteers", "phone_number"])
end

def import_supervisors
import do |row|
supervisor_params = row.to_hash.slice(:display_name, :email).compact
supervisor_params = row.to_hash.slice(:display_name, :email, :phone_number).compact

unless supervisor_params.key?(:email)
raise "Row does not contain e-mail address."
end

supervisor_params[:phone_number] = supervisor_params.key?(:phone_number) ? "+#{supervisor_params[:phone_number]}" : ""

supervisor = Supervisor.find_by(email: supervisor_params[:email])
volunteer_assignment_list = email_addresses_to_users(Volunteer, String(row[:supervisor_volunteers]))

Expand Down
1 change: 1 addition & 0 deletions app/models/casa_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def change_to_supervisor!
# invited_by_type :string
# last_sign_in_at :datetime
# last_sign_in_ip :string
# phone_number :string default("")
# reset_password_sent_at :datetime
# reset_password_token :string
# sign_in_count :integer default(0), not null
Expand Down
1 change: 1 addition & 0 deletions app/models/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def recently_unassigned_volunteers
# invited_by_type :string
# last_sign_in_at :datetime
# last_sign_in_ip :string
# phone_number :string default("")
# reset_password_sent_at :datetime
# reset_password_token :string
# sign_in_count :integer default(0), not null
Expand Down
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class User < ApplicationRecord
include Roles
include ByOrganizationScope

validates_with UserValidator

has_paper_trail
devise :database_authenticatable, :invitable, :recoverable, :validatable, :timeoutable, :trackable

Expand Down Expand Up @@ -175,6 +177,7 @@ def last_deactivated_by
# invited_by_type :string
# last_sign_in_at :datetime
# last_sign_in_ip :string
# phone_number :string default("")
# reset_password_sent_at :datetime
# reset_password_token :string
# sign_in_count :integer default(0), not null
Expand Down
1 change: 1 addition & 0 deletions app/models/volunteer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def cases_where_contact_made_in_days(num_days = CONTACT_MADE_IN_DAYS_NUM)
# invited_by_type :string
# last_sign_in_at :datetime
# last_sign_in_ip :string
# phone_number :string default("")
# reset_password_sent_at :datetime
# reset_password_token :string
# sign_in_count :integer default(0), not null
Expand Down
32 changes: 32 additions & 0 deletions app/validators/user_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class UserValidator < ActiveModel::Validator
VALID_PHONE_NUMBER_LENGTH = 12
VALID_COUNTRY_CODE = "+1"
PHONE_NUMBER_ERROR = "Phone number is not in correct format: 1XXXXXXXXXX"

def validate(record)
if !valid_phone_number_contents(record.phone_number)
record.errors.add(:phone_number, PHONE_NUMBER_ERROR)
end
end

private

def valid_phone_number_contents(number)
if number.empty?
return true
end

if number.length != VALID_PHONE_NUMBER_LENGTH
return false
end

country_code = number[0..1]
phone_number = number[2..number.length]

if country_code != VALID_COUNTRY_CODE || !phone_number.scan(/\D/).empty?
return false
end

true
end
end
5 changes: 5 additions & 0 deletions db/migrate/20220402201247_add_phone_number_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPhoneNumberToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :phone_number, :string, default: ""
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2022_03_23_145733) do
ActiveRecord::Schema[7.0].define(version: 2022_04_02_201247) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -400,6 +400,7 @@
t.datetime "last_sign_in_at", precision: nil
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "phone_number", default: ""
t.index ["casa_org_id"], name: "index_users_on_casa_org_id"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true
Expand Down
1 change: 1 addition & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def initialize
@rng = Random.new(random_seed) # rng = random number generator
@db_populator = DbPopulator.new(rng)
Faker::Config.random = rng
Faker::Config.locale = "en-US" # only allow US phone numbers
end

def seed
Expand Down
1 change: 1 addition & 0 deletions db/seeds/db_populator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def create_users(casa_org, options)
password: SEED_PASSWORD,
password_confirmation: SEED_PASSWORD,
display_name: Faker::Name.name,
phone_number: Faker::PhoneNumber.cell_phone_in_e164,
active: true
}
# Approximately 1 out of 30 volunteers should be set to inactive.
Expand Down
6 changes: 3 additions & 3 deletions public/supervisors.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
email,display_name,supervisor_volunteers
supervisor1@example.net,Supervisor One,volunteer1@example.net
supervisor2@example.net,Supervisor Two,"volunteer2@example.net, volunteer3@example.net"
email,display_name,supervisor_volunteers,phone_number
supervisor1@example.net,Supervisor One,volunteer1@example.net,11111111111
supervisor2@example.net,Supervisor Two,"volunteer2@example.net, volunteer3@example.net",11111111111
6 changes: 3 additions & 3 deletions spec/fixtures/supervisor_volunteers.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
email,display_name,supervisor_volunteers
s5@example.com,s5,volunteer1@example.net
s6@example.com,s6,volunteer1@example.net
email,display_name,supervisor_volunteers,phone_number
s5@example.com,s5,volunteer1@example.net,11111111111
s6@example.com,s6,volunteer1@example.net,11111111111
8 changes: 4 additions & 4 deletions spec/fixtures/supervisors.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
email,display_name,supervisor_volunteers
supervisor1@example.net,Supervisor One,volunteer1@example.net
supervisor2@example.net,Supervisor Two,"volunteer2@example.net, volunteer3@example.net"
supervisor3@example.net,Supervisor Three,
email,display_name,supervisor_volunteers,phone_number
supervisor1@example.net,Supervisor One,volunteer1@example.net,11111111111
supervisor2@example.net,Supervisor Two,"volunteer2@example.net, volunteer3@example.net",11111111111
supervisor3@example.net,Supervisor Three,,11111111111
5 changes: 5 additions & 0 deletions spec/fixtures/supervisors_invalid_phone_numbers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
email,display_name,supervisor_volunteers,phone_number
supervisor1@example.net,Supervisor One,volunteer1@example.net,12345678
supervisor2@example.net,Supervisor Two,"volunteer2@example.net, volunteer3@example.net",+++++++++++
supervisor3@example.net,Supervisor Three,,111111111111111111
supervisor4@example.net,Supervisor Four,,1.111.111.1111
6 changes: 3 additions & 3 deletions spec/fixtures/supervisors_without_email.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
email,display_name,supervisor_volunteers
supervisor1@example.net,Supervisor One,volunteer1@example.net
,Supervisor Two,"volunteer2@example.net, volunteer3@example.net"
email,display_name,supervisor_volunteers,phone_number
supervisor1@example.net,Supervisor One,volunteer1@example.net,11111111111
,Supervisor Two,"volunteer2@example.net, volunteer3@example.net",11111111111
4 changes: 4 additions & 0 deletions spec/fixtures/supervisors_without_phone_numbers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
email,display_name,supervisor_volunteers,phone_number
supervisor1@example.net,Supervisor One,volunteer1@example.net,
supervisor2@example.net,Supervisor Two,"volunteer2@example.net, volunteer3@example.net",
supervisor3@example.net,Supervisor Three,,
32 changes: 32 additions & 0 deletions spec/lib/importers/supervisor_importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@
existing_supervisor.reload
}.to change(existing_supervisor, :display_name).to("Supervisor Two")
end

it "updates phone number to valid number" do
expect {
supervisor_importer.import_supervisors
existing_supervisor.reload
}.to change(existing_supervisor, :phone_number).to("+11111111111")
end
end

context "when row doesn't have e-mail address" do
Expand All @@ -91,6 +98,31 @@
end
end

context "when row doesn't have phone number" do
let(:supervisor_import_data_path) { Rails.root.join("spec", "fixtures", "supervisors_without_phone_numbers.csv") }

let!(:existing_supervisor_with_number) { create(:supervisor, display_name: "#", email: "supervisor1@example.net", phone_number: "+11111111111") }

it "updates phone number to be deleted" do
expect {
supervisor_importer.import_supervisors
existing_supervisor_with_number.reload
}.to change(existing_supervisor_with_number, :phone_number).to("")
end
end

context "when phone number in row is invalid" do
let(:supervisor_import_data_path) { Rails.root.join("spec", "fixtures", "supervisors_invalid_phone_numbers.csv") }

it "returns an error message" do
alert = supervisor_importer.import_supervisors

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

specify "static and instance methods have identical results" do
SupervisorImporter.new(supervisor_import_data_path, casa_org_id).import_supervisors
data_using_instance = Supervisor.pluck(:email).sort
Expand Down