diff --git a/app/controllers/assessor_interface/reference_requests_controller.rb b/app/controllers/assessor_interface/reference_requests_controller.rb index e870f1194f..0bccfe4611 100644 --- a/app/controllers/assessor_interface/reference_requests_controller.rb +++ b/app/controllers/assessor_interface/reference_requests_controller.rb @@ -111,13 +111,7 @@ def update_verify_failed def resend_email if reference_request.requested? && !reference_request.received? - DeliverEmail.call( - application_form:, - mailer: RefereeMailer, - action: :reference_requested, - reference_request:, - ) - + reference_request.send_requested_email flash[:info] = "The reference requested email has been resent." end diff --git a/app/models/reference_request.rb b/app/models/reference_request.rb index 27b72842a8..d2fcc51dd6 100644 --- a/app/models/reference_request.rb +++ b/app/models/reference_request.rb @@ -109,10 +109,23 @@ def send_reminder_email(_name, number_of_reminders_sent) ) end + def send_requested_email + DeliverEmail.call( + application_form:, + mailer: RefereeMailer, + action: :reference_requested, + reference_request: self, + ) + end + def expires_after 6.weeks end + def after_requested(*) + send_requested_email + end + def after_verified(*) UpdateAssessmentInductionRequired.call(assessment:) end diff --git a/app/services/request_requestable.rb b/app/services/request_requestable.rb index 06c3fcab32..ecf2f152de 100644 --- a/app/services/request_requestable.rb +++ b/app/services/request_requestable.rb @@ -3,17 +3,20 @@ class RequestRequestable include ServicePattern - def initialize(requestable:, user:) + def initialize(requestable:, user:, allow_already_requested: false) @requestable = requestable @user = user + @allow_already_requested = allow_already_requested end def call - raise AlreadyRequested if requestable.requested? + raise AlreadyRequested if !allow_already_requested && requestable.requested? ActiveRecord::Base.transaction do requestable.requested! + requestable.update!(expired_at: nil) if requestable.expired? + CreateTimelineEvent.call( "requestable_requested", application_form:, @@ -30,7 +33,7 @@ class AlreadyRequested < StandardError private - attr_reader :requestable, :user + attr_reader :requestable, :user, :allow_already_requested delegate :application_form, to: :requestable end diff --git a/app/services/update_work_history_contact.rb b/app/services/update_work_history_contact.rb index 0e414690c1..dfe46a48b0 100644 --- a/app/services/update_work_history_contact.rb +++ b/app/services/update_work_history_contact.rb @@ -12,30 +12,32 @@ def initialize(work_history:, user:, name: nil, job: nil, email: nil) end def call - ActiveRecord::Base.transaction do - change_value("contact_name", name) if name.present? + change_value("contact_name", name) if name.present? - change_value("contact_job", job) if job.present? + change_value("contact_job", job) if job.present? - if email.present? - change_value("contact_email", email) + if email.present? + email_address = EmailAddress.new(email) - email_address = EmailAddress.new(email) - work_history.update!( + change_value( + "contact_email", + email, + additional_updates: { canonical_contact_email: email_address.canonical, contact_email_domain: email_address.host_name, - ) - end + }, + ) end if email.present? && (reference_request = work_history.reference_request) - DeliverEmail.call( - application_form:, - mailer: RefereeMailer, - action: :reference_requested, - reference_request:, + RequestRequestable.call( + requestable: reference_request, + user:, + allow_already_requested: true, ) + ApplicationFormStatusUpdater.call(application_form:, user:) + DeliverEmail.call( application_form:, mailer: TeacherMailer, @@ -51,10 +53,13 @@ def call delegate :application_form, to: :work_history - def change_value(column_name, new_value) + def change_value(column_name, new_value, additional_updates: {}) old_value = work_history.send(column_name) - work_history.update!(column_name => new_value) - create_timeline_event(column_name, old_value, new_value) + + ActiveRecord::Base.transaction do + work_history.update!(column_name => new_value, **additional_updates) + create_timeline_event(column_name, old_value, new_value) + end end def create_timeline_event(column_name, old_value, new_value) diff --git a/app/services/verify_assessment.rb b/app/services/verify_assessment.rb index 5ff476d2ec..20caff6d93 100644 --- a/app/services/verify_assessment.rb +++ b/app/services/verify_assessment.rb @@ -39,7 +39,7 @@ def call end if reference_requests.present? - send_reference_request_emails(reference_requests) + send_references_requested_email(reference_requests) end reference_requests @@ -80,16 +80,7 @@ def create_reference_requests end end - def send_reference_request_emails(reference_requests) - reference_requests.each do |reference_request| - DeliverEmail.call( - application_form:, - mailer: RefereeMailer, - action: :reference_requested, - reference_request:, - ) - end - + def send_references_requested_email(reference_requests) DeliverEmail.call( application_form:, mailer: TeacherMailer, diff --git a/spec/support/shared_examples/requestable.rb b/spec/support/shared_examples/requestable.rb index a97938a0a7..8a2b5c229c 100644 --- a/spec/support/shared_examples/requestable.rb +++ b/spec/support/shared_examples/requestable.rb @@ -117,6 +117,8 @@ describe "#after_requested" do let(:after_requested) { subject.after_requested(user: "User") } + before { subject.requested! } + it "doesn't raise an error" do expect { after_requested }.to_not raise_error end