diff --git a/app/controllers/sales_logs_controller.rb b/app/controllers/sales_logs_controller.rb index 5e9777a3c..ecaa7e01a 100644 --- a/app/controllers/sales_logs_controller.rb +++ b/app/controllers/sales_logs_controller.rb @@ -3,6 +3,9 @@ class SalesLogsController < LogsController before_action :set_session_filters, if: :current_user, only: %i[index email_csv download_csv] before_action :authenticate_scope!, only: %i[download_csv email_csv] + before_action :extract_bulk_upload_from_session_filters, only: [:index] + before_action :redirect_if_bulk_upload_resolved, only: [:index] + def create super { SalesLog.new(log_params) } end @@ -61,6 +64,17 @@ class SalesLogsController < LogsController private + def extract_bulk_upload_from_session_filters + filter_service = FilterService.new(current_user:, session:) + @bulk_upload = filter_service.bulk_upload + end + + def redirect_if_bulk_upload_resolved + if @bulk_upload && @bulk_upload.sales? && @bulk_upload.sales_logs.in_progress.count.zero? + redirect_to resume_bulk_upload_sales_result_path(@bulk_upload) + end + end + def authenticate_scope! head :unauthorized and return if codes_only_export? && !current_user.support? end diff --git a/spec/controllers/sales_logs_controller_spec.rb b/spec/controllers/sales_logs_controller_spec.rb new file mode 100644 index 000000000..e8d85cb85 --- /dev/null +++ b/spec/controllers/sales_logs_controller_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe SalesLogsController do + let(:bulk_upload) { create(:bulk_upload, :sales) } + + before do + sign_in bulk_upload.user + end + + describe "#index" do + context "when a sales bulk upload has been resolved" do + it "redirects to resume_bulk_upload_sales_result_path" do + session[:logs_filters] = { bulk_upload_id: [bulk_upload.id.to_s] }.to_json + + get :index + + expect(response).to redirect_to("/sales-logs/bulk-upload-results/#{bulk_upload.id}/resume") + end + end + + context "when a resolved lettings bulk upload filter applied" do + let(:bulk_upload) { create(:bulk_upload, :lettings) } + + it "does not redirect to resume" do + session[:logs_filters] = { bulk_upload_id: [bulk_upload.id.to_s] }.to_json + + get :index + + expect(response).not_to redirect_to("/sales-logs/bulk-upload-results/#{bulk_upload.id}/resume") + end + end + end +end