Skip to content

Commit

Permalink
Updating user model to support parent email notifications
Browse files Browse the repository at this point in the history
We are adding a new feature to enable parents to get notification emails
about their child's progress. This changes make it so when a student
account changes, it will also update the parent's email preferences.
  • Loading branch information
daynew committed Mar 31, 2020
1 parent 96c02d0 commit eb77cc1
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
19 changes: 19 additions & 0 deletions dashboard/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ class User < ActiveRecord::Base

after_save :save_email_preference, if: -> {email_preference_opt_in.present?}

after_save :save_parent_email_preference, if: -> {parent_email_preference_opt_in.present?}

before_destroy :soft_delete_channels

def save_email_preference
Expand All @@ -223,6 +225,19 @@ def save_email_preference
end
end

# Enables/disables email notifications for the parent.
def save_parent_email_preference
if student? && parent_email.present?
EmailPreference.upsert!(
email: parent_email,
opt_in: parent_email_preference_opt_in.downcase == "yes",
ip_address: parent_email_preference_request_ip,
source: parent_email_preference_source,
form_kind: nil
)
end
end

# after_create :send_new_teacher_email
# def send_new_teacher_email
# TODO: it's not easy to pass cookies into an after_create call, so for now while this is behind a page mode
Expand Down Expand Up @@ -367,6 +382,10 @@ def self.find_or_create_facilitator(params, invited_by_user)
attr_accessor :email_preference_source
attr_accessor :email_preference_form_kind

attr_accessor :parent_email_preference_opt_in
attr_accessor :parent_email_preference_request_ip
attr_accessor :parent_email_preference_source

attr_accessor :data_transfer_agreement_required

has_many :plc_enrollments, class_name: '::Plc::UserCourseEnrollment', dependent: :destroy
Expand Down
77 changes: 77 additions & 0 deletions dashboard/test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class UserTest < ActiveSupport::TestCase
provider: AuthenticationOption::GOOGLE,
uid: '110879982140384463192',
}
@good_parent_email_params = {
parent_email: 'parent@email.com',
parent_email_preference_opt_in: 'yes',
parent_email_preference_request_ip: '127.0.0.1',
parent_email_preference_source: EmailPreference::ACCOUNT_SIGN_UP
}
@admin = create :admin
@user = create :user
@teacher = create :teacher
Expand Down Expand Up @@ -2199,6 +2205,77 @@ def email_preference_params(**args)
assert_equal '0', email_preference.form_kind
end

def assert_parent_email_params_equals_email_preference(parent_email_params, email_preference)
assert_equal parent_email_params[:parent_email], email_preference.email
assert_equal parent_email_params[:parent_email_preference_opt_in].casecmp?('yes'), email_preference.opt_in
assert_equal parent_email_params[:parent_email_preference_request_ip], email_preference.ip_address
assert_equal parent_email_params[:parent_email_preference_source], email_preference.source
end

test 'creating a student with parent email should create an email preference for the parent' do
parent_email_params = @good_parent_email_params
User.create(@good_data.merge(parent_email_params))
email_preference = EmailPreference.find_by_email(parent_email_params[:parent_email])
assert_not_nil email_preference
assert_parent_email_params_equals_email_preference parent_email_params, email_preference
end

test 'creating a student with parent email and opt-out should create an email preference for the parent' do
parent_email_params = @good_parent_email_params.merge({parent_email_preference_opt_in: 'no'})
User.create(@good_data.merge(parent_email_params))
email_preference = EmailPreference.find_by_email(parent_email_params[:parent_email])
assert_not_nil email_preference
assert_parent_email_params_equals_email_preference parent_email_params, email_preference
end

test 'updating a student with parent email should create an email preference for the parent' do
parent_email_params = @good_parent_email_params
user = User.create(@good_data)
email_preference = EmailPreference.find_by_email(parent_email_params[:parent_email])
# There should be no email preference because no parent_email was defined.
assert_nil email_preference
user.update!(parent_email_params)
email_preference = EmailPreference.find_by_email(parent_email_params[:parent_email])
# There should now be an email preference because the user was updated with a parent_email.
assert_not_nil email_preference
assert_parent_email_params_equals_email_preference parent_email_params, email_preference
end

test 'creating a student with parent email opt in and a nil email address should not create an email preference for the parent' do
parent_email_params = @good_parent_email_params.merge({parent_email: nil})
User.create(@good_data.merge(parent_email_params))
email_preference = EmailPreference.find_by_email(parent_email_params[:parent_email])
assert_nil email_preference
end

test 'creating a student with parent email opt in and an empty string email address should not create an email preference for the parent' do
parent_email_params = @good_parent_email_params.merge({parent_email: ''})
User.create(@good_data.merge(parent_email_params))
email_preference = EmailPreference.find_by_email(parent_email_params[:parent_email])
assert_nil email_preference
end

test 'creating a student with parent email opt in and a blank string email address should not create an email preference for the parent' do
parent_email_params = @good_parent_email_params.merge({parent_email: ' '})
User.create(@good_data.merge(parent_email_params))
email_preference = EmailPreference.find_by_email(parent_email_params[:parent_email])
assert_nil email_preference
end

test 'creating a student with parent email opt in and a nil source should not create an email preference for the parent' do
parent_email_params = @good_parent_email_params.merge({parent_email_preference_source: nil})
User.create(@good_data.merge(parent_email_params))
email_preference = EmailPreference.find_by_email(parent_email_params[:parent_email])
assert_nil email_preference
end

test 'creating a student with parent email opt in and a nil request_ip should not create an email preference for the parent' do
parent_email_params = @good_parent_email_params.merge({parent_email_preference_request_ip: nil})
User.create(@good_data.merge(parent_email_params))
email_preference = EmailPreference.find_by_email(parent_email_params[:parent_email])
assert_nil email_preference
end

test 'google_classroom_student? is true if user belongs to a google classroom section as a student' do
section = create(:section, login_type: Section::LOGIN_TYPE_GOOGLE_CLASSROOM)
user = create(:follower, section: section).student_user
Expand Down

0 comments on commit eb77cc1

Please sign in to comment.