Skip to content

Commit

Permalink
SheerID school fixes (#949)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dantemss authored Apr 2, 2021
1 parent 6792d24 commit 8a77301
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,53 +34,58 @@ def exec(verification_id:)
return
end

# Set the user's sheerid_verification_id only if they didn't already have one
# VerifyEducator always sets it and we don't want to overwrite the approved one
existing_user.sheerid_verification_id = verification_id \
if verification_id.present? && existing_user.sheerid_verification_id.blank?

# Update the user account with the data returned from SheerID
if verification_details.relevant?
existing_user.first_name = verification.first_name
existing_user.last_name = verification.last_name
existing_user.sheerid_reported_school = verification.organization_name
existing_user.faculty_status = verification.current_step_to_faculty_status

# Attempt to exactly match a school based on the sheerid_reported_school field
school = School.find_by sheerid_school_name: existing_user.sheerid_reported_school

if school.nil?
# No exact match found, so attempt to fuzzy match the school name
match = SheeridAPI::SHEERID_REGEX.match existing_user.sheerid_reported_school
name = match[1]
city = match[2]
state = match[3]

# Sometimes the city and/or state are duplicated, so remove them
name = name.chomp(" (#{city})") unless city.nil?
name = name.chomp(" (#{state})") unless state.nil?
name = name.chomp(" (#{city}, #{state})") unless city.nil? || state.nil?

# For Homeschool, the city is "Any" and the state is missing
city = nil if city == 'Any'

school = School.fuzzy_search name, city, state
end

existing_user.school = school
end

user_changed = existing_user.changed?
if user_changed
existing_user.save
transfer_errors_from(existing_user, {type: :verbatim}, :fail_if_errors)
end

if verification.errors.none? && verification.verified?
VerifyEducator.perform_later(verification_id: verification_id, user: existing_user)
elsif verification.rejected?
run(SheeridRejectedEducator, user: existing_user, verification_id: verification_id)
elsif verification.present?
existing_user.sheerid_verification_id = verification_id \
if existing_user.sheerid_verification_id.blank?

if verification_details.relevant?
existing_user.first_name = verification.first_name
existing_user.last_name = verification.last_name
existing_user.sheerid_reported_school = verification.organization_name
existing_user.faculty_status = verification.current_step_to_faculty_status

# Attempt to exactly match a school based on the sheerid_reported_school field
school = School.find_by sheerid_school_name: existing_user.sheerid_reported_school

if school.nil?
# No exact match found, so attempt to fuzzy match the school name
match = SheeridAPI::SHEERID_REGEX.match existing_user.sheerid_reported_school
name = match[1]
city = match[2]
state = match[3]

# Sometimes the city and/or state are duplicated, so remove them
name = name.chomp(" (#{city})") unless city.nil?
name = name.chomp(" (#{state})") unless state.nil?
name = name.chomp(" (#{city}, #{state})") unless city.nil? || state.nil?

# For Homeschool, the city is "Any" and the state is missing
city = nil if city == 'Any'

school = School.fuzzy_search name, city, state
end

existing_user.school = school
end

if existing_user.changed?
existing_user.save
transfer_errors_from(existing_user, {type: :verbatim}, :fail_if_errors)
SecurityLog.create!(
event_type: :user_updated_using_sheerid_data,
user: existing_user,
event_data: { verification: verification.inspect }
)
end
SecurityLog.create!(
event_type: :user_updated_using_sheerid_data,
user: existing_user,
event_data: { verification: verification.inspect }
) if user_changed
end

outputs.verification = verification
Expand Down
4 changes: 3 additions & 1 deletion spec/factories/sheerid_verification.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
FactoryBot.define do
factory :sheerid_verification do
current_step { 'success' }
current_step do
[ SheeridVerification::VERIFIED, SheeridVerification::REJECTED, 'pending' ].sample
end
email { Faker::Internet.free_email }
verification_id { Faker::Internet.uuid }
first_name { Faker::Name.first_name }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
end
let(:verification_details) do
SheeridAPI::Response.new(
'lastResponse' => { 'currentStep' => verification.current_step },
'personInfo' => {
'firstName' => user.first_name,
'lastName' => user.last_name,
Expand All @@ -20,9 +21,10 @@
end

before do
num_calls = verification.verified? ? :twice : :once
expect(SheeridAPI).to receive(:get_verification_details).with(
verification.verification_id
).and_return(verification_details)
).exactly(num_calls).and_return(verification_details)

expect(School).to receive(:find_by).with(
sheerid_school_name: school.sheerid_school_name
Expand Down

0 comments on commit 8a77301

Please sign in to comment.