Browse Source

CLDC-2741 Order log search results (#2299)

* Order lettings logs search results

* Order sales logs search results

* lint

* Update SQL casing
pull/2355/head^2
kosiakkatrina 9 months ago committed by GitHub
parent
commit
1d326182ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      app/models/lettings_log.rb
  2. 5
      app/models/sales_log.rb
  3. 24
      spec/models/lettings_log_spec.rb
  4. 94
      spec/models/sales_log_spec.rb

6
app/models/lettings_log.rb

@ -59,11 +59,17 @@ class LettingsLog < Log
query.all
}
scope :search_by, lambda { |param|
by_id = Arel.sql("CASE WHEN lettings_logs.id = #{param.to_i} THEN 0 ELSE 1 END")
by_tenant_code = Arel.sql("CASE WHEN tenancycode = '#{param}' THEN 0 WHEN tenancycode ILIKE '%#{param}%' THEN 1 ELSE 2 END")
by_propcode = Arel.sql("CASE WHEN propcode = '#{param}' THEN 0 WHEN propcode ILIKE '%#{param}%' THEN 1 ELSE 2 END")
by_postcode = Arel.sql("CASE WHEN REPLACE(postcode_full, ' ', '') = '#{param.delete(' ')}' THEN 0 when REPLACE(postcode_full, ' ', '') ILIKE '%#{param.delete(' ')}%' then 1 ELSE 2 END")
filter_by_location_postcode(param)
.or(filter_by_tenant_code(param))
.or(filter_by_propcode(param))
.or(filter_by_postcode(param))
.or(filter_by_id(param.gsub(/log/i, "")))
.order(by_id, by_tenant_code, by_propcode, by_postcode)
}
scope :after_date, ->(date) { where("lettings_logs.startdate >= ?", date) }
scope :before_date, ->(date) { where("lettings_logs.startdate < ?", date) }

5
app/models/sales_log.rb

@ -46,9 +46,14 @@ class SalesLog < Log
}
scope :filter_by_purchaser_code, ->(purchid) { where("purchid ILIKE ?", "%#{purchid}%") }
scope :search_by, lambda { |param|
by_id = Arel.sql("CASE WHEN id = #{param.to_i} THEN 0 ELSE 1 END")
by_purchaser_code = Arel.sql("CASE WHEN purchid = '#{param}' THEN 0 WHEN purchid ILIKE '%#{param}%' THEN 1 ELSE 2 END")
by_postcode = Arel.sql("CASE WHEN REPLACE(postcode_full, ' ', '') = '#{param.delete(' ')}' THEN 0 WHEN REPLACE(postcode_full, ' ', '') ILIKE '%#{param.delete(' ')}%' THEN 1 ELSE 2 END")
filter_by_purchaser_code(param)
.or(filter_by_postcode(param))
.or(filter_by_id(param.gsub(/log/i, "")))
.order(by_id, by_purchaser_code, by_postcode)
}
scope :age1_answered, -> { where.not(age1: nil).or(where(age1_known: [1, 2])) }
scope :duplicate_logs, lambda { |log|

24
spec/models/lettings_log_spec.rb

@ -2885,6 +2885,30 @@ RSpec.describe LettingsLog do
expect(result.first.id).to eq lettings_log_to_search.id
end
end
context "when matching multiple records on different fields" do
let!(:lettings_log_with_propcode) { create(:lettings_log, propcode: lettings_log_to_search.id) }
let!(:lettings_log_with_tenancycode) { create(:lettings_log, tenancycode: lettings_log_to_search.id) }
let!(:lettings_log_with_postcode) { create(:lettings_log, postcode_full: "C1 1AC") }
let!(:lettings_log_with_postcode_tenancycode) { create(:lettings_log, tenancycode: "C1 1AC") }
let!(:lettings_log_with_postcode_propcode) { create(:lettings_log, propcode: "C1 1AC") }
it "returns all matching records in correct order with matching IDs" do
result = described_class.search_by(lettings_log_to_search.id.to_s)
expect(result.count).to eq(3)
expect(result.first.id).to eq lettings_log_to_search.id
expect(result.second.id).to eq lettings_log_with_tenancycode.id
expect(result.third.id).to eq lettings_log_with_propcode.id
end
it "returns all matching records in correct order with matching postcode" do
result = described_class.search_by("C1 1AC")
expect(result.count).to eq(3)
expect(result.first.id).to eq lettings_log_with_postcode_tenancycode.id
expect(result.second.id).to eq lettings_log_with_postcode_propcode.id
expect(result.third.id).to eq lettings_log_with_postcode.id
end
end
end
end

94
spec/models/sales_log_spec.rb

@ -480,7 +480,7 @@ RSpec.describe SalesLog, type: :model do
context "when there is a log with nil values for purchid" do
let!(:purchid_not_given) { create(:sales_log, :duplicate, purchid: nil, owning_organisation: organisation) }
it "returns the log as a duplicate if tenancy code is nil" do
it "returns the log as a duplicate if purchaser code is nil" do
log.update!(purchid: nil)
expect(duplicate_sets.count).to eq(1)
expect(duplicate_sets.first).to contain_exactly(log.id, purchid_not_given.id)
@ -1142,5 +1142,97 @@ RSpec.describe SalesLog, type: :model do
end
end
end
context "when searching logs" do
let!(:sales_log_to_search) { create(:sales_log, :completed, purchid: "to search", postcode_full: "ME0 0WW") }
before do
create_list(:sales_log, 5, :completed)
end
describe "#filter_by_id" do
it "allows searching by a log ID" do
result = described_class.filter_by_id(sales_log_to_search.id.to_s)
expect(result.count).to eq(1)
expect(result.first.id).to eq sales_log_to_search.id
end
end
describe "#filter_by_purchaser_code" do
it "allows searching by a purchaser_code" do
result = described_class.filter_by_purchaser_code(sales_log_to_search.purchid)
expect(result.count).to eq(1)
expect(result.first.id).to eq sales_log_to_search.id
end
context "when purchaser_code has lower case letters" do
let(:matching_purchaser_code_lower_case) { sales_log_to_search.purchid.downcase }
it "allows searching by a purchaser_code" do
result = described_class.filter_by_purchaser_code(matching_purchaser_code_lower_case)
expect(result.count).to eq(1)
expect(result.first.id).to eq sales_log_to_search.id
end
end
end
describe "#filter_by_postcode" do
it "allows searching by a Property Postcode" do
result = described_class.filter_by_postcode(sales_log_to_search.postcode_full)
expect(result.count).to eq(1)
expect(result.first.id).to eq sales_log_to_search.id
end
end
describe "#search_by" do
it "allows searching using ID" do
result = described_class.search_by(sales_log_to_search.id.to_s)
expect(result.count).to eq(1)
expect(result.first.id).to eq sales_log_to_search.id
end
it "allows searching using purchaser code" do
result = described_class.search_by(sales_log_to_search.purchid)
expect(result.count).to eq(1)
expect(result.first.id).to eq sales_log_to_search.id
end
it "allows searching by a Property Postcode" do
result = described_class.search_by(sales_log_to_search.postcode_full)
expect(result.count).to eq(1)
expect(result.first.id).to eq sales_log_to_search.id
end
context "when postcode has spaces and lower case letters" do
let(:matching_postcode_lower_case_with_spaces) { sales_log_to_search.postcode_full.downcase.chars.insert(3, " ").join }
it "allows searching by a Property Postcode" do
result = described_class.search_by(matching_postcode_lower_case_with_spaces)
expect(result.count).to eq(1)
expect(result.first.id).to eq sales_log_to_search.id
end
end
context "when matching multiple records on different fields" do
let!(:sales_log_with_purchid) { create(:sales_log, purchid: sales_log_to_search.id) }
let!(:sales_log_with_postcode) { create(:sales_log, postcode_full: "C1 1AC") }
let!(:sales_log_with_postcode_purchid) { create(:sales_log, purchid: "C1 1AC") }
it "returns all matching records in correct order with matching IDs" do
result = described_class.search_by(sales_log_to_search.id.to_s)
expect(result.count).to eq(2)
expect(result.first.id).to eq sales_log_to_search.id
expect(result.second.id).to eq sales_log_with_purchid.id
end
it "returns all matching records in correct order with matching postcode" do
result = described_class.search_by("C1 1AC")
expect(result.count).to eq(2)
expect(result.first.id).to eq sales_log_with_postcode_purchid.id
expect(result.second.id).to eq sales_log_with_postcode.id
end
end
end
end
end
# rubocop:enable RSpec/MessageChain

Loading…
Cancel
Save