Skip to content

Commit

Permalink
Merge pull request #4577 from DFE-Digital/CST-2275-send-materials-link
Browse files Browse the repository at this point in the history
CST-2275 Send materials email to mentor
  • Loading branch information
edujackedu authored Feb 26, 2024
2 parents 425ad5b + 9b8fade commit 3f23f08
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 3 deletions.
28 changes: 28 additions & 0 deletions app/mailers/mentor_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

class MentorMailer < ApplicationMailer
MATERIALS_TEMPLATE = "d5409fd3-bad6-4e98-97d7-69363d2921c8"

def training_materials
mentor_profile = params[:mentor_profile]
mentor_email = mentor_profile.user.email
mentor_name = mentor_profile.user.full_name
ect_name = params[:ect_name]
cip_materials_name = params[:cip_materials_name]
sit_name = params[:sit_name]

template_mail(
MATERIALS_TEMPLATE,
to: mentor_email,
rails_mailer: mailer_name,
rails_mail_template: action_name,
personalisation: {
subject: "Access training materials for your new early career teacher",
mentor_name:,
ect_name:,
cip_materials_name:,
sit_name:,
},
).tag(:send_cip_materials_to_mentor).associate_with(mentor_profile, as: :mentor_profile)
end
end
36 changes: 33 additions & 3 deletions app/services/induction/change_mentor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
class Induction::ChangeMentor < BaseService
def call
ActiveRecord::Base.transaction do
Induction::ChangeInductionRecord.call(induction_record:,
changes: { mentor_profile: })

Induction::ChangeInductionRecord.call(
induction_record:,
changes: { mentor_profile: },
)
induction_record.participant_profile.update!(mentor_profile:)

send_training_materials_if_needed

mentor_profile&.user&.touch
end
end
Expand All @@ -19,4 +23,30 @@ def initialize(induction_record:, mentor_profile: nil)
@induction_record = induction_record
@mentor_profile = mentor_profile
end

def send_training_materials
MentorMailer.with(
mentor_profile:,
ect_name: induction_record.participant_profile.user.full_name,
cip_materials_name: cip_materials.name,
sit_name:,
).training_materials
.deliver_later
end

def cip_materials
induction_record.induction_programme.core_induction_programme
end

def sit_name
induction_record.school.induction_tutor&.full_name
end

def send_training_materials_if_needed
return unless sit_name

if induction_record.enrolled_in_cip? && cip_materials.present?
send_training_materials
end
end
end
29 changes: 29 additions & 0 deletions spec/mailers/mentor_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe MentorMailer, type: :mailer do
describe "#programme_changed_email" do
let(:school_cohort) { create :school_cohort }
let!(:induction_coordinator) { create(:user, :induction_coordinator, schools: [school_cohort.school]) }
let(:ect_profile) { create(:ect_participant_profile, school_cohort:) }
let(:mentor_profile) { create(:mentor_participant_profile, school_cohort:) }
let(:induction_programme) { create(:induction_programme, :fip, school_cohort:) }
let!(:induction_record) { Induction::Enrol.call(induction_programme:, participant_profile: ect_profile, start_date: 6.months.ago, mentor_profile:) }

let(:training_materials_email) do
MentorMailer.with(
mentor_profile:,
school_name: induction_record.school.name,
ect_name: induction_record.participant_profile.user.full_name,
cip_materials_name: "Core Induction Programme",
sit_name: induction_record.school.induction_tutor.full_name,
).training_materials.deliver_now
end

it "renders the right headers" do
expect(training_materials_email.to).to eq([mentor_profile.user.email])
expect(training_materials_email.from).to eq(["[email protected]"])
end
end
end
38 changes: 38 additions & 0 deletions spec/services/induction/change_mentor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,43 @@
service.call(induction_record:, mentor_profile: mentor_profile_2)
}.to change(mentor_profile_2.user, :updated_at)
end

it "does not send material email" do
allow(MentorMailer).to receive(:with).and_call_original

service.call(induction_record:, mentor_profile: mentor_profile_2)

expect(MentorMailer).not_to have_received(:with)
end

context "with CIP induction programme" do
let!(:sit) { create(:induction_coordinator_profile, schools: [induction_record.school]).user }
context "with SIT" do
let(:induction_programme) { create(:induction_programme, :cip, school_cohort:) }

it "sends the email" do
allow(MentorMailer).to receive(:with).and_call_original

service.call(induction_record:, mentor_profile: mentor_profile_2)

expect(MentorMailer).to have_received(:with).with({
ect_name: ect_profile.user.full_name,
sit_name: sit.full_name,
mentor_profile: mentor_profile_2,
cip_materials_name: induction_record.induction_programme.core_induction_programme.name,
})
end
end

context "without SIT" do
it "does not send material email" do
allow(MentorMailer).to receive(:with).and_call_original

service.call(induction_record:, mentor_profile: mentor_profile_2)

expect(MentorMailer).not_to have_received(:with)
end
end
end
end
end

0 comments on commit 3f23f08

Please sign in to comment.