Browse Source

Update filtering per organisation

pull/2403/head
Kat 2 years ago committed by kosiakkatrina
parent
commit
361bd2faa7
  1. 23
      app/controllers/lettings_logs_filters_controller.rb
  2. 2
      app/controllers/organisations_controller.rb
  3. 24
      app/controllers/sales_logs_filters_controller.rb
  4. 43
      app/helpers/filters_helper.rb
  5. 2
      app/views/filters/assigned_to.html.erb
  6. 2
      app/views/filters/managed_by.html.erb
  7. 2
      app/views/filters/needstype.html.erb
  8. 2
      app/views/filters/owned_by.html.erb
  9. 2
      app/views/filters/status.html.erb
  10. 2
      app/views/filters/years.html.erb
  11. 5
      app/views/logs/download_csv.html.erb
  12. 8
      config/routes.rb
  13. 8
      spec/features/lettings_log_spec.rb
  14. 6
      spec/features/sales_log_spec.rb
  15. 12
      spec/requests/lettings_logs_controller_spec.rb
  16. 20
      spec/requests/organisations_controller_spec.rb
  17. 10
      spec/requests/sales_logs_controller_spec.rb

23
app/controllers/lettings_logs_filters_controller.rb

@ -9,6 +9,13 @@ class LettingsLogsFiltersController < ApplicationController
@filter = filter @filter = filter
render "filters/#{filter}" render "filters/#{filter}"
end end
define_method("organisation_#{filter}") do
@organisation_id = params["id"]
@filter_type = "lettings_logs"
@filter = filter
render "filters/#{filter}"
end
end end
%w[status needstype assigned_to owned_by managed_by].each do |filter| %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"]) redirect_to csv_download_lettings_logs_path(search: params["search"], codes_only: params["codes_only"])
end 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 end
def update_years 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"]) redirect_to csv_download_lettings_logs_path(search: params["search"], codes_only: params["codes_only"])
end end
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 end
private private

2
app/controllers/organisations_controller.rb

@ -148,6 +148,8 @@ class OrganisationsController < ApplicationController
end end
def download_lettings_csv 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) organisation_logs = LettingsLog.visible.where(owning_organisation_id: @organisation.id)
unpaginated_filtered_logs = filter_manager.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"

24
app/controllers/sales_logs_filters_controller.rb

@ -9,6 +9,13 @@ class SalesLogsFiltersController < ApplicationController
@filter = filter @filter = filter
render "filters/#{filter}" render "filters/#{filter}"
end end
define_method("organisation_#{filter}") do
@filter_type = "sales_logs"
@organisation_id = params["id"]
@filter = filter
render "filters/#{filter}"
end
end end
%w[status assigned_to owned_by managed_by].each do |filter| %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"]) redirect_to csv_download_sales_logs_path(search: params["search"], codes_only: params["codes_only"])
end 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 end
def update_years 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"]) redirect_to csv_download_sales_logs_path(search: params["search"], codes_only: params["codes_only"])
end end
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 end
private private

43
app/helpers/filters_helper.rb

@ -175,25 +175,41 @@ module FiltersHelper
def check_your_answers_lettings_filters_list(session_filters) def check_your_answers_lettings_filters_list(session_filters)
[ [
{ label: "Collection year", value: formatted_years_filter(session_filters), path: filters_years_lettings_logs_path }, { id: "years", label: "Collection year", value: formatted_years_filter(session_filters) },
{ label: "Status", value: formatted_status_filter(session_filters), path: filters_status_lettings_logs_path }, { id: "status", label: "Status", value: formatted_status_filter(session_filters) },
{ label: "Needs type", value: formatted_needstype_filter(session_filters), path: filters_needstype_lettings_logs_path }, { id: "needstype", label: "Needs type", value: formatted_needstype_filter(session_filters) },
{ label: "Assigned to", value: formatted_assigned_to_filter(session_filters), path: filters_assigned_to_lettings_logs_path }, { id: "assigned_to", label: "Assigned to", value: formatted_assigned_to_filter(session_filters) },
{ label: "Owned by", value: formatted_owned_by_filter(session_filters), path: filters_owned_by_lettings_logs_path }, { id: "owned_by", label: "Owned by", value: formatted_owned_by_filter(session_filters) },
{ label: "Managed by", value: formatted_managed_by_filter(session_filters), path: filters_managed_by_lettings_logs_path }, { id: "managed_by", label: "Managed by", value: formatted_managed_by_filter(session_filters) },
] ]
end end
def check_your_answers_sales_filters_list(session_filters) def check_your_answers_sales_filters_list(session_filters)
[ [
{ label: "Collection year", value: formatted_years_filter(session_filters), path: filters_years_sales_logs_path }, { id: "years", label: "Collection year", value: formatted_years_filter(session_filters) },
{ label: "Status", value: formatted_status_filter(session_filters), path: filters_status_sales_logs_path }, { id: "status", label: "Status", value: formatted_status_filter(session_filters) },
{ label: "Assigned to", value: formatted_assigned_to_filter(session_filters), path: filters_assigned_to_sales_logs_path }, { id: "assigned_to", label: "Assigned to", value: formatted_assigned_to_filter(session_filters) },
{ label: "Owned by", value: formatted_owned_by_filter(session_filters), path: filters_owned_by_sales_logs_path }, { id: "owned_by", label: "Owned by", value: formatted_owned_by_filter(session_filters) },
{ label: "Managed by", value: formatted_managed_by_filter(session_filters), path: filters_managed_by_sales_logs_path }, { id: "managed_by", label: "Managed by", value: formatted_managed_by_filter(session_filters) },
] ]
end 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 private
def applied_filters(filter_type) def applied_filters(filter_type)
@ -250,9 +266,10 @@ private
end end
def formatted_owned_by_filter(session_filters) 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 end
def formatted_managed_by_filter(session_filters) def formatted_managed_by_filter(session_filters)

2
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", <%= render partial: "filters/radio_filter",
locals: { locals: {
f:, f:,

2
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: { <%= render partial: "filters/radio_filter", locals: {
f:, f:,
options: { options: {

2
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", <%= render partial: "filters/checkbox_filter",
locals: { locals: {
f:, f:,

2
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: { <%= render partial: "filters/radio_filter", locals: {
f:, f:,
options: { options: {

2
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", <%= render partial: "filters/checkbox_filter",
locals: { locals: {
f:, f:,

2
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? %> <% if params["error"].present? %>
<div class="govuk-error-summary" data-module="govuk-error-summary"> <div class="govuk-error-summary" data-module="govuk-error-summary">
<div role="alert"> <div role="alert">

5
app/views/logs/download_csv.html.erb

@ -6,6 +6,9 @@
<div class="govuk-grid-row"> <div class="govuk-grid-row">
<div class="govuk-grid-column-full"> <div class="govuk-grid-column-full">
<% if @organisation.present? %>
<span class="govuk-caption-l"><%= @organisation.name %></span>
<% end %>
<h1 class="govuk-heading-l">Download CSV</h1> <h1 class="govuk-heading-l">Download CSV</h1>
<p class="govuk-body">We'll send a secure download link to your email address <strong><%= @current_user.email %></strong>.</p> <p class="govuk-body">We'll send a secure download link to your email address <strong><%= @current_user.email %></strong>.</p>
@ -35,7 +38,7 @@
<% row.with_action( <% row.with_action(
text: "Change", 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 %>
<% end %> <% end %>

8
config/routes.rb

@ -181,6 +181,14 @@ Rails.application.routes.draw do
get "merge-request", to: "organisations#merge_request" get "merge-request", to: "organisations#merge_request"
get "deactivate", to: "organisations#deactivate" get "deactivate", to: "organisations#deactivate"
get "reactivate", to: "organisations#reactivate" 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
end end

8
spec/features/lettings_log_spec.rb

@ -177,8 +177,8 @@ RSpec.describe "Lettings Log Features" do
it "allows updating filters" do it "allows updating filters" do
click_link("Download (CSV, codes only)") click_link("Download (CSV, codes only)")
expect(page).to have_content("You've selected 2 logs") expect(page).to have_content("You've selected 2 logs")
click_link("Change", href: "/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?search=1&codes_only=true") expect(page).to have_current_path("/lettings-logs/filters/needstype?codes_only=true&search=1")
check("needstypes-1-field", allow_label_click: true) check("needstypes-1-field", allow_label_click: true)
click_button("Save changes") 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_current_path("/lettings-logs/csv-download?codes_only=true&search=1")
expect(page).to have_content("You've selected 1 logs") 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) check("status-not-started-field", allow_label_click: true)
click_button("Save changes") 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 it "routes back to the filters CYA when cancel is pressed" do
click_link("Download (CSV)") 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") click_link(text: "Cancel")
expect(page).to have_current_path("/lettings-logs/csv-download?codes_only=false&search=1") expect(page).to have_current_path("/lettings-logs/csv-download?codes_only=false&search=1")

6
spec/features/sales_log_spec.rb

@ -205,14 +205,14 @@ RSpec.describe "Sales Log Features" do
it "allows updating filters" do it "allows updating filters" do
click_link("Download (CSV, codes only)") click_link("Download (CSV, codes only)")
expect(page).to have_content("You've selected 2 logs") 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) choose("assigned-to-you-field", allow_label_click: true)
click_button("Save changes") click_button("Save changes")
expect(page).to have_content("You've selected 1 logs") 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) check("status-not-started-field", allow_label_click: true)
click_button("Save changes") 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 it "routes back to the filters CYA when cancel is pressed" do
click_link("Download (CSV)") 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") click_link(text: "Cancel")
expect(page).to have_current_path("/sales-logs/csv-download?codes_only=false&search=1") expect(page).to have_current_path("/sales-logs/csv-download?codes_only=false&search=1")

12
spec/requests/lettings_logs_controller_spec.rb

@ -1426,12 +1426,12 @@ RSpec.describe LettingsLogsController, type: :request do
it "allows updating log filters" do it "allows updating log filters" do
expect(page).to have_content("Check your filters") expect(page).to have_content("Check your filters")
expect(page).to have_link("Change", count: 6) 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/years?codes_only=false&search=#{search_term}")
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/assigned-to?codes_only=false&search=#{search_term}")
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/owned-by?codes_only=false&search=#{search_term}")
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/managed-by?codes_only=false&search=#{search_term}")
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/status?codes_only=false&search=#{search_term}")
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/needstype?codes_only=false&search=#{search_term}")
end end
end end

20
spec/requests/organisations_controller_spec.rb

@ -1589,7 +1589,7 @@ RSpec.describe OrganisationsController, type: :request do
let(:tenancycode) { "42" } let(:tenancycode) { "42" }
before do before do
create(:lettings_log, owning_organisation: organisation, tenancycode:) create(:lettings_log, :in_progress, owning_organisation: organisation, tenancycode:)
end end
context "when there is at least one log visible" do 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 context "when you download the CSV" do
let(:other_organisation) { create(:organisation) } let(:other_organisation) { create(:organisation) }
let!(:lettings_logs) { create_list(:lettings_log, 2, :in_progress, owning_organisation: organisation) }
before do before do
create_list(:lettings_log, 2, owning_organisation: organisation) create(:lettings_log, :in_progress, owning_organisation: organisation, status: "pending", skip_update_status: true)
create(:lettings_log, owning_organisation: organisation, status: "pending", skip_update_status: true) create_list(:lettings_log, 2, :in_progress, owning_organisation: other_organisation)
create_list(:lettings_log, 2, owning_organisation: other_organisation)
end end
it "only includes logs from that organisation" do context "when no year filters are applied" do
get "/organisations/#{organisation.id}/lettings-logs/csv-download?codes_only=false" 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.") expect(page).to have_text("You've selected 3 logs.")
end end

10
spec/requests/sales_logs_controller_spec.rb

@ -986,11 +986,11 @@ RSpec.describe SalesLogsController, type: :request do
it "allows updating log filters" do it "allows updating log filters" do
expect(page).to have_content("Check your filters") expect(page).to have_content("Check your filters")
expect(page).to have_link("Change", count: 5) 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/years?codes_only=false&search=#{search_term}")
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/assigned-to?codes_only=false&search=#{search_term}")
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/owned-by?codes_only=false&search=#{search_term}")
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/managed-by?codes_only=false&search=#{search_term}")
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/status?codes_only=false&search=#{search_term}")
end end
it "has a hidden field with the search term" do it "has a hidden field with the search term" do

Loading…
Cancel
Save