From 5d167c54a8a735abab5c2c820934d49d1f54f1a9 Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Fri, 5 Apr 2024 10:19:46 +0100 Subject: [PATCH] Add RequestConsent This adds a service which encapsulates the logic related to requesting consent so it can be used outside the controller. --- .../consent_requests_controller.rb | 21 +------- app/services/request_consent.rb | 49 +++++++++++++++++++ spec/services/request_consent_spec.rb | 35 +++++++++++++ 3 files changed, 85 insertions(+), 20 deletions(-) create mode 100644 app/services/request_consent.rb create mode 100644 spec/services/request_consent_spec.rb diff --git a/app/controllers/assessor_interface/consent_requests_controller.rb b/app/controllers/assessor_interface/consent_requests_controller.rb index a27c02d651..94cc94b597 100644 --- a/app/controllers/assessor_interface/consent_requests_controller.rb +++ b/app/controllers/assessor_interface/consent_requests_controller.rb @@ -38,26 +38,7 @@ def edit_request end def update_request - if consent_requests.present? - ActiveRecord::Base.transaction do - consent_requests.each do |requestable| - RequestRequestable.call(requestable:, user: current_staff) - end - - application_form.reload - - ApplicationFormStatusUpdater.call( - application_form:, - user: current_staff, - ) - end - - DeliverEmail.call( - application_form:, - mailer: TeacherMailer, - action: :consent_requested, - ) - end + RequestConsent.call(assessment:, user: current_staff) redirect_to [ :assessor_interface, diff --git a/app/services/request_consent.rb b/app/services/request_consent.rb new file mode 100644 index 0000000000..20138662b5 --- /dev/null +++ b/app/services/request_consent.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +class RequestConsent + include ServicePattern + + def initialize(assessment:, user:) + @assessment = assessment + @user = user + end + + def call + return unless consent_requests.exists? + + raise AlreadyRequested if consent_requests.requested.exists? + + create_and_request + send_email + end + + class AlreadyRequested < StandardError + end + + private + + attr_reader :assessment, :user + + delegate :application_form, to: :assessment + delegate :consent_requests, to: :assessment + + def create_and_request + ActiveRecord::Base.transaction do + consent_requests.each do |requestable| + RequestRequestable.call(requestable:, user:) + end + + application_form.reload + + ApplicationFormStatusUpdater.call(application_form:, user:) + end + end + + def send_email + DeliverEmail.call( + application_form:, + mailer: TeacherMailer, + action: :consent_requested, + ) + end +end diff --git a/spec/services/request_consent_spec.rb b/spec/services/request_consent_spec.rb new file mode 100644 index 0000000000..fc21655262 --- /dev/null +++ b/spec/services/request_consent_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe RequestConsent do + let(:application_form) { create(:application_form, :submitted) } + let(:assessment) { create(:assessment, application_form:) } + let(:user) { create(:staff) } + + subject(:call) { described_class.call(assessment:, user:) } + + before { create(:consent_request, assessment:) } + + it "changes the application form statuses" do + expect { call }.to change(application_form, :statuses).to( + %w[waiting_on_consent], + ) + end + + it "queues an email job" do + expect { call }.to have_enqueued_mail(TeacherMailer, :consent_requested) + end + + it "records a requestable requested timeline event" do + expect { call }.to have_recorded_timeline_event(:requestable_requested) + end + + context "with an existing request" do + before { create(:requested_consent_request, assessment:) } + + it "raises an error" do + expect { call }.to raise_error(RequestConsent::AlreadyRequested) + end + end +end