Browse Source

extract bulk upload filter from session

bu-sales-23-v3
Phil Lee 2 years ago
parent
commit
cea1bf575a
  1. 14
      app/controllers/sales_logs_controller.rb
  2. 33
      spec/controllers/sales_logs_controller_spec.rb

14
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

33
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
Loading…
Cancel
Save