-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a service which encapsulates the logic related to requesting consent so it can be used outside the controller.
- Loading branch information
1 parent
eab3ad4
commit 5d167c5
Showing
3 changed files
with
85 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |