diff --git a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb index e30bbd9bb..78a8ae357 100644 --- a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb +++ b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb @@ -659,8 +659,9 @@ module JamRuby end end + # teacher def teacher_lesson_request(lesson_booking) - email = lesson_booking.teacher.email + email = lesson_booking.school_over_teacher subject = "You have received a lesson request through JamKazam!" unique_args = {:type => "teacher_lesson_request"} @@ -670,7 +671,7 @@ module JamRuby sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [lesson_booking.teacher.id]) mail(:to => email, :subject => subject) do |format| @@ -721,13 +722,13 @@ module JamRuby @lesson_session = lesson_session @message = message - email = lesson_session.teacher.email + email = lesson_session.school_and_teacher unique_args = {:type => "teacher_lesson_accepted"} sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [lesson_session.teacher.id]) mail(:to => email, :subject => @subject) do |format| @@ -778,13 +779,13 @@ module JamRuby @lesson_session = lesson_session @message = message - email = lesson_session.teacher.email + email = lesson_session.school_and_teacher unique_args = {:type => "teacher_lesson_update_all"} sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [lesson_session.teacher.id]) mail(:to => email, :subject => subject) do |format| @@ -870,7 +871,7 @@ module JamRuby # student proposed counter time; so send msg to the teacher def teacher_lesson_counter(lesson_session, slot) - email = lesson_session.teacher.email + email = lesson_session.school_over_teacher subject = "Student has proposed a different time for their lesson" unique_args = {:type => "teacher_lesson_counter"} @student = lesson_session.student @@ -883,7 +884,7 @@ module JamRuby sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [@teacher.id]) mail(:to => email, :subject => subject) do |format| @@ -900,7 +901,7 @@ module JamRuby @session_date = lesson_session.slot.pretty_scheduled_start(true) @session_url = lesson_session.web_url @lesson_session = lesson_session - email = lesson_session.teacher.email + email = lesson_session.school_and_teacher if @lesson_session.student_missed subject = "You will be paid for your lesson with #{@student.name}" else @@ -913,7 +914,7 @@ module JamRuby sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [@teacher.id]) mail(:to => email, :subject => subject) do |format| @@ -958,14 +959,14 @@ module JamRuby @session_url = lesson_session.web_url @lesson_session = lesson_session - email = @teacher.email + email = lesson_session.school_and_teacher subject = "Your TestDrive with #{@student.name} was not successful" unique_args = {:type => "teacher_test_drive_no_bill"} sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [@teacher.id]) mail(:to => email, :subject => subject) do |format| @@ -1059,14 +1060,14 @@ module JamRuby @session_date = lesson_session.slot.pretty_scheduled_start(true) @session_url = lesson_session.web_url @lesson_session = lesson_session - email = lesson_session.teacher.email + email = lesson_session.school_and_teacher subject = "Your student #{@student.name} will not be charged for their lesson" unique_args = {:type => "teacher_lesson_normal_no_bill"} sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [@teacher.id]) mail(:to => email, :subject => subject) do |format| @@ -1108,14 +1109,14 @@ module JamRuby @session_date = lesson_session.slot.pretty_scheduled_start(true) @session_url = lesson_session.web_url @lesson_session = lesson_session - email = lesson_session.teacher.email + email = lesson_session.school_over_teacher subject = "Your JamClass lesson today with #{@student.first_name}" unique_args = {:type => "teacher_lesson_normal_done"} sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [@teacher.id]) mail(:to => email, :subject => subject) do |format| @@ -1219,7 +1220,7 @@ module JamRuby @lesson_booking = lesson_booking @month_name = lesson_package_purchase.month_name - email = @teacher.email + email = lesson_booking.school_over_teacher if lesson_booking.is_suspended? @subject = "Your weekly lessons with #{@student.name} has been suspended." else @@ -1232,7 +1233,7 @@ module JamRuby sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [@teacher.id]) mail(:to => email, :subject => @subject) do |format| @@ -1279,7 +1280,7 @@ module JamRuby @lesson_booking = lesson_booking @month_name = lesson_package_purchase.month_name - email = @teacher.email + email = lesson_booking.school_over_teacher if lesson_booking.is_suspended? @subject = "Your weekly lessons with #{@student.name} has been suspended." else @@ -1292,7 +1293,7 @@ module JamRuby sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [@teacher.id]) mail(:to => email, :subject => @subject) do |format| @@ -1310,7 +1311,7 @@ module JamRuby @payable_teacher = teacher_payment.payable_teacher @name = @teacher.first_name || 'Anonymous' @student = @distribution.student - email = @teacher.email + email = @distribution.target.lesson_booking.school_over_teacher if @school if @distribution.is_test_drive? @@ -1339,7 +1340,7 @@ module JamRuby sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [@teacher.id]) mail(:to => email, :subject => @subject) do |format| @@ -1357,7 +1358,7 @@ module JamRuby @payable_teacher = teacher_payment.payable_teacher @student = @distribution.student @name = @payable_teacher.first_name || 'Anonymous' - email = @payable_teacher.email + email = @distribution.target.lesson_booking.school_over_teacher @card_declined = teacher_payment.is_card_declined? @card_expired = teacher_payment.is_card_expired? @@ -1391,7 +1392,7 @@ module JamRuby sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [@payable_teacher.id]) mail(:to => email, :subject => @subject) do |format| @@ -1447,7 +1448,7 @@ module JamRuby email = @student.email subject = "Your JamClass lesson today with #{@teacher.first_name}" - unique_args = {:type => "student_lesson_normal_no_bill"} + unique_args = {:type => "monthly_recurring_done"} sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] @@ -1544,13 +1545,13 @@ module JamRuby @session_description = @lesson_session.music_session.description @session_date = @lesson_session.slot.pretty_scheduled_start(true) - email = @teacher.email + email = @lesson_booking.school_and_teacher @subject = "Your lesson has been canceled" unique_args = {:type => "teacher_lesson_booking_canceled"} sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [@teacher.id]) mail(:to => email, :subject => @subject) do |format| @@ -1593,13 +1594,13 @@ module JamRuby @session_description = @lesson_session.music_session.description @session_date = @lesson_session.slot.pretty_scheduled_start(true) - email = @teacher.email + email = @lesson_booking.school_and_teacher @subject = "Your lesson has been canceled" unique_args = {:type => "teacher_lesson_canceled"} sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [@teacher.id]) mail(:to => email, :subject => @subject) do |format| @@ -1704,14 +1705,14 @@ module JamRuby @session_date = lesson_session.slot.pretty_scheduled_start(true) @session_url = lesson_session.web_url @lesson_session = lesson_session - email = lesson_session.teacher.email + email = lesson_session.school_over_teacher @subject = "Student #{@student.name}'s time proposal is still awaiting your response" unique_args = {:type => "teacher_counter_reminder"} sendgrid_category "Notification" sendgrid_unique_args :type => unique_args[:type] - sendgrid_recipients([email]) + sendgrid_recipients(email) sendgrid_substitute('@USERID', [@teacher.id]) mail(:to => email, :subject => @subject) do |format| diff --git a/ruby/lib/jam_ruby/models/lesson_booking.rb b/ruby/lib/jam_ruby/models/lesson_booking.rb index 55cc26f99..d170e140b 100644 --- a/ruby/lib/jam_ruby/models/lesson_booking.rb +++ b/ruby/lib/jam_ruby/models/lesson_booking.rb @@ -456,6 +456,7 @@ module JamRuby end result end + def lesson_package_type if is_single_free? LessonPackageType.single_free @@ -767,6 +768,22 @@ module JamRuby same_school end + def school_and_teacher + if school && school.scheduling_comm? + [school.correspondence_email, teacher.email] + else + [teacher.email] + end + end + + def school_over_teacher + if school && school.scheduling_comm? + [school.correspondence_email] + else + [teacher.email] + end + end + def self.find_bookings_needing_sessions(minimum_start_time) MusicSession.select([:lesson_booking_id]).joins(:lesson_session => :lesson_booking).where("lesson_bookings.active = true").where('lesson_bookings.recurring = true').where("scheduled_start is not null").where("scheduled_start > ?", minimum_start_time).group(:lesson_booking_id).having('count(lesson_booking_id) < 2') end @@ -921,6 +938,21 @@ module JamRuby end end + + def scheduling_email + school_scheduling_comm? ? school.communication_email : teacher.email + end + + # when you need to email potentially both school and teacher for same email + def teacher_school_emails + if school_comm? + [school.communication_email, teacher.email] + else + [teacher.email] + end + + end + def home_url APP_CONFIG.external_root_url + "/client#/jamclass" end @@ -936,5 +968,10 @@ module JamRuby def admin_url APP_CONFIG.admin_root_url + "/admin/lesson_bookings/" + id end + + private + def school_scheduling_comm? + school ? school.school_comm? : false + end end end diff --git a/ruby/lib/jam_ruby/models/lesson_session.rb b/ruby/lib/jam_ruby/models/lesson_session.rb index 99ca697c7..0b0b69acf 100644 --- a/ruby/lib/jam_ruby/models/lesson_session.rb +++ b/ruby/lib/jam_ruby/models/lesson_session.rb @@ -11,7 +11,7 @@ module JamRuby @@log = Logging.logger[LessonSession] delegate :sent_billing_notices, :last_billing_attempt_at, :billing_attempts, :billing_should_retry, :billed_at, :billing_error_detail, :billing_error_reason, :is_card_declined?, :is_card_expired?, :last_billed_at_date, :sent_billing_notices, to: :lesson_payment_charge, allow_nil: true - delegate :is_test_drive?, :is_single_free?, :is_normal?, :approved_before?, :is_active?, :recurring, :is_monthly_payment?, :school_on_school?, to: :lesson_booking + delegate :is_test_drive?, :is_single_free?, :is_normal?, :approved_before?, :is_active?, :recurring, :is_monthly_payment?, :school_on_school?, :scheduling_email, :teacher_school_emails, :school_and_teacher, :school_over_teacher, to: :lesson_booking delegate :pretty_scheduled_start, to: :music_session diff --git a/ruby/lib/jam_ruby/models/school.rb b/ruby/lib/jam_ruby/models/school.rb index af10595c8..e5e79373d 100644 --- a/ruby/lib/jam_ruby/models/school.rb +++ b/ruby/lib/jam_ruby/models/school.rb @@ -31,6 +31,14 @@ module JamRuby after_create :create_affiliate before_save :stringify_avatar_info, :if => :updating_avatar + def scheduling_comm? + scheduling_communication == SCHEDULING_COMM_SCHOOL + end + + def communication_email + correspondence_email.blank? ? owner.email : correspondence_email + end + def create_affiliate AffiliatePartner.create_from_school(self) end diff --git a/ruby/lib/jam_ruby/models/teacher_distribution.rb b/ruby/lib/jam_ruby/models/teacher_distribution.rb index 79dd8159c..0b82910b7 100644 --- a/ruby/lib/jam_ruby/models/teacher_distribution.rb +++ b/ruby/lib/jam_ruby/models/teacher_distribution.rb @@ -64,6 +64,14 @@ module JamRuby distribution end + def target + if lesson_session + lesson_session + else + lesson_package_purchase + end + end + def amount amount_in_cents / 100.0 end diff --git a/ruby/spec/mailers/teacher_lesson_email_spec.rb b/ruby/spec/mailers/teacher_lesson_email_spec.rb new file mode 100644 index 000000000..1a49bc635 --- /dev/null +++ b/ruby/spec/mailers/teacher_lesson_email_spec.rb @@ -0,0 +1,168 @@ +# verifies that teacher directed emails go to the right location +require "spec_helper" + + +# https://jamkazam.atlassian.net/browse/VRFS-4128#add-comment +# * *teacher-always* : the teacher always gets this email +# * *school-over-teacher*: if there is a school, then the school gets the email if it has the scheduling communication pref set to true; otherwise, just teacher +# * *school-and-teacher*: if there is a school, send to both school and teacher; otherwise, just teacher + + +describe "TeacherLessonEmails" do + + def school_over_teacher + if @noSchool + mail.to.should eql [teacher.email] + elsif @schoolDirectComm + mail.to.should eql [teacher.email] + else + mail.to.should eql [school.communication_email] + end + end + + def teacher_always + mail.to.should eql [teacher.email] + end + + def school_and_teacher + if @noSchool + mail.to.should eql [teacher.email] + elsif @schoolDirectComm + mail.to.should eql [teacher.email] + else + mail.to.should eql [teacher.email, school.correspondence_email] + end + end + + before(:each) { + UserMailer.deliveries.clear + @noSchool = @schooDirectComm = @schoolSchedulingComm = false + } + # lessons with no school affiliation + describe "no-school" do + + let(:user) { FactoryGirl.create(:user) } + let(:teacher) { FactoryGirl.create(:teacher_user) } + let(:lesson) { normal_lesson(user, teacher) } + let(:mail) { UserMailer.deliveries[-1] } + + before(:each) { + @noSchool = true + } + + it "teacher_lesson_request" do + UserMailer.teacher_lesson_request(lesson.lesson_booking) + school_over_teacher + end + + it "teacher_lesson_accepted" do + UserMailer.teacher_lesson_accepted(lesson, "come along now and teach", lesson.lesson_booking.lesson_booking_slots[0]) + school_and_teacher + end + + it "teacher_lesson_update_all" do + UserMailer.teacher_lesson_update_all(lesson, "come along now and teach", lesson.lesson_booking.lesson_booking_slots[0]) + school_and_teacher + end + + it "teacher_lesson_counter" do + UserMailer.teacher_lesson_counter(lesson, lesson.lesson_booking.lesson_booking_slots[0]) + school_over_teacher + end + + it "teacher_lesson_normal_done" do + UserMailer.teacher_lesson_normal_done(lesson) + school_over_teacher + end + + it "teacher_lesson_completed" do + UserMailer.teacher_lesson_completed(lesson) + school_and_teacher + end + + it "teacher_test_drive_no_bill" do + UserMailer.teacher_test_drive_no_bill(lesson) + school_and_teacher + end + + it "teacher_lesson_normal_no_bill" do + UserMailer.teacher_lesson_normal_no_bill(lesson) + school_and_teacher + end + + it "teacher_unable_charge_monthly" do + paid = monthly_lesson(user, teacher, {accept:true, finish: true}) + paid.reload + UserMailer.teacher_unable_charge_monthly(paid.lesson_package_purchase) + school_over_teacher + end + + it "teacher_lesson_monthly_charged" do + paid = monthly_lesson(user, teacher, {accept:true, finish: true}) + paid.reload + UserMailer.teacher_lesson_monthly_charged(paid.lesson_package_purchase) + school_over_teacher + end + + it "teacher_distribution_done" do + teacher_payment = FactoryGirl.create(:teacher_payment, teacher: teacher, teacher_distribution: FactoryGirl.create(:teacher_distribution, lesson_session: lesson)) + UserMailer.teacher_distribution_done(teacher_payment) + school_over_teacher + end + + it "teacher_distribution_fail" do + teacher_payment = FactoryGirl.create(:teacher_payment, teacher: teacher, teacher_distribution: FactoryGirl.create(:teacher_distribution, lesson_session: lesson)) + UserMailer.teacher_distribution_fail(teacher_payment) + school_over_teacher + end + + it "teacher_lesson_booking_canceled" do + canceled_lesson = normal_lesson(user, teacher, {cancel: true}) + UserMailer.teacher_lesson_booking_canceled(canceled_lesson.lesson_booking, "Sorry I can't teach anymore!") + school_and_teacher + end + + it "teacher_lesson_canceled" do + canceled_lesson = normal_lesson(user, teacher, {cancel: true}) + UserMailer.teacher_lesson_canceled(canceled_lesson, "Sorry I can't teach anymore!") + school_and_teacher + end + + it "lesson_chat" do + + end + + it "teacher_counter_reminder" do + UserMailer.teacher_counter_reminder(lesson) + school_over_teacher + end + + it "lesson_starting_soon_teacher" do + UserMailer.lesson_starting_soon_teacher(lesson) + teacher_always + end + end + + # lessons with school affiliation, but teacher gets all communication + describe "school-but-direct-comm" do + before(:each) { + @schoolDirectComm = true + } + let(:school) { FactoryGirl.create(:school, scheduling_communication: School::SCHEDULING_COMM_TEACHER) } + let(:user) { FactoryGirl.create(:user) } + let(:teacher_obj) { FactoryGirl.create(:teacher_obj, school) } + let(:teacher) { FactoryGirl.create(:teacher_user, teacher: teacher_obj) } + + end + + # lessons with school affiliation, and they want communication + describe "school-with-scheduling-comm" do + before(:each) { + @schoolSchedulingComm = true + } + let(:school) { FactoryGirl.create(:school, scheduling_communication: School::SCHEDULING_COMM_SCHOOL) } + let(:user) { FactoryGirl.create(:user) } + let(:teacher_obj) { FactoryGirl.create(:teacher_obj, school) } + let(:teacher) { FactoryGirl.create(:teacher_user, teacher: teacher_obj) } + end +end