Skip to content

Commit

Permalink
SC-85 Evaluator journey: Download documents (#2023)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwin-jebaraj authored Jan 22, 2025
2 parents b5d4542 + 394518f commit 13c7a22
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/assets/config/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
//= link_tree ../../javascript/faf
//= link_tree ../../javascript/misc
//= link_tree ../../javascript/request
//= link tick.svg
1 change: 1 addition & 0 deletions app/assets/images/tick.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions app/controllers/evaluation/download_documents_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module Evaluation
class DownloadDocumentsController < ApplicationController
before_action :set_current_case
before_action :check_user_is_evaluator
before_action :set_documents
before_action :set_downloaded_documents
before_action :set_download_document, only: %i[update]
before_action { @back_url = evaluation_task_path(@current_case) }
SUPPORTED_TYPES = [
"Support::EmailAttachment",
"EmailAttachment",
"Support::CaseAttachment",
"EnergyBill",
"Support::EmailTemplateAttachment",
"Support::CaseUploadDocument",
].freeze
def show; end

def update
update_support_details
send_data @download_document.file.download, type: @download_document.file_type, disposition: "inline", filename: @download_document.file_name
end

private

helper_method def current_evaluator
@current_evaluator ||= Support::Evaluator.find_by(support_case_id: params[:id], email: current_user.email)
end
def set_current_case
@current_case = Support::Case.find(params[:id])
@evaluation_due_date = @current_case.evaluation_due_date? ? @current_case.evaluation_due_date.strftime("%d %B %Y") : nil
end

def set_downloaded_documents
@downloaded_documents = Support::EvaluatorsDownloadDocument.where(support_case_id: @current_case.id, email: current_user.email)
end

def check_user_is_evaluator
return if @current_evaluator.nil? || current_user == @current_evaluator.user

redirect_to root_path, notice: I18n.t("evaluation.tasks.not_permitted")
end

def set_documents
@documents = @current_case.upload_documents
end

def download_document_data
[@download_document.file.download, { type: @download_document.file_type, disposition: "inline", filename: @download_document.file_name }]
end

def update_support_details
Support::EvaluatorsDownloadDocument.upsert(
{
support_case_upload_document_id: params[:document_id],
support_case_id: @download_document.support_case_id,
email: current_user.email,
has_downloaded_documents: true,
},
unique_by: %i[email support_case_id support_case_upload_document_id],
)
end

def set_download_document
@requested_file_type = CGI.unescape(params[:document_type])
if SUPPORTED_TYPES.include?(@requested_file_type)
@download_document = @requested_file_type.safe_constantize.find(params[:document_id])
else
head :unsupported_media_type
end
end
end
end
23 changes: 22 additions & 1 deletion app/controllers/evaluation/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module Evaluation
class TasksController < ApplicationController
before_action :set_current_case
before_action :check_user_is_evaluator
before_action :set_uploaded_documents
before_action :set_downloaded_documents
before_action :download_document_status

def edit
session[:email_evaluator_link] = evaluation_task_path(@current_case, host: request.host)
Expand All @@ -22,9 +25,27 @@ def set_current_case
end

def check_user_is_evaluator
return if @current_evaluator.nil? || current_user == @current_evaluator.user
return if current_evaluator.present? && current_user.email == current_evaluator.email

redirect_to root_path, notice: I18n.t("evaluation.tasks.not_permitted")
end

def set_downloaded_documents
@downloaded_documents = Support::EvaluatorsDownloadDocument.where(support_case_id: params[:id], email: current_user.email)
end

def set_uploaded_documents
@documents = @current_case.upload_documents
end

def download_document_status
@download_document_status = if @documents.count == @downloaded_documents.count && @documents.any?
"complete"
elsif @documents.count > @downloaded_documents.count && @downloaded_documents.any?
"in_progress"
else
"to_do"
end
end
end
end
15 changes: 15 additions & 0 deletions app/javascript/controllers/download_documents_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="download_documents"
export default class extends Controller {
static targets = ["downloadLinks"]

connect() {
this.downloadLinksTarget.querySelectorAll('.govuk-link').forEach(link => {
link.addEventListener("click", (e) => {
e.target.closest('.govuk-summary-list__row').querySelector('.dowloaded-icon').classList.remove('govuk-!-display-none');
});
});
}

}
3 changes: 3 additions & 0 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@ application.register("select-option-filter", SelectOptionFilterController)

import SelectOptionTextFieldController from "./select_option_text_field_controller"
application.register("select-option-text-field", SelectOptionTextFieldController)

import DownloadDocumentsController from "./download_documents_controller"
application.register("download_documents", DownloadDocumentsController)
5 changes: 5 additions & 0 deletions app/models/support/evaluators_download_document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Support
class EvaluatorsDownloadDocument < ApplicationRecord
belongs_to :support_case, class_name: "Support::Case"
end
end
17 changes: 17 additions & 0 deletions app/views/evaluation/download_documents/_document_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%= form_with url: evaluation_download_document_path(@current_case), method: :patch do |form| %>
<div class="govuk-form-group govuk-!-static-margin-bottom-8" data-controller="download_documents">
<dl class="govuk-summary-list" data-download_documents-target="downloadLinks">
<% @documents.each do |document| %>
<div class="govuk-summary-list__row">
<dd class="govuk-summary-list__value">
<%= link_to document.file_name, evaluation_download_document_path(@current_case, document_type: document.class, document_id: document.id), method: :put, class: "govuk-link" %>
</dd>
<% hideClass = @downloaded_documents.present? && @downloaded_documents.map(&:support_case_upload_document_id).include?(document.id) ? '' : 'govuk-!-display-none' %>
<dd class="govuk-summary-list__value govuk-!-padding-top-4 dowloaded-icon <%= hideClass%>">
<%= image_tag "tick.svg", alt: "Already downloaded", width: "24", height: "24" %>
</dd>
</div>
<% end %>
</dl>
</div>
<% end %>
13 changes: 13 additions & 0 deletions app/views/evaluation/download_documents/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= render partial: "support/cases/components/case_header",
locals: { current_case: @current_case } %>
<h1 class="govuk-heading-l"><%= I18n.t("evaluation.download_documents.header") %></h1>
<p class="govuk-body">Use these documents to complete your evaluation of each supplier</p>
<p class="govuk-body govuk-!-static-margin-bottom-8">If you have a problem or conflict of interest,
<% mail_to_address = "mailto:#{ENV.fetch('MS_GRAPH_SHARED_MAILBOX_ADDRESS')}?subject= Case #{ @current_case.ref} - query" %>
<%= link_to "contact us", mail_to_address , class: "govuk-link" %>
</p>
<%= render "document_list"%>
<div class="govuk-button-group flex-align-center">
<%= link_to I18n.t("generic.button.continue"), @back_url, class: "govuk-button" %>
<%= link_to I18n.t("generic.button.cancel"), @back_url, class: "govuk-link govuk-link--no-visited-state" %>
</div>
11 changes: 10 additions & 1 deletion app/views/evaluation/tasks/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@
<p class="govuk-body govuk-!-static-margin-bottom-8">Work through the tasklist to complete your evaluation and upload it by <%= @evaluation_due_date%></p>

<%= govuk_task_list(id_prefix: "evaluator_task") do |task_list|
task_list.with_item(title: I18n.t("evaluation.task_list.item.download_documents"), href: '#', status: govuk_tag(text: I18n.t("support.case.label.tasklist.status.to_do")))

if @download_document_status == 'complete'
download_status = govuk_tag(text: I18n.t("support.case.label.tasklist.status.complete"), colour: "green")
elsif @download_document_status == 'in_progress'
download_status = govuk_tag(text: I18n.t("support.case.label.tasklist.status.in_progress"))
else
download_status = govuk_tag(text: I18n.t("support.case.label.tasklist.status.to_do"))
end

task_list.with_item(title: I18n.t("evaluation.task_list.item.download_documents"), href: evaluation_download_document_path(@current_case), status: download_status)

task_list.with_item(title: I18n.t("evaluation.task_list.item.upload_completed_documents")) do | item |
item.with_status(text: govuk_tag(text: I18n.t("support.case.label.tasklist.status.cannot_start"), colour: "grey"), cannot_start_yet: true)
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ en:
download_documents: Download documents
upload_completed_documents: Upload completed documents
evaluation_approved_by_dfe: Evaluation approved by DfE
download_documents:
header: Download documents
exit_survey:
better_quality:
options:
Expand Down Expand Up @@ -798,6 +800,7 @@ en:
start: Start now
submit: Submit
update: Update
continue: Continue
label:
what_is: What is %{label}?
link:
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@
end

namespace :evaluation do
resources :download_documents, only: %i[show update]
resources :tasks, only: %i[show edit]
get "verify/evaluator/link/:id", to: "tasks#edit", as: :verify_evaluators_unique_link
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AddSupportEvaluatorsDownloadDocuments < ActiveRecord::Migration[7.2]
def change
create_table "support_evaluators_download_documents", id: :uuid do |t|
t.references "support_case", type: :uuid
t.references "support_case_upload_document", type: :uuid
t.string "email", null: false
t.boolean "has_downloaded_documents", default: false
t.timestamps
t.index %w[email support_case_id support_case_upload_document_id], unique: true
end
end
end
14 changes: 13 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2024_12_11_171744) do
ActiveRecord::Schema[7.2].define(version: 2025_01_10_150114) do
create_sequence "evaluation_refs"
create_sequence "framework_refs"

Expand Down Expand Up @@ -828,6 +828,18 @@
t.index ["support_case_id"], name: "index_support_evaluators_on_support_case_id"
end

create_table "support_evaluators_download_documents", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "support_case_id"
t.uuid "support_case_upload_document_id"
t.string "email", null: false
t.boolean "has_downloaded_documents", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email", "support_case_id", "support_case_upload_document_id"], name: "idx_on_email_support_case_id_support_case_upload_do_e4a88327e6", unique: true
t.index ["support_case_id"], name: "index_support_evaluators_download_documents_on_support_case_id"
t.index ["support_case_upload_document_id"], name: "idx_on_support_case_upload_document_id_fbb53116e2"
end

create_table "support_frameworks", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name"
t.string "supplier"
Expand Down
48 changes: 48 additions & 0 deletions spec/features/evaluation/download_documents_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "rails_helper"
describe "Evaluator can see uploaded documents", :js do
let(:support_case) { create(:support_case) }
let(:user) { create(:user) }
let(:file_1) { fixture_file_upload(Rails.root.join("spec/fixtures/support/text-file.txt"), "text/plain") }
let(:file_2) { fixture_file_upload(Rails.root.join("spec/fixtures/support/another-text-file.txt"), "text/plain") }
let(:document_uploader) { support_case.document_uploader(files: [file_1, file_2]) }

specify "Evaluator can download documents" do
create(:support_evaluator, support_case:, dsi_uid: user.dfe_sign_in_uid, email: user.email)

Current.user = user

user_exists_in_dfe_sign_in(user:)

user_is_signed_in(user:)

expect { document_uploader.save! }.to change { support_case.upload_documents.count }.from(0).to(2)

visit evaluation_task_path(support_case)

expect(page).to have_text("Evaluator task list")

expect(find("#evaluator_task-1-status")).to have_text("To do")

visit evaluation_download_document_path(support_case)

expect(page).to have_text("Download documents")

expect(page).to have_content("text-file.txt")

expect(page).to have_content("another-text-file.txt")

find_all(".govuk-summary-list__row a")[0].click

visit evaluation_task_path(support_case)

expect(find("#evaluator_task-1-status")).to have_text("In progress")

visit evaluation_download_document_path(support_case)

find_all(".govuk-summary-list__row a")[1].click

visit evaluation_task_path(support_case)

expect(find("#evaluator_task-1-status")).to have_text("Complete")
end
end
2 changes: 1 addition & 1 deletion spec/features/evaluation/task_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let(:user) { create(:user) }

specify "Authenticating and seeing the task list" do
create(:support_evaluator, support_case:, dsi_uid: user.dfe_sign_in_uid)
create(:support_evaluator, support_case:, dsi_uid: user.dfe_sign_in_uid, email: user.email)
Current.user = user
user_exists_in_dfe_sign_in(user:)

Expand Down

0 comments on commit 13c7a22

Please sign in to comment.