Skip to content

Commit

Permalink
Add RequestRequestable
Browse files Browse the repository at this point in the history
This adds a service, which works in a similar way to ExpireRequestable
and ReceiveRequestable to handle the transition from created to
requested.
  • Loading branch information
thomasleese committed Oct 4, 2023
1 parent a8a5cb7 commit ad5d73d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 48 deletions.
22 changes: 6 additions & 16 deletions app/services/create_further_information_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
44 changes: 44 additions & 0 deletions app/services/request_requestable.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 4 additions & 15 deletions app/services/submit_application_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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)

if application_form.reduced_evidence_accepted
UpdateAssessmentInductionRequired.call(assessment:)
end

ApplicationFormStatusUpdater.call(application_form:, user:)
end

TeacherMailer
Expand Down Expand Up @@ -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
23 changes: 6 additions & 17 deletions app/services/verify_assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions spec/services/request_requestable_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ad5d73d

Please sign in to comment.