diff --git a/app/models/location.rb b/app/models/location.rb index f57a5706a..a2c57a7bb 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -110,7 +110,7 @@ class Location < ApplicationRecord def self.find_by_id_on_multiple_fields(id) return if id.nil? - where(id:).or(where(old_visible_id: id)).first + where(id:).or(where("ltrim(old_visible_id, '0') = ?", id.to_s)).first end def postcode=(postcode) diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 06550caec..58135dd76 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -169,9 +169,9 @@ class Scheme < ApplicationRecord if scheme_id.start_with?("S") where(id: scheme_id[1..]).first elsif location_id.present? - joins(:locations).where("schemes.old_visible_id = ? AND locations.old_visible_id = ?", scheme_id.to_s, location_id.to_s).first || where(old_visible_id: scheme_id).first + joins(:locations).where("ltrim(schemes.old_visible_id, '0') = ? AND ltrim(locations.old_visible_id, '0') = ?", scheme_id.to_s, location_id.to_s).first || where("ltrim(schemes.old_visible_id, '0') = ?", scheme_id.to_s).first else - where(old_visible_id: scheme_id).first + where("ltrim(old_visible_id, '0') = ?", scheme_id.to_s).first end end diff --git a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb index 3d77ff467..edd88d5a1 100644 --- a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb @@ -1390,6 +1390,19 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do it "assigns the correct location" do expect(parser.log.location).to eql(location) end + + context "when location had leading zeroes in its id in Old CORE" do + let(:attributes) { { bulk_upload:, field_4: scheme.old_visible_id, field_5: "123", field_111: owning_org.old_visible_id, field_113: owning_org.old_visible_id } } + + before do + location.old_visible_id = "00123" + location.save! + end + + it "assigns the correct location" do + expect(parser.log.location).to eql(location) + end + end end end @@ -1408,6 +1421,19 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do it "assigns the correct scheme" do expect(parser.log.scheme).to eql(scheme) end + + context "when scheme had leading zeroes in its id in Old CORE" do + let(:attributes) { { bulk_upload:, field_4: "10", field_5: location.old_visible_id, field_111: owning_org.old_visible_id, field_113: owning_org.old_visible_id } } + + before do + scheme.old_visible_id = "010" + scheme.save! + end + + it "assigns the correct scheme" do + expect(parser.log.scheme).to eql(scheme) + end + end end end diff --git a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb index 399001dbc..391bb5123 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -1733,6 +1733,20 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do it "assigns the correct location" do expect(parser.log.location).to eql(location) end + + context "when location had leading zeroes in its id in Old CORE" do + let(:attributes) { { bulk_upload:, field_15: scheme.old_visible_id, field_16: "123", field_1: owning_org.old_visible_id, field_2: owning_org.old_visible_id } } + + before do + location.old_visible_id = "00123" + location.save! + end + + it "assigns the correct location" do + expect(parser.log.location).to eql(location) + end + end + end end @@ -1751,6 +1765,19 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do it "assigns the correct scheme" do expect(parser.log.scheme).to eql(scheme) end + + context "when scheme had leading zeroes in its id in Old CORE" do + let(:attributes) { { bulk_upload:, field_15: "10", field_16: location.old_visible_id, field_1: owning_org.old_visible_id, field_2: owning_org.old_visible_id } } + + before do + scheme.old_visible_id = "010" + scheme.save! + end + + it "assigns the correct scheme" do + expect(parser.log.scheme).to eql(scheme) + end + end end end