Browse Source

can filter logs by bulk upload

pull/1223/head
Phil Lee 3 years ago
parent
commit
216075155d
  1. 4
      app/controllers/modules/logs_filter.rb
  2. 4
      app/models/log.rb
  3. 4
      app/models/user.rb
  4. 19
      app/views/logs/_log_filters.erb
  5. 6
      spec/models/user_spec.rb
  6. 49
      spec/requests/lettings_logs_controller_spec.rb

4
app/controllers/modules/logs_filter.rb

@ -7,7 +7,9 @@ module Modules::LogsFilter
def load_session_filters(specific_org: false) def load_session_filters(specific_org: false)
current_filters = session[:logs_filters] current_filters = session[:logs_filters]
new_filters = current_filters.present? ? JSON.parse(current_filters) : {} new_filters = current_filters.present? ? JSON.parse(current_filters) : {}
current_user.logs_filters(specific_org:).each { |filter| new_filters[filter] = params[filter] if params[filter].present? } current_user.logs_filters(specific_org:).each do |filter|
new_filters[filter] = params[filter] if params[filter].present?
end
params["organisation_select"] == "all" ? new_filters.except("organisation") : new_filters params["organisation_select"] == "all" ? new_filters.except("organisation") : new_filters
end end

4
app/models/log.rb

@ -24,6 +24,10 @@ class Log < ApplicationRecord
where(created_by: user) where(created_by: user)
end end
} }
scope :filter_by_bulk_upload_id, lambda { |bulk_upload_id, user|
joins(:bulk_upload)
.where(bulk_upload: { id: bulk_upload_id, user: })
}
scope :created_by, ->(user) { where(created_by: user) } scope :created_by, ->(user) { where(created_by: user) }
def collection_start_year def collection_start_year

4
app/models/user.rb

@ -145,9 +145,9 @@ class User < ApplicationRecord
def logs_filters(specific_org: false) def logs_filters(specific_org: false)
if (support? && !specific_org) || organisation.has_managing_agents? if (support? && !specific_org) || organisation.has_managing_agents?
%w[status years user organisation] %w[status years user organisation bulk_upload_id]
else else
%w[status years user] %w[status years user bulk_upload_id]
end end
end end

19
app/views/logs/_log_filters.erb

@ -3,13 +3,21 @@
<div class="app-filter__header"> <div class="app-filter__header">
<h2 class="govuk-heading-m">Filters</h2> <h2 class="govuk-heading-m">Filters</h2>
</div> </div>
<div class="app-filter__content"> <div class="app-filter__content">
<%= form_with html: { method: :get } do |f| %> <%= form_with html: { method: :get } do |f| %>
<% years = { "2021": "2021/22", "2022": "2022/23" } %> <% years = { "2021": "2021/22", "2022": "2022/23" } %>
<% all_or_yours = { "all": { label: "All" }, "yours": { label: "Yours" } } %> <% all_or_yours = { "all": { label: "All" }, "yours": { label: "Yours" } } %>
<% bulk_upload_options = (@session_filters["bulk_upload_id"] || []).reject(&:blank?).index_with { |_bulk_upload_id| "With logs from bulk upload" } %>
<% if bulk_upload_options.present? %>
<%= render partial: "filters/checkbox_filter", locals: { f: f, options: bulk_upload_options, label: "Bulk upload", category: "bulk_upload_id" } %>
<% end %>
<%= render partial: "filters/checkbox_filter", locals: { f: f, options: years, label: "Collection year", category: "years" } %> <%= render partial: "filters/checkbox_filter", locals: { f: f, options: years, label: "Collection year", category: "years" } %>
<%= render partial: "filters/checkbox_filter", locals: { f: f, options: status_filters, label: "Status", category: "status" } %> <%= render partial: "filters/checkbox_filter", locals: { f: f, options: status_filters, label: "Status", category: "status" } %>
<%= render partial: "filters/radio_filter", locals: { f: f, options: all_or_yours, label: "Logs", category: "user", } %> <%= render partial: "filters/radio_filter", locals: { f: f, options: all_or_yours, label: "Logs", category: "user" } %>
<% if (@current_user.support? || @current_user.organisation.has_managing_agents?) && request.path == "/lettings-logs" %> <% if (@current_user.support? || @current_user.organisation.has_managing_agents?) && request.path == "/lettings-logs" %>
<%= render partial: "filters/radio_filter", locals: { <%= render partial: "filters/radio_filter", locals: {
f: f, f: f,
@ -21,14 +29,15 @@
type: "select", type: "select",
label: "Organisation", label: "Organisation",
category: "organisation", category: "organisation",
options: organisations_filter_options(@current_user) options: organisations_filter_options(@current_user),
} },
} },
}, },
label: "Organisation", label: "Organisation",
category: "organisation_select" category: "organisation_select",
} %> } %>
<% end %> <% end %>
<%= f.govuk_submit "Apply filters", class: "govuk-!-margin-bottom-0" %> <%= f.govuk_submit "Apply filters", class: "govuk-!-margin-bottom-0" %>
<% end %> <% end %>
</div> </div>

6
spec/models/user_spec.rb

@ -123,7 +123,7 @@ RSpec.describe User, type: :model do
end end
it "can filter lettings logs by user, year and status" do it "can filter lettings logs by user, year and status" do
expect(user.logs_filters).to eq(%w[status years user]) expect(user.logs_filters).to eq(%w[status years user bulk_upload_id])
end end
end end
@ -133,7 +133,7 @@ RSpec.describe User, type: :model do
end end
it "can filter lettings logs by user, year, status and organisation" do it "can filter lettings logs by user, year, status and organisation" do
expect(user.logs_filters).to eq(%w[status years user organisation]) expect(user.logs_filters).to eq(%w[status years user organisation bulk_upload_id])
end end
end end
end end
@ -159,7 +159,7 @@ RSpec.describe User, type: :model do
end end
it "can filter lettings logs by user, year, status and organisation" do it "can filter lettings logs by user, year, status and organisation" do
expect(user.logs_filters).to eq(%w[status years user organisation]) expect(user.logs_filters).to eq(%w[status years user organisation bulk_upload_id])
end end
end end

49
spec/requests/lettings_logs_controller_spec.rb

@ -400,6 +400,55 @@ RSpec.describe LettingsLogsController, type: :request do
expect(page).not_to have_link(lettings_log_2022.id.to_s) expect(page).not_to have_link(lettings_log_2022.id.to_s)
end end
end end
context "with bulk_upload_id filter" do
context "with bulk upload that belongs to current user" do
let(:organisation) { create(:organisation) }
let(:user) { create(:user, organisation:) }
let(:bulk_upload) { create(:bulk_upload, user:) }
let!(:included_log) { create(:lettings_log, bulk_upload:, owning_organisation: organisation) }
let!(:excluded_log) { create(:lettings_log, owning_organisation: organisation) }
it "returns logs only associated with the bulk upload" do
get "/lettings-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).to have_content(included_log.id)
expect(page).not_to have_content(excluded_log.id)
end
it "displays filter" do
get "/lettings-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).to have_content("With logs from bulk upload")
end
end
context "with bulk upload that belongs to another user" do
let(:organisation) { create(:organisation) }
let(:user) { create(:user, organisation:) }
let(:other_user) { create(:user, organisation:) }
let(:bulk_upload) { create(:bulk_upload, user: other_user) }
let!(:excluded_log) { create(:lettings_log, bulk_upload:, owning_organisation: organisation) }
let!(:also_excluded_log) { create(:lettings_log, owning_organisation: organisation) }
it "does not return any logs" do
get "/lettings-logs?bulk_upload_id[]=#{bulk_upload.id}"
expect(page).not_to have_content(excluded_log.id)
expect(page).not_to have_content(also_excluded_log.id)
end
end
end
context "without bulk_upload_id" do
it "does not display filter" do
get "/lettings-logs"
expect(page).not_to have_content("With logs from bulk upload")
end
end
end end
end end

Loading…
Cancel
Save