From ad5d73d7fc07c04964d29fbe4ff4f3a83353d653 Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Tue, 26 Sep 2023 17:08:27 +0100 Subject: [PATCH] Add RequestRequestable This adds a service, which works in a similar way to ExpireRequestable and ReceiveRequestable to handle the transition from created to requested. --- .../create_further_information_request.rb | 22 +++------- app/services/request_requestable.rb | 44 +++++++++++++++++++ app/services/submit_application_form.rb | 19 ++------ app/services/verify_assessment.rb | 23 +++------- spec/services/request_requestable_spec.rb | 43 ++++++++++++++++++ 5 files changed, 103 insertions(+), 48 deletions(-) create mode 100644 app/services/request_requestable.rb create mode 100644 spec/services/request_requestable_spec.rb diff --git a/app/services/create_further_information_request.rb b/app/services/create_further_information_request.rb index ef14f3fdff..a81e1a17c5 100644 --- a/app/services/create_further_information_request.rb +++ b/app/services/create_further_information_request.rb @@ -11,21 +11,20 @@ def initialize(assessment:, user:) def call further_information_request = ActiveRecord::Base.transaction do - request = - assessment.further_information_requests.create!( + requestable = + FurtherInformationRequest.create!( + assessment:, items: FurtherInformationRequestItemsFactory.call( assessment_sections: assessment.sections, ), - requested_at: Time.zone.now, ) - ApplicationFormStatusUpdater.call(application_form:, user:) + RequestRequestable.call(requestable:, user:) - create_timeline_event(request) - request.after_requested(user:) + ApplicationFormStatusUpdater.call(application_form:, user:) - request + requestable end TeacherMailer.with(teacher:).further_information_requested.deliver_later @@ -44,13 +43,4 @@ def application_form def teacher @teacher ||= application_form.teacher end - - def create_timeline_event(further_information_request) - TimelineEvent.create!( - application_form:, - creator: user, - event_type: "requestable_requested", - requestable: further_information_request, - ) - end end diff --git a/app/services/request_requestable.rb b/app/services/request_requestable.rb new file mode 100644 index 0000000000..8a3102b205 --- /dev/null +++ b/app/services/request_requestable.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +class RequestRequestable + include ServicePattern + + def initialize(requestable:, user:) + @requestable = requestable + @user = user + end + + def call + raise AlreadyRequested if requestable.requested? + + ActiveRecord::Base.transaction do + requestable.requested! + create_timeline_event + ApplicationFormStatusUpdater.call(application_form:, user:) + end + + requestable.after_requested(user:) + end + + class AlreadyRequested < StandardError + end + + private + + attr_reader :requestable, :user + + delegate :application_form, to: :requestable + + def create_timeline_event + creator = user.is_a?(String) ? nil : user + creator_name = user.is_a?(String) ? user : "" + + TimelineEvent.create!( + application_form:, + creator:, + creator_name:, + event_type: "requestable_requested", + requestable:, + ) + end +end diff --git a/app/services/submit_application_form.rb b/app/services/submit_application_form.rb index 38f598502f..5b6de925f1 100644 --- a/app/services/submit_application_form.rb +++ b/app/services/submit_application_form.rb @@ -18,6 +18,8 @@ def call region.requires_preliminary_check application_form.submitted_at = Time.zone.now + ApplicationFormStatusUpdater.call(application_form:, user:) + assessment = AssessmentFactory.call(application_form:) create_professional_standing_request(assessment) @@ -25,8 +27,6 @@ def call if application_form.reduced_evidence_accepted UpdateAssessmentInductionRequired.call(assessment:) end - - ApplicationFormStatusUpdater.call(application_form:, user:) end TeacherMailer @@ -68,19 +68,8 @@ def call def create_professional_standing_request(assessment) return unless application_form.teaching_authority_provides_written_statement - requestable = - ProfessionalStandingRequest.create!( - assessment:, - requested_at: Time.zone.now, - ) - - TimelineEvent.create!( - event_type: "requestable_requested", - application_form:, - creator: user, - requestable:, - ) + requestable = ProfessionalStandingRequest.create!(assessment:) - requestable.after_requested(user:) + RequestRequestable.call(requestable:, user:) end end diff --git a/app/services/verify_assessment.rb b/app/services/verify_assessment.rb index 9b6be33cb7..1b54c15c9d 100644 --- a/app/services/verify_assessment.rb +++ b/app/services/verify_assessment.rb @@ -57,37 +57,26 @@ def create_professional_standing_request return if application_form.teaching_authority_provides_written_statement ProfessionalStandingRequest - .create!(assessment:, requested_at: Time.zone.now) - .tap { |requestable| create_timeline_event(requestable) } + .create!(assessment:) + .tap { |requestable| RequestRequestable.call(requestable:, user:) } end def create_qualification_requests qualifications.map do |qualification| QualificationRequest - .create!(assessment:, qualification:, requested_at: Time.zone.now) - .tap { |requestable| create_timeline_event(requestable) } + .create!(assessment:, qualification:) + .tap { |requestable| RequestRequestable.call(requestable:, user:) } end end def create_reference_requests work_histories.map do |work_history| ReferenceRequest - .create!(assessment:, work_history:, requested_at: Time.zone.now) - .tap { |requestable| create_timeline_event(requestable) } + .create!(assessment:, work_history:) + .tap { |requestable| RequestRequestable.call(requestable:, user:) } end end - def create_timeline_event(requestable) - TimelineEvent.create!( - application_form:, - creator: user, - event_type: "requestable_requested", - requestable:, - ) - - requestable.after_requested(user:) - end - def send_reference_request_emails(reference_requests) reference_requests.each do |reference_request| RefereeMailer.with(reference_request:).reference_requested.deliver_later diff --git a/spec/services/request_requestable_spec.rb b/spec/services/request_requestable_spec.rb new file mode 100644 index 0000000000..90afdcf0b6 --- /dev/null +++ b/spec/services/request_requestable_spec.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe RequestRequestable do + let(:application_form) { create(:application_form, :submitted) } + let(:requestable) do + create( + :qualification_request, + assessment: create(:assessment, application_form:), + ) + end + let(:user) { "John Smith" } + + subject(:call) { described_class.call(requestable:, user:) } + + context "with an already requested requestable" do + before { requestable.requested! } + + it "raises an error" do + expect { call }.to raise_error(RequestRequestable::AlreadyRequested) + end + end + + it "changes the requestable state to requested" do + expect { call }.to change(requestable, :requested?).from(false).to(true) + end + + it "changes the requestable requested at" do + freeze_time do + expect { call }.to change(requestable, :requested_at).from(nil).to( + Time.current, + ) + end + end + + it "records a requestable requested timeline event" do + expect { call }.to have_recorded_timeline_event( + :requestable_requested, + requestable:, + ) + end +end