Browse Source

feat: set org filters to mirror primary sales and lettings

pull/1675/head
natdeanlewissoftwire 3 years ago
parent
commit
5dc5374843
  1. 2
      app/controllers/lettings_logs_controller.rb
  2. 28
      app/controllers/organisations_controller.rb
  3. 2
      app/controllers/sales_logs_controller.rb
  4. 2
      spec/controllers/lettings_logs_controller_spec.rb
  5. 4
      spec/controllers/sales_logs_controller_spec.rb
  6. 38
      spec/helpers/filters_helper_spec.rb

2
app/controllers/lettings_logs_controller.rb

@ -94,7 +94,7 @@ class LettingsLogsController < LogsController
def email_csv def email_csv
all_orgs = params["organisation_select"] == "all" all_orgs = params["organisation_select"] == "all"
EmailCsvJob.perform_later(current_user, search_term, @session_filters, all_orgs, nil, codes_only_export?) EmailCsvJob.perform_later(current_user, search_term, session_filters, all_orgs, nil, codes_only_export?)
redirect_to csv_confirmation_lettings_logs_path redirect_to csv_confirmation_lettings_logs_path
end end

28
app/controllers/organisations_controller.rb

@ -5,7 +5,7 @@ class OrganisationsController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
before_action :find_resource, except: %i[index new create] before_action :find_resource, except: %i[index new create]
before_action :authenticate_scope!, except: [:index] before_action :authenticate_scope!, except: [:index]
before_action -> { session_filters(specific_org: true) }, if: -> { current_user.support? || current_user.organisation.has_managing_agents? }, only: %i[lettings_logs sales_logs email_lettings_csv download_lettings_csv email_sales_csv download_sales_csv] before_action :session_filters, if: -> { current_user.support? || current_user.organisation.has_managing_agents? }, only: %i[lettings_logs sales_logs email_lettings_csv download_lettings_csv email_sales_csv download_sales_csv]
before_action :set_session_filters, if: -> { current_user.support? || current_user.organisation.has_managing_agents? }, only: %i[lettings_logs sales_logs email_lettings_csv download_lettings_csv email_sales_csv download_sales_csv] before_action :set_session_filters, if: -> { current_user.support? || current_user.organisation.has_managing_agents? }, only: %i[lettings_logs sales_logs email_lettings_csv download_lettings_csv email_sales_csv download_sales_csv]
def index def index
@ -90,7 +90,7 @@ class OrganisationsController < ApplicationController
def lettings_logs def lettings_logs
organisation_logs = LettingsLog.visible.where(owning_organisation_id: @organisation.id) organisation_logs = LettingsLog.visible.where(owning_organisation_id: @organisation.id)
unpaginated_filtered_logs = filtered_logs(organisation_logs, search_term, @session_filters) unpaginated_filtered_logs = filter_manager.filtered_logs(organisation_logs, search_term, session_filters)
respond_to do |format| respond_to do |format|
format.html do format.html do
@ -99,6 +99,7 @@ class OrganisationsController < ApplicationController
@searched = search_term.presence @searched = search_term.presence
@total_count = organisation_logs.size @total_count = organisation_logs.size
@log_type = :lettings @log_type = :lettings
@filter_type = "lettings_logs"
render "logs", layout: "application" render "logs", layout: "application"
end end
end end
@ -106,20 +107,20 @@ class OrganisationsController < ApplicationController
def download_lettings_csv def download_lettings_csv
organisation_logs = LettingsLog.visible.where(owning_organisation_id: @organisation.id) organisation_logs = LettingsLog.visible.where(owning_organisation_id: @organisation.id)
unpaginated_filtered_logs = filtered_logs(organisation_logs, search_term, @session_filters) unpaginated_filtered_logs = filter_manager.filtered_logs(organisation_logs, search_term, session_filters)
codes_only = params.require(:codes_only) == "true" codes_only = params.require(:codes_only) == "true"
render "logs/download_csv", locals: { search_term:, count: unpaginated_filtered_logs.size, post_path: lettings_logs_email_csv_organisation_path, codes_only: } render "logs/download_csv", locals: { search_term:, count: unpaginated_filtered_logs.size, post_path: lettings_logs_email_csv_organisation_path, codes_only: }
end end
def email_lettings_csv def email_lettings_csv
EmailCsvJob.perform_later(current_user, search_term, @session_filters, false, @organisation, codes_only_export?) EmailCsvJob.perform_later(current_user, search_term, session_filters, false, @organisation, codes_only_export?)
redirect_to lettings_logs_csv_confirmation_organisation_path redirect_to lettings_logs_csv_confirmation_organisation_path
end end
def sales_logs def sales_logs
organisation_logs = SalesLog.where(owning_organisation_id: @organisation.id) organisation_logs = SalesLog.where(owning_organisation_id: @organisation.id)
unpaginated_filtered_logs = filtered_logs(organisation_logs, search_term, @session_filters) unpaginated_filtered_logs = filter_manager.filtered_logs(organisation_logs, search_term, session_filters)
respond_to do |format| respond_to do |format|
format.html do format.html do
@ -128,6 +129,7 @@ class OrganisationsController < ApplicationController
@searched = search_term.presence @searched = search_term.presence
@total_count = organisation_logs.size @total_count = organisation_logs.size
@log_type = :sales @log_type = :sales
@filter_type = "sales_logs"
render "logs", layout: "application" render "logs", layout: "application"
end end
@ -139,14 +141,14 @@ class OrganisationsController < ApplicationController
def download_sales_csv def download_sales_csv
organisation_logs = SalesLog.visible.where(owning_organisation_id: @organisation.id) organisation_logs = SalesLog.visible.where(owning_organisation_id: @organisation.id)
unpaginated_filtered_logs = filtered_logs(organisation_logs, search_term, @session_filters) unpaginated_filtered_logs = filter_manager.filtered_logs(organisation_logs, search_term, session_filters)
codes_only = params.require(:codes_only) == "true" codes_only = params.require(:codes_only) == "true"
render "logs/download_csv", locals: { search_term:, count: unpaginated_filtered_logs.size, post_path: sales_logs_email_csv_organisation_path, codes_only: } render "logs/download_csv", locals: { search_term:, count: unpaginated_filtered_logs.size, post_path: sales_logs_email_csv_organisation_path, codes_only: }
end end
def email_sales_csv def email_sales_csv
EmailCsvJob.perform_later(current_user, search_term, @session_filters, false, @organisation, codes_only_export?, "sales") EmailCsvJob.perform_later(current_user, search_term, session_filters, false, @organisation, codes_only_export?, "sales")
redirect_to sales_logs_csv_confirmation_organisation_path redirect_to sales_logs_csv_confirmation_organisation_path
end end
@ -156,6 +158,18 @@ class OrganisationsController < ApplicationController
private private
def set_session_filters
filter_manager.serialize_filters_to_session("#{params[:action]}", specific_org: true)
end
def session_filters
filter_manager.session_filters("#{params[:action]}", specific_org: true)
end
def filter_manager
FilterManager.new(current_user:, session:, params:)
end
def org_params def org_params
params.require(:organisation).permit(:name, :address_line1, :address_line2, :postcode, :phone, :holds_own_stock, :provider_type, :housing_registration_no) params.require(:organisation).permit(:name, :address_line1, :address_line2, :postcode, :phone, :holds_own_stock, :provider_type, :housing_registration_no)
end end

2
app/controllers/sales_logs_controller.rb

@ -65,7 +65,7 @@ class SalesLogsController < LogsController
def email_csv def email_csv
all_orgs = params["organisation_select"] == "all" all_orgs = params["organisation_select"] == "all"
EmailCsvJob.perform_later(current_user, search_term, @session_filters, all_orgs, nil, codes_only_export?, "sales") EmailCsvJob.perform_later(current_user, search_term, session_filters, all_orgs, nil, codes_only_export?, "sales")
redirect_to csv_confirmation_sales_logs_path redirect_to csv_confirmation_sales_logs_path
end end

2
spec/controllers/lettings_logs_controller_spec.rb

@ -10,7 +10,7 @@ RSpec.describe LettingsLogsController do
let(:bulk_upload) { create(:bulk_upload, :sales) } let(:bulk_upload) { create(:bulk_upload, :sales) }
it "does not redirect to resume path" do it "does not redirect to resume path" do
session[:logs_filters] = { bulk_upload_id: [bulk_upload.id.to_s] }.to_json session[:lettings_logs_filters] = { bulk_upload_id: [bulk_upload.id.to_s] }.to_json
get :index get :index

4
spec/controllers/sales_logs_controller_spec.rb

@ -10,7 +10,7 @@ RSpec.describe SalesLogsController do
describe "#index" do describe "#index" do
context "when a sales bulk upload has been resolved" do context "when a sales bulk upload has been resolved" do
it "redirects to resume_bulk_upload_sales_result_path" do it "redirects to resume_bulk_upload_sales_result_path" do
session[:logs_filters] = { bulk_upload_id: [bulk_upload.id.to_s] }.to_json session[:sales_logs_filters] = { bulk_upload_id: [bulk_upload.id.to_s] }.to_json
get :index get :index
@ -22,7 +22,7 @@ RSpec.describe SalesLogsController do
let(:bulk_upload) { create(:bulk_upload, :lettings) } let(:bulk_upload) { create(:bulk_upload, :lettings) }
it "does not redirect to resume" do it "does not redirect to resume" do
session[:logs_filters] = { bulk_upload_id: [bulk_upload.id.to_s] }.to_json session[:sales_logs_filters] = { bulk_upload_id: [bulk_upload.id.to_s] }.to_json
get :index get :index

38
spec/helpers/filters_helper_spec.rb

@ -4,80 +4,80 @@ RSpec.describe FiltersHelper do
describe "#filter_selected?" do describe "#filter_selected?" do
context "when no filters are selected" do context "when no filters are selected" do
it "returns false for all filters" do it "returns false for all filters" do
expect(filter_selected?("status", "completed")).to be_falsey expect(filter_selected?("status", "completed", "lettings_logs")).to be_falsey
expect(filter_selected?("status", "in_progress")).to be_falsey expect(filter_selected?("status", "in_progress", "lettings_logs")).to be_falsey
end end
end end
context "when the filter is the user filter but session filters is empty" do context "when the filter is the user filter but session filters is empty" do
before do before do
session[:logs_filters] = {}.to_json session[:lettings_logs_filters] = {}.to_json
end end
context "when looking at the all value" do context "when looking at the all value" do
it "returns true if no filters have been set yet" do it "returns true if no filters have been set yet" do
expect(filter_selected?("user", :all)).to be true expect(filter_selected?("user", :all, "lettings_logs")).to be true
expect(filter_selected?("user", :yours)).to be false expect(filter_selected?("user", :yours, "lettings_logs")).to be false
end end
end end
end end
context "when one filter is selected" do context "when one filter is selected" do
before do before do
session[:logs_filters] = { "status": "in_progress" }.to_json session[:lettings_logs_filters] = { "status": "in_progress" }.to_json
end end
it "returns false for non selected filters" do it "returns false for non selected filters" do
expect(filter_selected?("status", "completed")).to be false expect(filter_selected?("status", "completed", "lettings_logs")).to be false
end end
it "returns true for selected filter" do it "returns true for selected filter" do
expect(filter_selected?("status", "in_progress")).to be true expect(filter_selected?("status", "in_progress", "lettings_logs")).to be true
end end
end end
context "when support user is using the organisation filter" do context "when support user is using the organisation filter" do
before do before do
session[:logs_filters] = { "organisation": "1" }.to_json session[:lettings_logs_filters] = { "organisation": "1" }.to_json
end end
it "returns true for the parent organisation_select filter" do it "returns true for the parent organisation_select filter" do
expect(filter_selected?("organisation_select", :specific_org)).to be true expect(filter_selected?("organisation_select", :specific_org, "lettings_logs")).to be true
expect(filter_selected?("organisation_select", :all)).to be false expect(filter_selected?("organisation_select", :all, "lettings_logs")).to be false
end end
end end
context "when support user has not set the organisation_select filter" do context "when support user has not set the organisation_select filter" do
before do before do
session[:logs_filters] = {}.to_json session[:lettings_logs_filters] = {}.to_json
end end
it "defaults to all organisations" do it "defaults to all organisations" do
expect(filter_selected?("organisation_select", :all)).to be true expect(filter_selected?("organisation_select", :all, "lettings_logs")).to be true
expect(filter_selected?("organisation_select", :specific_org)).to be false expect(filter_selected?("organisation_select", :specific_org, "lettings_logs")).to be false
end end
end end
context "when the specific organisation filter is not set" do context "when the specific organisation filter is not set" do
before do before do
session[:logs_filters] = { "status" => [""], "years" => [""], "user" => "all" }.to_json session[:lettings_logs_filters] = { "status" => [""], "years" => [""], "user" => "all" }.to_json
end end
it "marks the all options as checked" do it "marks the all options as checked" do
expect(filter_selected?("organisation_select", :all)).to be true expect(filter_selected?("organisation_select", :all, "lettings_logs")).to be true
expect(filter_selected?("organisation_select", :specific_org)).to be false expect(filter_selected?("organisation_select", :specific_org, "lettings_logs")).to be false
end end
end end
end end
describe "#selected_option" do describe "#selected_option" do
before do before do
session[:logs_filters] = {}.to_json session[:lettings_logs_filters] = {}.to_json
end end
context "when nothing has been selected" do context "when nothing has been selected" do
it "returns an empty string" do it "returns an empty string" do
expect(selected_option("organisation")).to eq("") expect(selected_option("organisation", "lettings_logs")).to eq("")
end end
end end
end end

Loading…
Cancel
Save