Skip to content

Commit

Permalink
Add QualificationRequestsViewObject
Browse files Browse the repository at this point in the history
This adds a view object for the qualification requests.
  • Loading branch information
thomasleese committed Feb 16, 2024
1 parent 9b08622 commit 3d5f648
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/components/task_list/component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
</h2>
<% end %>

<% if (description = section[:description]).present? %>
<p class="govuk-body"><%= description %></p>
<% end %>

<ul class="<%= section[:indentation] == false ? 'app-task-list__items-no-indentation' : 'app-task-list__items' %>">
<% section[:items].each do |item| %>
<li class="app-task-list__item">
Expand Down
1 change: 1 addition & 0 deletions app/components/task_list/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Component < ViewComponent::Base
# sections: [
# {
# title: "Title",
# description: "Description",
# items: [
# {
# name: "Do this thing",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# frozen_string_literal: true

module AssessorInterface
class QualificationRequestsViewObject
include QualificationHelper

def initialize(application_form:)
@application_form = application_form
end

def all_task_items
items = [
check_consent_method_task_item,
if generate_consent_document_in_all_qualifications?
generate_consent_document_task_item
end,
].compact

if show_individual_task_items?
items
else
items +
individual_task_items_for(
qualification_request: qualification_requests.first,
)
end
end

def show_individual_task_items?
all_consent_methods_selected? && qualification_requests.count > 1
end

def individual_task_items_for(qualification_request:)
return [] if qualification_request.consent_method_unknown?

if qualification_request.consent_method_unsigned?
unsigned_consent_method_task_items(qualification_request)
else
signed_consent_method_task_items(qualification_request)
end + ecctis_task_items(qualification_request)
end

private

attr_reader :application_form

delegate :assessment, to: :application_form

def qualification_requests
@qualification_requests ||=
application_form.assessment.qualification_requests.order_by_user
end

def all_consent_methods_selected?
qualification_requests.none?(&:consent_method_unknown?)
end

def check_consent_method_task_item
status =
if qualification_requests.all?(&:consent_method_unknown?)
"not_started"
elsif all_consent_methods_selected?
"completed"
else
"in_progress"
end

{ name: "Check and select consent method", link: "#", status: }
end

def generate_consent_document_in_all_qualifications?
all_consent_methods_selected? &&
qualification_requests.consent_method_unsigned.count >= 2
end

def generate_consent_document_task_item
{
name: "Generate consent document",
link: "#",
status:
(
if assessment.unsigned_consent_document_generated
"completed"
else
"not_started"
end
),
}
end

def unsigned_consent_method_task_items(_qualification_request)
if generate_consent_document_in_all_qualifications?
[]
else
[generate_consent_document_task_item]
end
end

def signed_consent_method_task_items(qualification_request)
document_uploaded =
qualification_request.unsigned_consent_document.completed?

[
{
name: "Upload consent document",
link: "#",
status: document_uploaded ? "completed" : "not_started",
},
{
name: "Send consent document to applicant",
link: "#",
status:
if document_uploaded
if qualification_request.consent_requested?
"completed"
else
"not_started"
end
else
"cannot_start"
end,
},
{
name: "Record applicant response",
link: "#",
status:
if document_uploaded
if qualification_request.consent_received?
"completed"
else
"not_started"
end
else
"cannot_start"
end,
},
]
end

def ecctis_task_items(qualification_request)
can_start =
(
qualification_request.consent_method_unsigned? &&
assessment.unsigned_consent_document_generated
) || qualification_request.consent_received?

[
{
name: "Request Ecctis verification",
link: "#",
status:
if can_start
qualification_request.requested? ? "completed" : "not_started"
else
"cannot_start"
end,
},
{
name: "Record Ecctis response",
link: "#",
status:
if can_start && qualification_request.requested?
qualification_request.received? ? "completed" : "not_started"
else
"cannot_start"
end,
},
]
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe AssessorInterface::QualificationRequestsViewObject do
subject(:view_object) { described_class.new(application_form:) }

let(:application_form) do
create(:application_form, :verification_stage, :with_assessment)
end

let!(:qualification_request) do
create(
:qualification_request,
assessment: application_form.assessment,
qualification: create(:qualification, application_form:),
)
end

describe "#all_task_items" do
subject(:all_task_items) { view_object.all_task_items }

it do
is_expected.to eq(
[
{
name: "Check and select consent method",
link: "#",
status: "not_started",
},
],
)
end

context "when consent methods are chosen" do
before { qualification_request.consent_method_unsigned! }

it do
is_expected.to eq(
[
{
name: "Check and select consent method",
link: "#",
status: "completed",
},
{
name: "Generate consent document",
link: "#",
status: "not_started",
},
{
name: "Request Ecctis verification",
link: "#",
status: "cannot_start",
},
{
name: "Record Ecctis response",
link: "#",
status: "cannot_start",
},
],
)
end
end
end

describe "#show_individual_task_items?" do
subject(:show_individual_task_items?) do
view_object.show_individual_task_items?
end

it { is_expected.to be false }

context "with more than one qualification request" do
before do
qualification_request.consent_method_unsigned!
create(
:qualification_request,
assessment: application_form.assessment,
consent_method: "unsigned",
qualification: create(:qualification, application_form:),
)
end

it { is_expected.to be true }
end
end

describe "#individual_task_items_for" do
subject(:individual_task_items_for) do
view_object.individual_task_items_for(qualification_request:)
end

it { is_expected.to be_empty }

context "when unsigned consent method" do
before { qualification_request.consent_method_unsigned! }

it do
is_expected.to eq(
[
{
name: "Generate consent document",
link: "#",
status: "not_started",
},
{
name: "Request Ecctis verification",
link: "#",
status: "cannot_start",
},
{
name: "Record Ecctis response",
link: "#",
status: "cannot_start",
},
],
)
end
end

context "when signed consent method" do
before { qualification_request.consent_method_signed_ecctis! }

it do
is_expected.to eq(
[
{
name: "Upload consent document",
link: "#",
status: "not_started",
},
{
name: "Send consent document to applicant",
link: "#",
status: "cannot_start",
},
{
name: "Record applicant response",
link: "#",
status: "cannot_start",
},
{
name: "Request Ecctis verification",
link: "#",
status: "cannot_start",
},
{
name: "Record Ecctis response",
link: "#",
status: "cannot_start",
},
],
)
end
end
end
end

0 comments on commit 3d5f648

Please sign in to comment.