Skip to content
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 @@ -10,8 +10,10 @@ class FurtherInformationRequestsController < BaseController
authorize [:assessor_interface, further_information_request]
end

before_action :ensure_can_decline, only: %i[edit_decline update_decline]
before_action :load_application_form_and_assessment, only: %i[new edit]
before_action :load_view_object, only: %i[edit update]
before_action :load_view_object,
only: %i[edit update edit_decline update_decline]

def new
@further_information_request =
Expand All @@ -34,19 +36,57 @@ def create

def edit
@form =
RequestableReviewForm.new(
requestable: view_object.further_information_request,
form_class.new(
user: current_staff,
passed: view_object.further_information_request.review_passed,
note: view_object.further_information_request.review_note,
**form_class.initial_attributes(further_information_request),
)
end

def update
@form =
RequestableReviewForm.new(
requestable_review_form_params.merge(
requestable: view_object.further_information_request,
form_class.new(
further_information_request_review_form_params.merge(
further_information_request:,
user: current_staff,
),
)

if @form.save
if @form.all_further_information_request_items_accepted?
redirect_to [
:edit,
:assessor_interface,
view_object.application_form,
view_object.assessment,
]
else
redirect_to [
:decline,
:assessor_interface,
view_object.application_form,
view_object.assessment,
view_object.further_information_request,
]
end
else
render :edit, status: :unprocessable_entity
end
end

def edit_decline
@form =
AssessorInterface::FurtherInformationRequestDeclineForm.new(
further_information_request:,
user: current_staff,
note: further_information_request.review_note,
)
end

def update_decline
@form =
AssessorInterface::FurtherInformationRequestDeclineForm.new(
further_information_request_decline_form_params.merge(
further_information_request:,
user: current_staff,
),
)
Expand All @@ -59,12 +99,19 @@ def update
view_object.assessment,
]
else
render :edit, status: :unprocessable_entity
render :edit_decline, status: :unprocessable_entity
end
end

private

def form_class
@form_class ||=
AssessorInterface::FurtherInformationRequestReviewForm.for_further_information_request(
further_information_request,
)
end

def load_application_form_and_assessment
@application_form = application_form
@assessment = assessment
Expand Down Expand Up @@ -98,11 +145,26 @@ def view_object
@view_object ||= FurtherInformationRequestViewObject.new(params:)
end

def requestable_review_form_params
params.require(:assessor_interface_requestable_review_form).permit(
:passed,
:note,
)
def further_information_request_review_form_params
unless params[:assessor_interface_further_information_request_review_form]
return {}
end

params.require(
:assessor_interface_further_information_request_review_form,
).permit(*form_class.permittable_parameters(further_information_request))
end

def further_information_request_decline_form_params
params.require(
:assessor_interface_further_information_request_decline_form,
).permit(:note)
end

def ensure_can_decline
return if view_object.can_decline?

redirect_to [:assessor_interface, view_object.application_form]
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

class AssessorInterface::FurtherInformationRequestDeclineForm
include ActiveModel::Model
include ActiveModel::Attributes

attr_accessor :further_information_request, :user
validates :further_information_request, :user, presence: true

attribute :note, :string
validates :note, presence: true

def save
return false if invalid?

ReviewRequestable.call(
requestable: further_information_request,
user:,
passed: false,
note:,
)

true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# frozen_string_literal: true

class AssessorInterface::FurtherInformationRequestReviewForm
include ActiveModel::Model
include ActiveModel::Attributes

attr_accessor :further_information_request, :user
validates :further_information_request, :user, presence: true

def save
return false if invalid?

ActiveRecord::Base.transaction do
further_information_request.items.each do |item|
review_decision = send("#{item.id}_decision")

item.update!(review_decision:)
end
end

if all_further_information_request_items_accepted?
ReviewRequestable.call(
requestable: further_information_request,
user:,
passed: true,
note: "",
)
end

true
end

def all_further_information_request_items_accepted?
further_information_request.items.all?(&:review_decision_accept?)
end

class << self
def for_further_information_request(further_information_request)
klass =
Class.new(self) do
mattr_accessor :preliminary

def self.name
"AssessorInterface::FurtherInformationRequestReviewForm"
end
end

further_information_request.items.each do |item|
klass.attribute "#{item.id}_decision"
klass.validates "#{item.id}_decision",
inclusion: {
in:
FurtherInformationRequestItem.review_decisions.keys,
message:
"Select how you would like to respond to the applicant’s information",
}
end

klass
end

def initial_attributes(further_information_request)
attributes = { further_information_request: }

further_information_request.items.each do |item|
attributes["#{item.id}_decision"] = item.review_decision
end

attributes
end

def permittable_parameters(further_information_request)
further_information_request.items.map { |item| "#{item.id}_decision" }
end
end
end
11 changes: 11 additions & 0 deletions app/models/further_information_request_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# failure_reason_key :string default(""), not null
# information_type :string
# response :text
# review_decision :string
# review_decision_note :text
# created_at :datetime not null
# updated_at :datetime not null
# further_information_request_id :bigint
Expand Down Expand Up @@ -40,6 +42,8 @@ class FurtherInformationRequestItem < ApplicationRecord
work_history_contact: "work_history_contact",
}

enum :review_decision, { accept: "accept", decline: "decline" }, prefix: true

def status
if completed?
"completed"
Expand Down Expand Up @@ -76,4 +80,11 @@ def update_work_history_contact(user:)
email: contact_email,
)
end

def assessment_section
assessment
.sections
.includes(:selected_failure_reasons)
.find_by(selected_failure_reasons: { key: failure_reason_key })
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ def edit?

true
end

def edit_decline?
edit?
end

def update_decline?
update?
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,14 @@ def further_information_request_task_list_item(further_information_request)
if !further_information_request.received?
:cannot_start
elsif !further_information_request.reviewed?
:not_started
if further_information_request
.items
.pluck(:review_decision)
.all?(&:nil?)
:not_started
else
:in_progress
end
elsif assessment.request_further_information?
:in_progress
else
Expand Down
Loading
Loading