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

Refactor consent requests #2037

Merged
merged 3 commits into from
Feb 27, 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
7 changes: 3 additions & 4 deletions app/controllers/teacher_interface/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def document
.assessment
&.further_information_requests
&.flat_map(&:items) || []
) + (application_form.assessment&.qualification_requests || []),
) + (application_form.assessment&.consent_requests || []),
).find(params[:document_id] || params[:id])
end

Expand All @@ -42,9 +42,8 @@ def redirect_unless_draft_or_additional_information
unless document.documentable.further_information_request.requested?
redirect_to %i[teacher_interface application_form]
end
elsif document.for_qualification_request?
if document.documentable.consent_requested_at.nil? ||
document.documentable.consent_received_at.present?
elsif document.for_consent_request?
if !document.documentable.requested? || document.documentable.received?
redirect_to %i[teacher_interface application_form]
end
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# frozen_string_literal: true

module TeacherInterface
class ConsentRequestsController < BaseController
include HandleApplicationFormSection
include HistoryTrackable

before_action :load_consent_request, only: %i[edit_download update_download]

define_history_check :check
define_history_origin :index
define_history_reset :index

def index
@view_object = ConsentRequestsViewObject.new(application_form:)
end

def check
@view_object = ConsentRequestsViewObject.new(application_form:)
end

def submit
ActiveRecord::Base.transaction do
application_form.assessment.consent_requests.each do |requestable|
ReceiveRequestable.call(requestable:, user: current_teacher)
end
end

TeacherMailer.with(application_form:).consent_submitted.deliver_later

redirect_to %i[teacher_interface application_form]
end

def edit_download
@form =
DownloadUnsignedConsentDocumentForm.new(
consent_request:,
downloaded: consent_request.unsigned_document_downloaded,
)
end

def update_download
@form =
DownloadUnsignedConsentDocumentForm.new(
consent_request:,
downloaded:
params.dig(
:teacher_interface_download_unsigned_consent_document_form,
:downloaded,
),
)

handle_application_form_section(
form: @form,
if_success_then_redirect:
teacher_interface_application_form_consent_requests_path,
if_failure_then_render: :edit_download,
)
end

private

attr_reader :consent_request

def load_consent_request
@consent_request =
ConsentRequest
.joins(assessment: :application_form)
.includes(:qualification, :application_form)
.find_by!(id: params[:id], assessment: { application_form: })

@qualification = consent_request.qualification
end
end
end

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class AssessorInterface::UploadUnsignedConsentDocumentForm
include ActiveModel::Attributes
include UploadableForm

attr_accessor :qualification_request
validates :qualification_request, presence: true
attr_accessor :consent_request
validates :consent_request, presence: true

def save
return false if invalid?
Expand All @@ -17,6 +17,6 @@ def save
end

def document
qualification_request&.unsigned_consent_document
consent_request&.unsigned_consent_document
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module TeacherInterface
class DownloadUnsignedConsentDocumentForm < BaseForm
attr_accessor :consent_request
attribute :downloaded, :boolean

validates :consent_request, presence: true
validates :downloaded, presence: true

def update_model
consent_request.update!(unsigned_document_downloaded: true) if downloaded
end
end
end

This file was deleted.

20 changes: 7 additions & 13 deletions app/lib/application_form_status_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def requestable_statuses
end

def overdue_consent
overdue?(requestables: qualification_requests.reject(&:requested?))
overdue?(requestables: consent_requests)
end

def overdue_further_information
Expand All @@ -193,12 +193,7 @@ def overdue_reference
end

def received_consent
qualification_requests
.reject(&:verified?)
.reject(&:reviewed?)
.reject(&:expired?)
.reject(&:requested?)
.any?(&:consent_received?)
received?(requestables: consent_requests)
end

def received_further_information
Expand Down Expand Up @@ -239,12 +234,7 @@ def received_reference
end

def waiting_on_consent
qualification_requests
.reject(&:verified?)
.reject(&:reviewed?)
.reject(&:expired?)
.reject(&:consent_received?)
.any?(&:consent_requested?)
waiting_on?(requestables: consent_requests)
end

def waiting_on_further_information
Expand All @@ -263,6 +253,10 @@ def waiting_on_reference
waiting_on?(requestables: reference_requests)
end

def consent_requests
@consent_requests ||= assessment&.consent_requests.to_a
end

def further_information_requests
@further_information_requests ||=
assessment&.further_information_requests.to_a
Expand Down
8 changes: 4 additions & 4 deletions app/lib/document_continue_redirection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def initialize(document:)
def call
if document.for_further_information_request?
further_information_request_url
elsif document.for_qualification_request?
qualification_request_url
elsif document.for_consent_request?
consent_request_url
else
send("#{document.document_type}_url")
end
Expand Down Expand Up @@ -68,7 +68,7 @@ def further_information_request_url
]
end

def qualification_request_url
%i[teacher_interface application_form qualification_requests]
def consent_request_url
%i[teacher_interface application_form consent_requests]
end
end
14 changes: 2 additions & 12 deletions app/mailers/teacher_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ def application_received
end

def consent_reminder
@expires_at =
assessment
.qualification_requests
.consent_method_signed
.map(&:expires_at)
.max
@expires_at = assessment.consent_requests.map(&:expires_at).max

view_mail(
GOVUK_NOTIFY_TEMPLATE_ID,
Expand All @@ -72,12 +67,7 @@ def consent_reminder
end

def consent_requested
@expires_at =
assessment
.qualification_requests
.consent_method_signed
.map(&:expires_at)
.max
@expires_at = assessment.consent_requests.map(&:expires_at).max

view_mail(
GOVUK_NOTIFY_TEMPLATE_ID,
Expand Down
13 changes: 7 additions & 6 deletions app/models/application_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ def should_send_reminder_email?(name, number_of_reminders_sent)
case name
when "consent"
number_of_reminders_sent.zero? &&
consent_requests_not_yet_received.any? do |qualification_request|
qualification_request.days_until_expired <= 21
consent_requests_not_yet_received_or_rejected.any? do |consent_request|
consent_request.days_until_expired <= 21
end
when "expiration"
return false if days_until_expired.nil?
Expand Down Expand Up @@ -296,7 +296,7 @@ def send_reminder_email(name, number_of_reminders_sent)
end
end

def expires_from
def requested_at
created_at
end

Expand All @@ -314,10 +314,11 @@ def reference_requests_not_yet_received_or_rejected
.where(received_at: nil, verify_passed: nil, review_passed: nil)
end

def consent_requests_not_yet_received
QualificationRequest
def consent_requests_not_yet_received_or_rejected
ConsentRequest
.joins(:qualification)
.where(qualifications: { application_form_id: id })
.consent_respondable
.where.not(requested_at: nil)
.where(received_at: nil, verify_passed: nil)
end
end
4 changes: 3 additions & 1 deletion app/models/assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ class Assessment < ApplicationRecord
belongs_to :application_form

has_many :sections, class_name: "AssessmentSection", dependent: :destroy

has_many :consent_requests, dependent: :destroy
has_many :further_information_requests, dependent: :destroy
has_one :professional_standing_request, dependent: :destroy, required: false
has_many :reference_requests, dependent: :destroy
has_many :qualification_requests, dependent: :destroy
has_many :reference_requests, dependent: :destroy

enum :recommendation,
{
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/expirable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module Expirable
extend ActiveSupport::Concern

def expires_at
return nil if expires_from.nil? || expires_after.nil?
return nil if requested_at.nil? || expires_after.nil?

expires_from + expires_after
requested_at + expires_after
end

def days_until_expired
Expand Down
Loading
Loading