Browse Source

Filter logs list by existing forms only

pull/2149/head
Kat 2 years ago
parent
commit
3672122cde
  1. 2
      app/controllers/lettings_logs_controller.rb
  2. 4
      app/controllers/organisations_controller.rb
  3. 2
      app/controllers/sales_logs_controller.rb
  4. 16
      app/models/form_handler.rb
  5. 7
      app/models/lettings_log.rb
  6. 7
      app/models/sales_log.rb
  7. 28
      spec/models/lettings_log_spec.rb
  8. 27
      spec/models/sales_log_spec.rb

2
app/controllers/lettings_logs_controller.rb

@ -13,7 +13,7 @@ class LettingsLogsController < LogsController
before_action :redirect_if_bulk_upload_resolved, only: [:index] before_action :redirect_if_bulk_upload_resolved, only: [:index]
def index def index
all_logs = current_user.lettings_logs.visible all_logs = current_user.lettings_logs.visible.filter_by_years_or_nil(FormHandler.instance.years_of_available_lettings_forms)
unpaginated_filtered_logs = filter_manager.filtered_logs(all_logs, search_term, session_filters) unpaginated_filtered_logs = filter_manager.filtered_logs(all_logs, search_term, session_filters)
@delete_logs_path = delete_logs_lettings_logs_path(search: search_term) @delete_logs_path = delete_logs_lettings_logs_path(search: search_term)

4
app/controllers/organisations_controller.rb

@ -106,7 +106,7 @@ class OrganisationsController < ApplicationController
end end
def lettings_logs def lettings_logs
organisation_logs = LettingsLog.visible.filter_by_organisation(@organisation) organisation_logs = LettingsLog.visible.filter_by_organisation(@organisation).filter_by_years_or_nil(FormHandler.instance.years_of_available_lettings_forms)
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)
@search_term = search_term @search_term = search_term
@ -134,7 +134,7 @@ class OrganisationsController < ApplicationController
end end
def sales_logs def sales_logs
organisation_logs = SalesLog.visible.filter_by_organisation(@organisation) organisation_logs = SalesLog.visible.filter_by_organisation(@organisation).filter_by_years_or_nil(FormHandler.instance.years_of_available_sales_forms)
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)
respond_to do |format| respond_to do |format|

2
app/controllers/sales_logs_controller.rb

@ -15,7 +15,7 @@ class SalesLogsController < LogsController
end end
def index def index
all_logs = current_user.sales_logs.visible all_logs = current_user.sales_logs.visible.filter_by_years_or_nil(FormHandler.instance.years_of_available_sales_forms)
unpaginated_filtered_logs = filter_manager.filtered_logs(all_logs, search_term, session_filters) unpaginated_filtered_logs = filter_manager.filtered_logs(all_logs, search_term, session_filters)
@delete_logs_path = delete_logs_sales_logs_path(search: search_term) @delete_logs_path = delete_logs_sales_logs_path(search: search_term)

16
app/models/form_handler.rb

@ -207,6 +207,22 @@ class FormHandler
end end
end end
def years_of_available_lettings_forms
years = []
lettings_forms.each_value do |form|
years << form.start_date.year
end
years
end
def years_of_available_sales_forms
years = []
sales_forms.each_value do |form|
years << form.start_date.year
end
years
end
private private
def get_all_forms def get_all_forms

7
app/models/lettings_log.rb

@ -40,6 +40,13 @@ class LettingsLog < Log
belongs_to :managing_organisation, class_name: "Organisation", optional: true belongs_to :managing_organisation, class_name: "Organisation", optional: true
scope :filter_by_year, ->(year) { where(startdate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) } scope :filter_by_year, ->(year) { where(startdate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) }
scope :filter_by_years_or_nil, lambda { |years, _user = nil|
first_year = years.shift
query = filter_by_year(first_year)
years.each { |year| query = query.or(filter_by_year(year)) }
query = query.or(where(startdate: nil))
query.all
}
scope :filter_by_tenant_code, ->(tenant_code) { where("tenancycode ILIKE ?", "%#{tenant_code}%") } scope :filter_by_tenant_code, ->(tenant_code) { where("tenancycode ILIKE ?", "%#{tenant_code}%") }
scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") } scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") }
scope :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") } scope :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") }

7
app/models/sales_log.rb

@ -36,6 +36,13 @@ class SalesLog < Log
belongs_to :managing_organisation, class_name: "Organisation", optional: true belongs_to :managing_organisation, class_name: "Organisation", optional: true
scope :filter_by_year, ->(year) { where(saledate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) } scope :filter_by_year, ->(year) { where(saledate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) }
scope :filter_by_years_or_nil, lambda { |years, _user = nil|
first_year = years.shift
query = filter_by_year(first_year)
years.each { |year| query = query.or(filter_by_year(year)) }
query = query.or(where(saledate: nil))
query.all
}
scope :filter_by_purchaser_code, ->(purchid) { where("purchid ILIKE ?", "%#{purchid}%") } scope :filter_by_purchaser_code, ->(purchid) { where("purchid ILIKE ?", "%#{purchid}%") }
scope :search_by, lambda { |param| scope :search_by, lambda { |param|
filter_by_purchaser_code(param) filter_by_purchaser_code(param)

28
spec/models/lettings_log_spec.rb

@ -2849,6 +2849,34 @@ RSpec.describe LettingsLog do
end end
end end
context "when filtering by year or nil" do
before do
Timecop.freeze(Time.utc(2021, 5, 3))
end
after do
Timecop.unfreeze
end
it "allows filtering on a single year or nil" do
lettings_log_1.startdate = nil
lettings_log_1.save!(validate: false)
expect(described_class.filter_by_years_or_nil(%w[2021]).count).to eq(2)
end
it "allows filtering by multiple years or nil using OR" do
lettings_log_1.startdate = nil
lettings_log_1.save!(validate: false)
expect(described_class.filter_by_years_or_nil(%w[2021 2022]).count).to eq(3)
end
it "can filter by year(s) AND status" do
lettings_log_2.startdate = nil
lettings_log_2.save!(validate: false)
expect(described_class.filter_by_years_or_nil(%w[2021 2022]).filter_by_status("in_progress").count).to eq(3)
end
end
context "when filtering by organisation" do context "when filtering by organisation" do
let(:organisation_1) { create(:organisation) } let(:organisation_1) { create(:organisation) }
let(:organisation_2) { create(:organisation) } let(:organisation_2) { create(:organisation) }

27
spec/models/sales_log_spec.rb

@ -160,6 +160,33 @@ RSpec.describe SalesLog, type: :model do
end end
end end
context "when filtering by year or nil" do
before do
Timecop.freeze(Time.utc(2021, 5, 3))
create(:sales_log, :in_progress, saledate: nil)
create(:sales_log, :in_progress, saledate: Time.zone.local(2021, 4, 1))
sales_log_3 = create(:sales_log, :in_progress)
sales_log_3.saledate = Time.zone.local(2022, 5, 1)
sales_log_3.save!(validate: false)
end
after do
Timecop.unfreeze
end
it "allows filtering on a single year or nil" do
expect(described_class.filter_by_years_or_nil(%w[2021]).count).to eq(2)
end
it "allows filtering by multiple years or nil using OR" do
expect(described_class.filter_by_years_or_nil(%w[2021 2022]).count).to eq(3)
end
it "can filter by year(s) AND status" do
expect(described_class.filter_by_years_or_nil(%w[2021 2022]).filter_by_status("in_progress").count).to eq(3)
end
end
context "when filtering duplicate logs" do context "when filtering duplicate logs" do
let(:organisation) { create(:organisation) } let(:organisation) { create(:organisation) }
let(:log) { create(:sales_log, :duplicate, owning_organisation: organisation) } let(:log) { create(:sales_log, :duplicate, owning_organisation: organisation) }

Loading…
Cancel
Save