Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RequestConsent #2124

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 49 additions & 0 deletions app/services/request_consent.rb
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions spec/services/request_consent_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading