Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update user school info from submitted teacher application #19977

Merged
merged 2 commits into from
Jan 13, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def on_successful_create
@application.auto_score!
workshop = @application.find_default_workshop
@application.pd_workshop_id = workshop.id if workshop
@application.update_user_school_info!

::Pd::Application::Teacher1819ApplicationMailer.confirmation(@application).deliver_now
::Pd::Application::Teacher1819ApplicationMailer.principal_approval(@application).deliver_now
Expand Down
39 changes: 39 additions & 0 deletions dashboard/app/models/pd/application/teacher1819_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Teacher1819Application < ApplicationBase
include Teacher1819ApplicationConstants
include RegionalPartnerTeacherconMapping
include SerializedProperties
include SchoolInfoDeduplicator

serialized_attrs %w(
pd_workshop_id
Expand Down Expand Up @@ -88,6 +89,17 @@ def send_decision_notification_email
update!(decision_notification_email_sent_at: Time.zone.now)
end

# Updates the associated user's school info with the info from this teacher application
# based on these rules in order:
# 1. Application has a specific school? always overwrite the user's school info
# 2. User doesn't have a specific school? overwrite with the custom school info.
def update_user_school_info!
if school_id || user.school_info.try(&:school).nil?
school_info = get_duplicate_school_info(school_info_attr) || SchoolInfo.create!(school_info_attr)
user.update_column(:school_info_id, school_info.id)
end
end

def set_type_and_year
self.application_year = YEAR_18_19
self.application_type = TEACHER_APPLICATION
Expand Down Expand Up @@ -802,6 +814,33 @@ def sanitize_form_data_hash
super.merge(account_email: user.email)
end

def school_id
raw_school_id = sanitize_form_data_hash[:school]

# -1 designates custom school info, in which case return nil
raw_school_id.to_i == -1 ? nil : raw_school_id
end

def school_info_attr
if school_id
{
school_id: school_id
}
else
hash = sanitize_form_data_hash
{
country: 'US',
# Take the first word in school type, downcased. E.g. "Public school" -> "public"
school_type: hash[:school_type].split(' ').first.downcase,
state: hash[:school_state],
zip: hash[:school_zip_code],
school_name: hash[:school_name],
full_address: hash[:school_address],
validation_type: SchoolInfo::VALIDATION_NONE
}
end
end

protected

def yes_no_response_to_yes_no_score(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,12 @@ class TeacherApplicationsControllerTest < ::ActionController::TestCase
end
assert_response :success
end

test 'updates user school info on successful create' do
Pd::Application::Teacher1819Application.any_instance.expects(:update_user_school_info!)

sign_in @applicant
put :create, params: @test_params
end
end
end
25 changes: 24 additions & 1 deletion dashboard/test/factories/pd_factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,6 @@
first_name: 'Severus',
preferred_first_name: 'Sevvy',
last_name: 'Snape',
account_meail: 'severus@hogwarts.edu',
alternate_email: 'ilovepotions@gmail.com',
phone: '5558675309',
address: '123 Fake Street',
Expand Down Expand Up @@ -690,6 +689,30 @@
end
end.stringify_keys
end

trait :with_custom_school do
transient do
school_name 'Code.org'
school_address '1501 4th Ave'
school_city 'Seattle'
school_state 'Washington'
school_zip_code '98101'
school_type 'Public school'
end
after(:build) do |hash, evaluator|
hash.merge!(
{
school: -1,
school_name: evaluator.school_name,
school_address: evaluator.school_address,
school_city: evaluator.school_city,
school_state: evaluator.school_state,
school_zip_code: evaluator.school_zip_code,
school_type: evaluator.school_type
}.stringify_keys
)
end
end
end

factory :pd_teacher1819_application, class: 'Pd::Application::Teacher1819Application' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,5 +508,71 @@ class Teacher1819ApplicationTest < ActiveSupport::TestCase

assert_not_equal first_enrollment.id, application.auto_assigned_enrollment_id
end

test 'school_info_attr for specific school' do
school = create :school
form_data_hash = build :pd_teacher1819_application_hash, school: school
application = create :pd_teacher1819_application, form_data_hash: form_data_hash
assert_equal({school_id: school.id}, application.school_info_attr)
end

test 'school_info_attr for custom school' do
application = create :pd_teacher1819_application, form_data_hash: (
build :pd_teacher1819_application_hash,
:with_custom_school,
school_name: 'Code.org',
school_address: '1501 4th Ave',
school_city: 'Seattle',
school_state: 'Washington',
school_zip_code: '98101',
school_type: 'Public school'
)
assert_equal(
{
country: 'US',
school_type: 'public',
state: 'Washington',
zip: '98101',
school_name: 'Code.org',
full_address: '1501 4th Ave',
validation_type: SchoolInfo::VALIDATION_NONE
},
application.school_info_attr
)
end

test 'update_user_school_info with specific school overwrites user school info' do
user = create :teacher, school_info: create(:school_info)
application_school_info = create :school_info
application = create :pd_teacher1819_application, user: user, form_data_hash: (
build :pd_teacher1819_application_hash, school: application_school_info.school
)

application.update_user_school_info!
assert_equal application_school_info, user.school_info
end

test 'update_user_school_info with custom school does nothing when the user already a specific school' do
original_school_info = create :school_info
user = create :teacher, school_info: original_school_info
application = create :pd_teacher1819_application, user: user, form_data_hash: (
build :pd_teacher1819_application_hash, :with_custom_school
)

application.update_user_school_info!
assert_equal original_school_info, user.school_info
end

test 'update_user_school_info with custom school updates user info when user does not have a specific school' do
original_school_info = create :school_info_us_other
user = create :teacher, school_info: original_school_info
application = create :pd_teacher1819_application, user: user, form_data_hash: (
build :pd_teacher1819_application_hash, :with_custom_school
)

application.update_user_school_info!
refute_equal original_school_info.id, user.school_info_id
assert_not_nil user.school_info_id
end
end
end