From 8a773011ccd33f41248b3864abf594156a65f331 Mon Sep 17 00:00:00 2001 From: Dante Soares Date: Fri, 2 Apr 2021 08:27:43 -0500 Subject: [PATCH] SheerID school fixes (#949) --- .../process_sheerid_webhook_request.rb | 89 ++++++++++--------- spec/factories/sheerid_verification.rb | 4 +- .../process_sheerid_webhook_request_spec.rb | 4 +- 3 files changed, 53 insertions(+), 44 deletions(-) diff --git a/app/routines/newflow/educator_signup/process_sheerid_webhook_request.rb b/app/routines/newflow/educator_signup/process_sheerid_webhook_request.rb index a70a893ad..6e1963c68 100644 --- a/app/routines/newflow/educator_signup/process_sheerid_webhook_request.rb +++ b/app/routines/newflow/educator_signup/process_sheerid_webhook_request.rb @@ -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 diff --git a/spec/factories/sheerid_verification.rb b/spec/factories/sheerid_verification.rb index d5721f17a..27c046d37 100644 --- a/spec/factories/sheerid_verification.rb +++ b/spec/factories/sheerid_verification.rb @@ -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 } diff --git a/spec/routines/newflow/educator_signup/process_sheerid_webhook_request_spec.rb b/spec/routines/newflow/educator_signup/process_sheerid_webhook_request_spec.rb index 1a9beaabe..e622c2e66 100644 --- a/spec/routines/newflow/educator_signup/process_sheerid_webhook_request_spec.rb +++ b/spec/routines/newflow/educator_signup/process_sheerid_webhook_request_spec.rb @@ -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, @@ -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