diff --git a/app/controllers/lettings_logs_filters_controller.rb b/app/controllers/lettings_logs_filters_controller.rb index e223ad93d..9180737d3 100644 --- a/app/controllers/lettings_logs_filters_controller.rb +++ b/app/controllers/lettings_logs_filters_controller.rb @@ -9,6 +9,13 @@ class LettingsLogsFiltersController < ApplicationController @filter = filter render "filters/#{filter}" end + + define_method("organisation_#{filter}") do + @organisation_id = params["id"] + @filter_type = "lettings_logs" + @filter = filter + render "filters/#{filter}" + end end %w[status needstype assigned_to owned_by managed_by].each do |filter| @@ -17,6 +24,12 @@ class LettingsLogsFiltersController < ApplicationController redirect_to csv_download_lettings_logs_path(search: params["search"], codes_only: params["codes_only"]) end + + define_method("update_organisation_#{filter}") do + @organisation_id = params["id"] + @filter_type = "lettings_logs" + redirect_to lettings_logs_csv_download_organisation_path(params["id"], search: params["search"], codes_only: params["codes_only"]) + end end def update_years @@ -27,6 +40,16 @@ class LettingsLogsFiltersController < ApplicationController redirect_to csv_download_lettings_logs_path(search: params["search"], codes_only: params["codes_only"]) end end + + def update_organisation_years + @filter_type = "lettings_logs" + @organisation_id = params["id"] + if params["years"].nil? + redirect_to lettings_logs_filters_years_organisation_path(search: params["search"], codes_only: params["codes_only"], error: "Please select a year") + else + redirect_to lettings_logs_csv_download_organisation_path(params["id"], search: params["search"], codes_only: params["codes_only"]) + end + end end private diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 7dc4300f9..4c937c02b 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -148,6 +148,8 @@ class OrganisationsController < ApplicationController end def download_lettings_csv + redirect_to lettings_logs_filters_years_organisation_path(search: search_term, codes_only: codes_only_export?) and return if session_filters["years"].blank? || session_filters["years"].count != 1 + organisation_logs = LettingsLog.visible.where(owning_organisation_id: @organisation.id) unpaginated_filtered_logs = filter_manager.filtered_logs(organisation_logs, search_term, session_filters) codes_only = params.require(:codes_only) == "true" diff --git a/app/controllers/sales_logs_filters_controller.rb b/app/controllers/sales_logs_filters_controller.rb index bebd362e0..b58b9c585 100644 --- a/app/controllers/sales_logs_filters_controller.rb +++ b/app/controllers/sales_logs_filters_controller.rb @@ -9,6 +9,13 @@ class SalesLogsFiltersController < ApplicationController @filter = filter render "filters/#{filter}" end + + define_method("organisation_#{filter}") do + @filter_type = "sales_logs" + @organisation_id = params["id"] + @filter = filter + render "filters/#{filter}" + end end %w[status assigned_to owned_by managed_by].each do |filter| @@ -17,6 +24,13 @@ class SalesLogsFiltersController < ApplicationController redirect_to csv_download_sales_logs_path(search: params["search"], codes_only: params["codes_only"]) end + + define_method("update_organisation_#{filter}") do + @organisation_id = params["id"] + @filter_type = "sales_logs" + + redirect_to sales_logs_csv_download_organisation_path(params["id"], search: params["search"], codes_only: params["codes_only"]) + end end def update_years @@ -27,6 +41,16 @@ class SalesLogsFiltersController < ApplicationController redirect_to csv_download_sales_logs_path(search: params["search"], codes_only: params["codes_only"]) end end + + def update_organisation_years + @filter_type = "sales_logs" + @organisation_id = params["id"] + if params["years"].nil? + redirect_to sales_logs_filters_years_organisation_path(search: params["search"], codes_only: params["codes_only"], error: "Please select a year") + else + redirect_to sales_logs_csv_download_organisation_path(params["id"], search: params["search"], codes_only: params["codes_only"]) + end + end end private diff --git a/app/helpers/filters_helper.rb b/app/helpers/filters_helper.rb index 5ee36e5a9..9f56a8747 100644 --- a/app/helpers/filters_helper.rb +++ b/app/helpers/filters_helper.rb @@ -175,25 +175,41 @@ module FiltersHelper def check_your_answers_lettings_filters_list(session_filters) [ - { label: "Collection year", value: formatted_years_filter(session_filters), path: filters_years_lettings_logs_path }, - { label: "Status", value: formatted_status_filter(session_filters), path: filters_status_lettings_logs_path }, - { label: "Needs type", value: formatted_needstype_filter(session_filters), path: filters_needstype_lettings_logs_path }, - { label: "Assigned to", value: formatted_assigned_to_filter(session_filters), path: filters_assigned_to_lettings_logs_path }, - { label: "Owned by", value: formatted_owned_by_filter(session_filters), path: filters_owned_by_lettings_logs_path }, - { label: "Managed by", value: formatted_managed_by_filter(session_filters), path: filters_managed_by_lettings_logs_path }, + { id: "years", label: "Collection year", value: formatted_years_filter(session_filters) }, + { id: "status", label: "Status", value: formatted_status_filter(session_filters) }, + { id: "needstype", label: "Needs type", value: formatted_needstype_filter(session_filters) }, + { id: "assigned_to", label: "Assigned to", value: formatted_assigned_to_filter(session_filters) }, + { id: "owned_by", label: "Owned by", value: formatted_owned_by_filter(session_filters) }, + { id: "managed_by", label: "Managed by", value: formatted_managed_by_filter(session_filters) }, ] end def check_your_answers_sales_filters_list(session_filters) [ - { label: "Collection year", value: formatted_years_filter(session_filters), path: filters_years_sales_logs_path }, - { label: "Status", value: formatted_status_filter(session_filters), path: filters_status_sales_logs_path }, - { label: "Assigned to", value: formatted_assigned_to_filter(session_filters), path: filters_assigned_to_sales_logs_path }, - { label: "Owned by", value: formatted_owned_by_filter(session_filters), path: filters_owned_by_sales_logs_path }, - { label: "Managed by", value: formatted_managed_by_filter(session_filters), path: filters_managed_by_sales_logs_path }, + { id: "years", label: "Collection year", value: formatted_years_filter(session_filters) }, + { id: "status", label: "Status", value: formatted_status_filter(session_filters) }, + { id: "assigned_to", label: "Assigned to", value: formatted_assigned_to_filter(session_filters) }, + { id: "owned_by", label: "Owned by", value: formatted_owned_by_filter(session_filters) }, + { id: "managed_by", label: "Managed by", value: formatted_managed_by_filter(session_filters) }, ] end + def update_csv_filters_url(filter_type, filter, organisation_id) + if organisation_id.present? + send("#{filter_type}_filters_update_#{filter}_organisation_path", organisation_id) + else + send("filters_update_#{filter}_#{filter_type}_path") + end + end + + def change_filter_for_csv_url(filter, filter_type, search_term, codes_only, organisation_id) + if organisation_id.present? + send("#{filter_type}_filters_#{filter[:id]}_organisation_path", organisation_id, search: search_term, codes_only:) + else + send("filters_#{filter[:id]}_#{filter_type}_path", search: search_term, codes_only:) + end + end + private def applied_filters(filter_type) @@ -250,9 +266,10 @@ private end def formatted_owned_by_filter(session_filters) - return "All" if session_filters["owning_organisation"].blank? || session_filters["owning_organisation"]&.include?("all") + return "All" if params["id"].blank? && (session_filters["owning_organisation"].blank? || session_filters["owning_organisation"]&.include?("all")) - Organisation.find(session_filters["owning_organisation"].first)&.name + session_org_id = session_filters["owning_organisation"].is_a?(Array) ? session_filters["owning_organisation"].first : session_filters["owning_organisation"] + Organisation.find(session_org_id || params["id"])&.name end def formatted_managed_by_filter(session_filters) diff --git a/app/views/filters/assigned_to.html.erb b/app/views/filters/assigned_to.html.erb index 35b91cc6b..8af513587 100644 --- a/app/views/filters/assigned_to.html.erb +++ b/app/views/filters/assigned_to.html.erb @@ -1,4 +1,4 @@ -<%= form_with html: { method: :get }, url: send("filters_update_#{@filter}_#{@filter_type}_path") do |f| %> +<%= form_with html: { method: :get }, url: update_csv_filters_url(@filter_type, @filter, @organisation_id) do |f| %> <%= render partial: "filters/radio_filter", locals: { f:, diff --git a/app/views/filters/managed_by.html.erb b/app/views/filters/managed_by.html.erb index b5fbe8272..9675656ba 100644 --- a/app/views/filters/managed_by.html.erb +++ b/app/views/filters/managed_by.html.erb @@ -1,4 +1,4 @@ -<%= form_with html: { method: :get }, url: send("filters_update_#{@filter}_#{@filter_type}_path") do |f| %> +<%= form_with html: { method: :get }, url: update_csv_filters_url(@filter_type, @filter, @organisation_id) do |f| %> <%= render partial: "filters/radio_filter", locals: { f:, options: { diff --git a/app/views/filters/needstype.html.erb b/app/views/filters/needstype.html.erb index fe098f75b..c761acb51 100644 --- a/app/views/filters/needstype.html.erb +++ b/app/views/filters/needstype.html.erb @@ -1,4 +1,4 @@ -<%= form_with html: { method: :get }, url: send("filters_update_#{@filter}_#{@filter_type}_path") do |f| %> +<%= form_with html: { method: :get }, url: update_csv_filters_url(@filter_type, @filter, @organisation_id) do |f| %> <%= render partial: "filters/checkbox_filter", locals: { f:, diff --git a/app/views/filters/owned_by.html.erb b/app/views/filters/owned_by.html.erb index c18bd9e60..f11b15204 100644 --- a/app/views/filters/owned_by.html.erb +++ b/app/views/filters/owned_by.html.erb @@ -1,4 +1,4 @@ -<%= form_with html: { method: :get }, url: send("filters_update_#{@filter}_#{@filter_type}_path") do |f| %> +<%= form_with html: { method: :get }, url: update_csv_filters_url(@filter_type, @filter, @organisation_id) do |f| %> <%= render partial: "filters/radio_filter", locals: { f:, options: { diff --git a/app/views/filters/status.html.erb b/app/views/filters/status.html.erb index bb26363ed..a6664056f 100644 --- a/app/views/filters/status.html.erb +++ b/app/views/filters/status.html.erb @@ -1,4 +1,4 @@ -<%= form_with html: { method: :get }, url: send("filters_update_#{@filter}_#{@filter_type}_path") do |f| %> +<%= form_with html: { method: :get }, url: update_csv_filters_url(@filter_type, @filter, @organisation_id) do |f| %> <%= render partial: "filters/checkbox_filter", locals: { f:, diff --git a/app/views/filters/years.html.erb b/app/views/filters/years.html.erb index dc13c16bf..4e4c31816 100644 --- a/app/views/filters/years.html.erb +++ b/app/views/filters/years.html.erb @@ -1,4 +1,4 @@ -<%= form_with html: { method: :get }, url: send("filters_update_#{@filter}_#{@filter_type}_path") do |f| %> +<%= form_with html: { method: :get }, url: update_csv_filters_url(@filter_type, @filter, @organisation_id) do |f| %> <% if params["error"].present? %>
diff --git a/app/views/logs/download_csv.html.erb b/app/views/logs/download_csv.html.erb index 43efcdac4..cf1a37a1d 100644 --- a/app/views/logs/download_csv.html.erb +++ b/app/views/logs/download_csv.html.erb @@ -6,6 +6,9 @@
+ <% if @organisation.present? %> + <%= @organisation.name %> + <% end %>

Download CSV

We'll send a secure download link to your email address <%= @current_user.email %>.

@@ -35,7 +38,7 @@ <% row.with_action( text: "Change", - href: "#{filter[:path]}?search=#{search_term}&codes_only=#{codes_only}", + href: change_filter_for_csv_url(filter, filter_type, search_term, codes_only, params["id"]), ) %> <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index fb2769b8d..1ce7095e7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -181,6 +181,14 @@ Rails.application.routes.draw do get "merge-request", to: "organisations#merge_request" get "deactivate", to: "organisations#deactivate" get "reactivate", to: "organisations#reactivate" + %w[years status needstype assigned-to owned-by managed-by].each do |filter| + get "lettings-logs/filters/#{filter}", to: "lettings_logs_filters#organisation_#{filter.underscore}" + get "lettings-logs/filters/update-#{filter}", to: "lettings_logs_filters#update_organisation_#{filter.underscore}" + end + %w[years status assigned-to owned-by managed-by].each do |filter| + get "sales-logs/filters/#{filter}", to: "sales_logs_filters#organisation_#{filter.underscore}" + get "sales-logs/filters/update-#{filter}", to: "sales_logs_filters#update_organisation_#{filter.underscore}" + end end end diff --git a/spec/features/lettings_log_spec.rb b/spec/features/lettings_log_spec.rb index f4611cc35..d462810a3 100644 --- a/spec/features/lettings_log_spec.rb +++ b/spec/features/lettings_log_spec.rb @@ -177,8 +177,8 @@ RSpec.describe "Lettings Log Features" do it "allows updating filters" do click_link("Download (CSV, codes only)") expect(page).to have_content("You've selected 2 logs") - click_link("Change", href: "/lettings-logs/filters/needstype?search=1&codes_only=true") - expect(page).to have_current_path("/lettings-logs/filters/needstype?search=1&codes_only=true") + click_link("Change", href: "/lettings-logs/filters/needstype?codes_only=true&search=1") + expect(page).to have_current_path("/lettings-logs/filters/needstype?codes_only=true&search=1") check("needstypes-1-field", allow_label_click: true) click_button("Save changes") @@ -186,7 +186,7 @@ RSpec.describe "Lettings Log Features" do expect(page).to have_current_path("/lettings-logs/csv-download?codes_only=true&search=1") expect(page).to have_content("You've selected 1 logs") - click_link("Change", href: "/lettings-logs/filters/status?search=1&codes_only=true") + click_link("Change", href: "/lettings-logs/filters/status?codes_only=true&search=1") check("status-not-started-field", allow_label_click: true) click_button("Save changes") @@ -196,7 +196,7 @@ RSpec.describe "Lettings Log Features" do it "routes back to the filters CYA when cancel is pressed" do click_link("Download (CSV)") - click_link("Change", href: "/lettings-logs/filters/needstype?search=1&codes_only=false") + click_link("Change", href: "/lettings-logs/filters/needstype?codes_only=false&search=1") click_link(text: "Cancel") expect(page).to have_current_path("/lettings-logs/csv-download?codes_only=false&search=1") diff --git a/spec/features/sales_log_spec.rb b/spec/features/sales_log_spec.rb index 1409982a8..7e8d5d847 100644 --- a/spec/features/sales_log_spec.rb +++ b/spec/features/sales_log_spec.rb @@ -205,14 +205,14 @@ RSpec.describe "Sales Log Features" do it "allows updating filters" do click_link("Download (CSV, codes only)") expect(page).to have_content("You've selected 2 logs") - click_link("Change", href: "/sales-logs/filters/assigned-to?search=1&codes_only=true") + click_link("Change", href: "/sales-logs/filters/assigned-to?codes_only=true&search=1") choose("assigned-to-you-field", allow_label_click: true) click_button("Save changes") expect(page).to have_content("You've selected 1 logs") - click_link("Change", href: "/sales-logs/filters/status?search=1&codes_only=true") + click_link("Change", href: "/sales-logs/filters/status?codes_only=true&search=1") check("status-not-started-field", allow_label_click: true) click_button("Save changes") @@ -222,7 +222,7 @@ RSpec.describe "Sales Log Features" do it "routes back to the filters CYA when cancel is pressed" do click_link("Download (CSV)") - click_link("Change", href: "/sales-logs/filters/assigned-to?search=1&codes_only=false") + click_link("Change", href: "/sales-logs/filters/assigned-to?codes_only=false&search=1") click_link(text: "Cancel") expect(page).to have_current_path("/sales-logs/csv-download?codes_only=false&search=1") diff --git a/spec/requests/lettings_logs_controller_spec.rb b/spec/requests/lettings_logs_controller_spec.rb index 96d39ab22..d30ae4c80 100644 --- a/spec/requests/lettings_logs_controller_spec.rb +++ b/spec/requests/lettings_logs_controller_spec.rb @@ -1426,12 +1426,12 @@ RSpec.describe LettingsLogsController, type: :request do it "allows updating log filters" do expect(page).to have_content("Check your filters") expect(page).to have_link("Change", count: 6) - expect(page).to have_link("Change", href: "/lettings-logs/filters/years?search=#{search_term}&codes_only=false") - expect(page).to have_link("Change", href: "/lettings-logs/filters/assigned-to?search=#{search_term}&codes_only=false") - expect(page).to have_link("Change", href: "/lettings-logs/filters/owned-by?search=#{search_term}&codes_only=false") - expect(page).to have_link("Change", href: "/lettings-logs/filters/managed-by?search=#{search_term}&codes_only=false") - expect(page).to have_link("Change", href: "/lettings-logs/filters/status?search=#{search_term}&codes_only=false") - expect(page).to have_link("Change", href: "/lettings-logs/filters/needstype?search=#{search_term}&codes_only=false") + expect(page).to have_link("Change", href: "/lettings-logs/filters/years?codes_only=false&search=#{search_term}") + expect(page).to have_link("Change", href: "/lettings-logs/filters/assigned-to?codes_only=false&search=#{search_term}") + expect(page).to have_link("Change", href: "/lettings-logs/filters/owned-by?codes_only=false&search=#{search_term}") + expect(page).to have_link("Change", href: "/lettings-logs/filters/managed-by?codes_only=false&search=#{search_term}") + expect(page).to have_link("Change", href: "/lettings-logs/filters/status?codes_only=false&search=#{search_term}") + expect(page).to have_link("Change", href: "/lettings-logs/filters/needstype?codes_only=false&search=#{search_term}") end end diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index 152b5662e..de6b618b5 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -1589,7 +1589,7 @@ RSpec.describe OrganisationsController, type: :request do let(:tenancycode) { "42" } before do - create(:lettings_log, owning_organisation: organisation, tenancycode:) + create(:lettings_log, :in_progress, owning_organisation: organisation, tenancycode:) end context "when there is at least one log visible" do @@ -1625,16 +1625,24 @@ RSpec.describe OrganisationsController, type: :request do context "when you download the CSV" do let(:other_organisation) { create(:organisation) } + let!(:lettings_logs) { create_list(:lettings_log, 2, :in_progress, owning_organisation: organisation) } before do - create_list(:lettings_log, 2, owning_organisation: organisation) - create(:lettings_log, owning_organisation: organisation, status: "pending", skip_update_status: true) - create_list(:lettings_log, 2, owning_organisation: other_organisation) + create(:lettings_log, :in_progress, owning_organisation: organisation, status: "pending", skip_update_status: true) + create_list(:lettings_log, 2, :in_progress, owning_organisation: other_organisation) end - it "only includes logs from that organisation" do - get "/organisations/#{organisation.id}/lettings-logs/csv-download?codes_only=false" + context "when no year filters are applied" do + it "redirects to years filter page" do + get "/organisations/#{organisation.id}/lettings-logs/csv-download?codes_only=false" + expect(response).to redirect_to("/organisations/#{organisation.id}/lettings-logs/filters/years?codes_only=false") + follow_redirect! + expect(page).to have_button("Save changes") + end + end + it "only includes logs from that organisation" do + get "/organisations/#{organisation.id}/lettings-logs/csv-download?years[]=#{lettings_logs[0].form.start_date.year}&codes_only=false" expect(page).to have_text("You've selected 3 logs.") end diff --git a/spec/requests/sales_logs_controller_spec.rb b/spec/requests/sales_logs_controller_spec.rb index e9652228b..af7ba4b5a 100644 --- a/spec/requests/sales_logs_controller_spec.rb +++ b/spec/requests/sales_logs_controller_spec.rb @@ -986,11 +986,11 @@ RSpec.describe SalesLogsController, type: :request do it "allows updating log filters" do expect(page).to have_content("Check your filters") expect(page).to have_link("Change", count: 5) - expect(page).to have_link("Change", href: "/sales-logs/filters/years?search=#{search_term}&codes_only=false") - expect(page).to have_link("Change", href: "/sales-logs/filters/assigned-to?search=#{search_term}&codes_only=false") - expect(page).to have_link("Change", href: "/sales-logs/filters/owned-by?search=#{search_term}&codes_only=false") - expect(page).to have_link("Change", href: "/sales-logs/filters/managed-by?search=#{search_term}&codes_only=false") - expect(page).to have_link("Change", href: "/sales-logs/filters/status?search=#{search_term}&codes_only=false") + expect(page).to have_link("Change", href: "/sales-logs/filters/years?codes_only=false&search=#{search_term}") + expect(page).to have_link("Change", href: "/sales-logs/filters/assigned-to?codes_only=false&search=#{search_term}") + expect(page).to have_link("Change", href: "/sales-logs/filters/owned-by?codes_only=false&search=#{search_term}") + expect(page).to have_link("Change", href: "/sales-logs/filters/managed-by?codes_only=false&search=#{search_term}") + expect(page).to have_link("Change", href: "/sales-logs/filters/status?codes_only=false&search=#{search_term}") end it "has a hidden field with the search term" do