Browse Source

Redirect sales log to review page if the collection year is closed for editing

pull/1721/head
Kat 2 years ago
parent
commit
47f72b4efb
  1. 7
      app/controllers/sales_logs_controller.rb
  2. 78
      spec/requests/sales_logs_controller_spec.rb

7
app/controllers/sales_logs_controller.rb

@ -1,5 +1,6 @@
class SalesLogsController < LogsController class SalesLogsController < LogsController
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found rescue_from ActiveRecord::RecordNotFound, with: :render_not_found
include CollectionTimeHelper
before_action :session_filters, if: :current_user, only: %i[index email_csv download_csv] before_action :session_filters, if: :current_user, only: %i[index email_csv download_csv]
before_action -> { filter_manager.serialize_filters_to_session }, if: :current_user, only: %i[index email_csv download_csv] before_action -> { filter_manager.serialize_filters_to_session }, if: :current_user, only: %i[index email_csv download_csv]
@ -37,7 +38,11 @@ class SalesLogsController < LogsController
def edit def edit
@log = current_user.sales_logs.visible.find(params[:id]) @log = current_user.sales_logs.visible.find(params[:id])
render "logs/edit", locals: { current_user: } if @log.form.edit_end_date < Time.zone.now || (@log.saledate.present? && @log.saledate < previous_collection_start_date)
redirect_to review_sales_log_path(@log, sales_log: true)
else
render "logs/edit", locals: { current_user: }
end
end end
def destroy def destroy

78
spec/requests/sales_logs_controller_spec.rb

@ -556,6 +556,84 @@ RSpec.describe SalesLogsController, type: :request do
end end
end end
context "when viewing a sales log" do
let(:headers) { { "Accept" => "text/html" } }
let(:completed_sales_log) { FactoryBot.create(:sales_log, :completed, owning_organisation: user.organisation, created_by: user) }
before do
sign_in user
Timecop.freeze(2021, 4, 1)
Singleton.__init__(FormHandler)
completed_sales_log.update!(saledate: Time.zone.local(2021, 4, 1))
completed_sales_log.reload
end
context "with sales logs that are owned by your organisation" do
before do
get "/sales-logs/#{completed_sales_log.id}", headers:, params: {}
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
it "shows the tasklist for sales logs you have access to" do
expect(response.body).to match("Log")
expect(response.body).to match(completed_sales_log.id.to_s)
end
it "displays a link to update the log for currently editable logs" do
completed_sales_log.update!(saledate: Time.zone.local(2021, 4, 1))
completed_sales_log.reload
get "/sales-logs/#{completed_sales_log.id}", headers:, params: {}
expect(completed_sales_log.form.new_logs_end_date).to eq(Time.zone.local(2022, 8, 7))
expect(completed_sales_log.status).to eq("completed")
expect(page).to have_link("review and make changes to this log", href: "/sales-logs/#{completed_sales_log.id}/review?sales_log=true")
end
end
context "with sales logs from a closed collection period before the previous collection" do
before do
sign_in user
Timecop.return
Singleton.__init__(FormHandler)
get "/sales-logs/#{completed_sales_log.id}", headers:, params: {}
end
it "redirects to review page" do
expect(response).to redirect_to("/sales-logs/#{completed_sales_log.id}/review?sales_log=true")
end
end
context "with sales logs from a closed previous collection period" do
before do
sign_in user
Timecop.freeze(2023, 2, 1)
Singleton.__init__(FormHandler)
get "/sales-logs/#{completed_sales_log.id}", headers:, params: {}
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
it "redirects to review page" do
expect(response).to redirect_to("/sales-logs/#{completed_sales_log.id}/review?sales_log=true")
end
it "displays a closed collection window message for previous collection year logs" do
get "/sales-logs/#{completed_sales_log.id}", headers:, params: {}
expect(completed_sales_log.form.new_logs_end_date).to eq(Time.zone.local(2022, 8, 7))
expect(completed_sales_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
end
context "when requesting CSV download" do context "when requesting CSV download" do
let(:headers) { { "Accept" => "text/html" } } let(:headers) { { "Accept" => "text/html" } }
let(:search_term) { "foot" } let(:search_term) { "foot" }

Loading…
Cancel
Save