From 3cd3504224b3a403a98d1167aa2587381feef117 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 22 Jun 2023 15:16:21 +0100 Subject: [PATCH] Redirect lettings log to review page if the collection year is closed for editing --- app/controllers/lettings_logs_controller.rb | 3 + app/helpers/review_helper.rb | 12 ++++ app/views/form/review.html.erb | 2 +- .../requests/lettings_logs_controller_spec.rb | 66 +++++++++++++++++-- 4 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 app/helpers/review_helper.rb diff --git a/app/controllers/lettings_logs_controller.rb b/app/controllers/lettings_logs_controller.rb index f67e94acc..f5267cb80 100644 --- a/app/controllers/lettings_logs_controller.rb +++ b/app/controllers/lettings_logs_controller.rb @@ -1,4 +1,5 @@ class LettingsLogsController < LogsController + include CollectionTimeHelper rescue_from ActiveRecord::RecordNotFound, with: :render_not_found before_action :find_resource, only: %i[update show] @@ -63,6 +64,8 @@ class LettingsLogsController < LogsController if @log.unresolved redirect_to(send(@log.form.unresolved_log_path, @log)) + elsif @log.form.edit_end_date < Time.zone.now || (@log.startdate.present? && @log.startdate < previous_collection_start_date) + redirect_to review_lettings_log_path(@log) else render("logs/edit", locals: { current_user: }) end diff --git a/app/helpers/review_helper.rb b/app/helpers/review_helper.rb new file mode 100644 index 000000000..c8918708e --- /dev/null +++ b/app/helpers/review_helper.rb @@ -0,0 +1,12 @@ +module ReviewHelper + include CollectionTimeHelper + + def review_log_info_text(log) + if log.collection_period_open? + "You can review and make changes to this log until #{log.form.submission_deadline.to_formatted_s(:govuk_date)}.".html_safe + else + start_year = log.startdate ? collection_start_year_for_date(log.startdate) : log.form.start_date.year + "This log is from the #{start_year}/#{start_year + 1} collection window, which is now closed." + end + end +end diff --git a/app/views/form/review.html.erb b/app/views/form/review.html.erb index f3b6a2f30..17bc1fbcc 100644 --- a/app/views/form/review.html.erb +++ b/app/views/form/review.html.erb @@ -12,7 +12,7 @@ <%= content_for(:title) %>

- You can review and make changes to this log until <%= @log.form.submission_deadline.to_formatted_s(:govuk_date) %>. + <%= review_log_info_text(@log) %>

<% @log.form.sections.map do |section| %>

<%= section.label %>

diff --git a/spec/requests/lettings_logs_controller_spec.rb b/spec/requests/lettings_logs_controller_spec.rb index 61664e81d..1f02d019b 100644 --- a/spec/requests/lettings_logs_controller_spec.rb +++ b/spec/requests/lettings_logs_controller_spec.rb @@ -908,15 +908,21 @@ RSpec.describe LettingsLogsController, type: :request do end context "with a signed in user" do + before do + Timecop.freeze(2021, 4, 1) + Singleton.__init__(FormHandler) + completed_lettings_log.update!(startdate: Time.zone.local(2021, 4, 1), voiddate: Time.zone.local(2021, 4, 1), mrcdate: Time.zone.local(2021, 4, 1)) + completed_lettings_log.reload + end + context "with lettings logs that are owned or managed by your organisation" do before do sign_in user get "/lettings-logs/#{lettings_log.id}", headers:, params: {} - Timecop.freeze(2021, 4, 1) - Singleton.__init__(FormHandler) - completed_lettings_log.update!(startdate: Time.zone.local(2021, 4, 1), voiddate: Time.zone.local(2021, 4, 1), mrcdate: Time.zone.local(2021, 4, 1)) - completed_lettings_log.reload - Timecop.unfreeze + end + + after do + Timecop.return Singleton.__init__(FormHandler) end @@ -933,7 +939,7 @@ RSpec.describe LettingsLogsController, type: :request do 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.update!(startdate: Time.zone.local(2021, 4, 1), tenancylength: nil) completed_lettings_log.reload get "/lettings-logs/#{completed_lettings_log.id}", headers:, params: {} @@ -941,11 +947,43 @@ RSpec.describe LettingsLogsController, type: :request do 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 + end - xit "displays a closed collection window message for previous collection year logs" do + context "with lettings logs from a closed collection period before the previous collection" do + before do + sign_in user + Timecop.return + Singleton.__init__(FormHandler) + get "/lettings-logs/#{completed_lettings_log.id}", headers:, params: {} + end + + it "redirects to review page" do + expect(response).to redirect_to("/lettings-logs/#{completed_lettings_log.id}/review") + end + end + + context "with lettings logs from a closed previous collection period" do + before do + sign_in user + Timecop.freeze(2023, 2, 1) + Singleton.__init__(FormHandler) + get "/lettings-logs/#{completed_lettings_log.id}", headers:, params: {} + end + + after do + Timecop.return + Singleton.__init__(FormHandler) + end + + it "redirects to review page" do + expect(response).to redirect_to("/lettings-logs/#{completed_lettings_log.id}/review") + end + + it "displays a closed collection window message for previous collection year logs" do get "/lettings-logs/#{completed_lettings_log.id}", headers:, params: {} expect(completed_lettings_log.form.new_logs_end_date).to eq(Time.zone.local(2022, 7, 1)) expect(completed_lettings_log.status).to eq("completed") + follow_redirect! expect(page).to have_content("This log is from the 2021/2022 collection window, which is now closed.") end end @@ -992,10 +1030,17 @@ RSpec.describe LettingsLogsController, type: :request do end before do + Timecop.freeze(2021, 4, 1) + Singleton.__init__(FormHandler) sign_in user get "/lettings-logs/#{section_completed_lettings_log.id}", headers:, params: {} end + after do + Timecop.unfreeze + Singleton.__init__(FormHandler) + end + it "displays a section status for a lettings log" do assert_select ".govuk-tag", text: /Not started/, count: 6 assert_select ".govuk-tag", text: /Completed/, count: 1 @@ -1019,12 +1064,19 @@ RSpec.describe LettingsLogsController, type: :request do let!(:location) { FactoryBot.create(:location, scheme:) } before do + Timecop.freeze(2021, 4, 1) + Singleton.__init__(FormHandler) FactoryBot.create_list(:lettings_log, 3, unresolved: true, created_by: user) lettings_log.update!(needstype: 2, scheme:, location:, unresolved: true) sign_in user get "/lettings-logs/#{lettings_log.id}", headers:, params: {} end + after do + Timecop.return + Singleton.__init__(FormHandler) + end + it "marks it as resolved when both scheme and location exist" do lettings_log.reload expect(lettings_log.unresolved).to eq(false)