From a1bd59552d4f3212a4a2b719aa72179baef730ef Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 5 Dec 2022 18:33:34 +0000 Subject: [PATCH] Cldc 1211 close collection period (#1044) * Conditionally display a correct review log text * do not allow the aswers from previous collection windows to be changed * does not let the user navigate to questions for previous collection year logs * Fix tests * Add some docs * Extract collection period check into a before_action and change path to rails route * update the tasklist message --- ...swers_summary_list_card_component.html.erb | 12 +++--- app/controllers/form_controller.rb | 7 ++++ app/helpers/tasklist_helper.rb | 8 ++++ app/models/log.rb | 4 ++ .../form/_check_answers_summary_list.html.erb | 12 +++--- app/views/form/review.html.erb | 2 +- app/views/logs/edit.html.erb | 2 +- docs/form/definition.md | 6 +++ .../form/accessible_autocomplete_spec.rb | 1 + spec/features/form/check_answers_page_spec.rb | 2 + spec/features/form/checkboxes_spec.rb | 1 + .../form/conditional_questions_spec.rb | 2 + spec/features/form/form_navigation_spec.rb | 2 + spec/features/form/page_routing_spec.rb | 1 + .../form/progressive_total_field_spec.rb | 1 + spec/features/form/saving_data_spec.rb | 1 + spec/features/form/tasklist_page_spec.rb | 1 + spec/features/form/validations_spec.rb | 2 + spec/requests/form_controller_spec.rb | 6 +++ .../requests/lettings_logs_controller_spec.rb | 38 ++++++++++++++++++- 20 files changed, 98 insertions(+), 13 deletions(-) diff --git a/app/components/check_answers_summary_list_card_component.html.erb b/app/components/check_answers_summary_list_card_component.html.erb index f299674b2..9b0c67872 100644 --- a/app/components/check_answers_summary_list_card_component.html.erb +++ b/app/components/check_answers_summary_list_card_component.html.erb @@ -25,11 +25,13 @@ <%= inferred_answer %> <% end %> <% end %> - <% row.action( - text: question.action_text(log), - href: question.action_href(log, question.page.id), - visually_hidden_text: question.check_answer_label.to_s.downcase, - ) %> + <% if @log.collection_period_open? %> + <% row.action( + text: question.action_text(log), + href: question.action_href(log, question.page.id), + visually_hidden_text: question.check_answer_label.to_s.downcase, + ) %> + <% end %> <% end %> <% end %> <% end %> diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 6e71de6c6..19d47f8e3 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -2,6 +2,7 @@ class FormController < ApplicationController before_action :authenticate_user! before_action :find_resource, only: %i[submit_form review] before_action :find_resource_by_named_id, except: %i[submit_form review] + before_action :check_collection_period, only: %i[submit_form show_page] def submit_form if @log @@ -176,4 +177,10 @@ private responses_for_page[question.id].nil? || responses_for_page[question.id].blank? end end + + def check_collection_period + return unless @log + + redirect_to lettings_log_path(@log) unless @log.collection_period_open? + end end diff --git a/app/helpers/tasklist_helper.rb b/app/helpers/tasklist_helper.rb index 7ab955a2e..05c84d1b7 100644 --- a/app/helpers/tasklist_helper.rb +++ b/app/helpers/tasklist_helper.rb @@ -36,4 +36,12 @@ module TasklistHelper subsection.label end end + + def review_log_text(log) + if log.collection_period_open? + "You can #{govuk_link_to 'review and make changes to this log', review_lettings_log_path(log)} until #{(log.form.end_date).to_formatted_s(:govuk_date)}.".html_safe + else + "This log is from the #{log.form.start_date.year}/#{log.form.start_date.year + 1} collection window, which is now closed." + end + end end diff --git a/app/models/log.rb b/app/models/log.rb index 4ddb63246..e3a85e9ee 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -45,6 +45,10 @@ class Log < ApplicationRecord managing_organisation&.provider_type end + def collection_period_open? + form.end_date > Time.zone.today + end + private def update_status! diff --git a/app/views/form/_check_answers_summary_list.html.erb b/app/views/form/_check_answers_summary_list.html.erb index 68a9cf00a..56ae87905 100644 --- a/app/views/form/_check_answers_summary_list.html.erb +++ b/app/views/form/_check_answers_summary_list.html.erb @@ -13,11 +13,13 @@ <%= inferred_answer %> <% end %> <% end %> - <% row.action( - text: question.action_text(@log), - href: question.action_href(@log, question.page.id), - visually_hidden_text: question.check_answer_label.to_s.downcase, - ) %> + <% if @log.collection_period_open? %> + <% row.action( + text: question.action_text(@log), + href: question.action_href(@log, question.page.id), + visually_hidden_text: question.check_answer_label.to_s.downcase, + ) %> + <% end %> <% end %> <% end %> <% end %> diff --git a/app/views/form/review.html.erb b/app/views/form/review.html.erb index 127e52762..a95b577bd 100644 --- a/app/views/form/review.html.erb +++ b/app/views/form/review.html.erb @@ -11,7 +11,7 @@ <%= content_for(:title) %>
- You can review and make changes to this log until 2nd June <%= @log.collection_start_year.present? ? @log.collection_start_year + 1 : "" %>. + <%= review_log_text(@log) %>
<% @log.form.sections.map do |section| %>- You can <%= govuk_link_to "review and make changes to this log", "/lettings-logs/#{@log.id}/review" %> until 2nd June <%= @log.collection_start_year.present? ? @log.collection_start_year + 1 : "" %>. + <%= review_log_text(@log) %>
<% end %> <%= render "tasklist" %> diff --git a/docs/form/definition.md b/docs/form/definition.md index 499abd079..02b0e0155 100644 --- a/docs/form/definition.md +++ b/docs/form/definition.md @@ -55,6 +55,9 @@ A form has the following attributes: - `start_date`: The start date of the form, in ISO 8601 format - `end_date`: The end date of the form, in ISO 8601 format +Each form has an `end_date` which for JSON forms is defined in the form definition JSON file and for code defined forms it is set to 1st July, 1 year after the start year. +Logs with a form that has `end_date` in the past can no longer be edited through the UI. + ## Form views The main view used for rendering the form is the `app/views/form/page.html.erb` view as the Form contains multiple pages (which live in subsections within sections). This page view then renders the appropriate partials for the question types of the questions on the current page. @@ -79,3 +82,6 @@ The form controller handles the form submission as well as the rendering of the ## FormHandler helper class The FormHandler helper is a helper that loads all of the defined forms and initialises them as Form objects. It can also be used to get specific forms if needed. +When the log type is chosen and the date is entered in the setup section of the form, an appropriate form for that log is selected and associated with the log. + +The current collection window gets incremented automatically in `FormHandler` and is determined within the `form_name_from_start_year`by looking at `current_collection_start_year` method which would increment the collection window on the 1st of April each year. diff --git a/spec/features/form/accessible_autocomplete_spec.rb b/spec/features/form/accessible_autocomplete_spec.rb index 52317107a..9c7c1b9ea 100644 --- a/spec/features/form/accessible_autocomplete_spec.rb +++ b/spec/features/form/accessible_autocomplete_spec.rb @@ -19,6 +19,7 @@ RSpec.describe "Accessible Automcomplete" do end before do + allow(lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) sign_in user end diff --git a/spec/features/form/check_answers_page_spec.rb b/spec/features/form/check_answers_page_spec.rb index 5f4665725..4ea6a905f 100644 --- a/spec/features/form/check_answers_page_spec.rb +++ b/spec/features/form/check_answers_page_spec.rb @@ -43,6 +43,8 @@ RSpec.describe "Form Check Answers Page" do let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } before do + allow(lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) + allow(fake_2021_2022_form).to receive(:end_date).and_return(Time.zone.today + 1.day) sign_in user allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) end diff --git a/spec/features/form/checkboxes_spec.rb b/spec/features/form/checkboxes_spec.rb index f7a3df152..d3e3671eb 100644 --- a/spec/features/form/checkboxes_spec.rb +++ b/spec/features/form/checkboxes_spec.rb @@ -16,6 +16,7 @@ RSpec.describe "Checkboxes" do let(:id) { lettings_log.id } before do + allow(lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) RequestHelper.stub_http_requests sign_in user end diff --git a/spec/features/form/conditional_questions_spec.rb b/spec/features/form/conditional_questions_spec.rb index 33d99955e..07a660321 100644 --- a/spec/features/form/conditional_questions_spec.rb +++ b/spec/features/form/conditional_questions_spec.rb @@ -25,6 +25,8 @@ RSpec.describe "Form Conditional Questions" do before do sign_in user + allow(sales_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) + allow(lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) end diff --git a/spec/features/form/form_navigation_spec.rb b/spec/features/form/form_navigation_spec.rb index ffa9bdd88..351675804 100644 --- a/spec/features/form/form_navigation_spec.rb +++ b/spec/features/form/form_navigation_spec.rb @@ -35,6 +35,8 @@ RSpec.describe "Form Navigation" do let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } before do + allow(lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) + allow(fake_2021_2022_form).to receive(:end_date).and_return(Time.zone.today + 1.day) sign_in user allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) end diff --git a/spec/features/form/page_routing_spec.rb b/spec/features/form/page_routing_spec.rb index 27f4feaf5..b60c2dd03 100644 --- a/spec/features/form/page_routing_spec.rb +++ b/spec/features/form/page_routing_spec.rb @@ -16,6 +16,7 @@ RSpec.describe "Form Page Routing" do let(:validator) { lettings_log._validators[nil].first } before do + allow(lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) allow(validator).to receive(:validate_pregnancy).and_return(true) sign_in user end diff --git a/spec/features/form/progressive_total_field_spec.rb b/spec/features/form/progressive_total_field_spec.rb index 5080de08c..77edfbedd 100644 --- a/spec/features/form/progressive_total_field_spec.rb +++ b/spec/features/form/progressive_total_field_spec.rb @@ -14,6 +14,7 @@ RSpec.describe "Accessible Automcomplete" do end before do + allow(lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) sign_in user end diff --git a/spec/features/form/saving_data_spec.rb b/spec/features/form/saving_data_spec.rb index 8312cfad9..a0889e9a4 100644 --- a/spec/features/form/saving_data_spec.rb +++ b/spec/features/form/saving_data_spec.rb @@ -30,6 +30,7 @@ RSpec.describe "Form Saving Data" do end before do + allow(lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) sign_in user end diff --git a/spec/features/form/tasklist_page_spec.rb b/spec/features/form/tasklist_page_spec.rb index 3f7ba6f1e..09d332583 100644 --- a/spec/features/form/tasklist_page_spec.rb +++ b/spec/features/form/tasklist_page_spec.rb @@ -44,6 +44,7 @@ RSpec.describe "Task List" do let(:status) { lettings_log.status } before do + allow(lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) sign_in user end diff --git a/spec/features/form/validations_spec.rb b/spec/features/form/validations_spec.rb index 0790096e2..9df179937 100644 --- a/spec/features/form/validations_spec.rb +++ b/spec/features/form/validations_spec.rb @@ -34,6 +34,8 @@ RSpec.describe "validations" do let(:id) { lettings_log.id } before do + allow(fake_2021_2022_form).to receive(:end_date).and_return(Time.zone.today + 1.day) + allow(lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) sign_in user allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) end diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index a7f374f27..cb5523b8e 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -35,6 +35,7 @@ RSpec.describe FormController, type: :request do let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } before do + allow(fake_2021_2022_form).to receive(:end_date).and_return(Time.zone.today + 1.day) allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) end @@ -87,6 +88,10 @@ RSpec.describe FormController, type: :request do let(:lettings_log_year_1) { create(:lettings_log, startdate: Time.zone.local(2021, 5, 1), owning_organisation: organisation, created_by: user) } let(:lettings_log_year_2) { create(:lettings_log, :about_completed, startdate: Time.zone.local(2022, 5, 1), owning_organisation: organisation, created_by: user) } + before do + allow(lettings_log_year_1.form).to receive(:end_date).and_return(Time.zone.today + 1.day) + end + it "displays the correct question details for each lettings log based on form year" do get "/lettings-logs/#{lettings_log_year_1.id}/tenant-code-test", headers: headers, params: {} expect(response.body).to include("What is the tenant code?") @@ -505,6 +510,7 @@ RSpec.describe FormController, type: :request do before do completed_lettings_log.update!(ecstat1: 1, earnings: 130, hhmemb: 1) # we're not routing to that page, so it gets cleared?§ allow(completed_lettings_log).to receive(:net_income_soft_validation_triggered?).and_return(true) + allow(completed_lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) post "/lettings-logs/#{completed_lettings_log.id}/form", params: interrupt_params, headers: headers.merge({ "HTTP_REFERER" => referrer }) end diff --git a/spec/requests/lettings_logs_controller_spec.rb b/spec/requests/lettings_logs_controller_spec.rb index d41c4d0a5..7745a9295 100644 --- a/spec/requests/lettings_logs_controller_spec.rb +++ b/spec/requests/lettings_logs_controller_spec.rb @@ -620,7 +620,7 @@ RSpec.describe LettingsLogsController, type: :request do end context "when requesting a specific lettings log" do - let(:completed_lettings_log) { FactoryBot.create(:lettings_log, :completed) } + let!(:completed_lettings_log) { FactoryBot.create(:lettings_log, :completed, owning_organisation: user.organisation, managing_organisation: user.organisation, created_by: user) } let(:id) { completed_lettings_log.id } before do @@ -672,6 +672,26 @@ RSpec.describe LettingsLogsController, type: :request do assert_select ".govuk-tag", text: /Completed/, count: 0 assert_select ".govuk-tag", text: /Cannot start yet/, count: 1 end + + it "displays a link to update the log for currently editable logs" do + completed_lettings_log.update!(startdate: Time.zone.local(2022, 4, 1), tenancylength: nil) + completed_lettings_log.reload + + get "/lettings-logs/#{completed_lettings_log.id}", headers:, params: {} + expect(completed_lettings_log.form.end_date).to eq(Time.zone.local(2023, 7, 1)) + expect(completed_lettings_log.status).to eq("completed") + expect(page).to have_link("review and make changes to this log", href: "/lettings-logs/#{completed_lettings_log.id}/review") + end + + it "displays a closed collection window message for previous collection year logs" do + completed_lettings_log.update!(startdate: Time.zone.local(2021, 4, 1)) + completed_lettings_log.reload + + get "/lettings-logs/#{completed_lettings_log.id}", headers:, params: {} + expect(completed_lettings_log.form.end_date).to eq(Time.zone.local(2022, 7, 1)) + expect(completed_lettings_log.status).to eq("completed") + expect(page).to have_content("This log is from the 2021/2022 collection window, which is now closed.") + end end context "with a lettings log with a single section complete" do @@ -741,6 +761,7 @@ RSpec.describe LettingsLogsController, type: :request do postcode_known: "No") end let(:id) { postcode_lettings_log.id } + let(:completed_lettings_log) { FactoryBot.create(:lettings_log, :completed, owning_organisation: user.organisation, managing_organisation: user.organisation, created_by: user, startdate: Time.zone.local(2021, 4, 1)) } before do stub_request(:get, /api.postcodes.io/) @@ -774,6 +795,21 @@ RSpec.describe LettingsLogsController, type: :request do get "/lettings-logs/#{id}/income-and-benefits/check-answers" expect(CGI.unescape_html(response.body)).to include("You didn’t answer this question") end + + it "does not allow you to change the answers for previous collection year logs" do + get "/lettings-logs/#{completed_lettings_log.id}/setup/check-answers", headers: { "Accept" => "text/html" }, params: {} + expect(page).not_to have_link("Change") + expect(page).not_to have_link("Answer") + + get "/lettings-logs/#{completed_lettings_log.id}/income-and-benefits/check-answers", headers: { "Accept" => "text/html" }, params: {} + expect(page).not_to have_link("Change") + expect(page).not_to have_link("Answer") + end + + it "does not let the user navigate to questions for previous collection year logs" do + get "/lettings-logs/#{completed_lettings_log.id}/needs-type", headers: { "Accept" => "text/html" }, params: {} + expect(response).to redirect_to("/lettings-logs/#{completed_lettings_log.id}") + end end context "when requesting CSV download" do