Browse Source

bulk upload handles locations

pull/1329/head
Phil Lee 3 years ago
parent
commit
cb3b2d0487
  1. 6
      app/models/location.rb
  2. 25
      app/services/bulk_upload/lettings/row_parser.rb
  3. 5
      spec/factories/location.rb
  4. 64
      spec/services/bulk_upload/lettings/row_parser_spec.rb

6
app/models/location.rb

@ -366,6 +366,12 @@ class Location < ApplicationRecord
enum type_of_unit: TYPE_OF_UNIT enum type_of_unit: TYPE_OF_UNIT
def self.find_by_id_on_mulitple_fields(id)
return if id.nil?
where(id:).or(where(old_visible_id: id)).first
end
def postcode=(postcode) def postcode=(postcode)
if postcode if postcode
super UKPostcode.parse(postcode).to_s super UKPostcode.parse(postcode).to_s

25
app/services/bulk_upload/lettings/row_parser.rb

@ -167,6 +167,9 @@ class BulkUpload::Lettings::RowParser
validate :validate_scheme_related validate :validate_scheme_related
validate :validate_scheme_exists validate :validate_scheme_exists
validate :validate_location_related
validate :validate_location_exists
def valid? def valid?
errors.clear errors.clear
@ -202,6 +205,27 @@ class BulkUpload::Lettings::RowParser
private private
def validate_location_related
return if scheme.blank? || location.blank?
unless location.scheme == scheme
block_log_creation!
errors.add(:field_5, "Scheme code must relate to a location that is owned by owning organisation or managing organisation")
end
end
def location
return if scheme.nil?
@location ||= scheme.locations.find_by_id_on_mulitple_fields(field_5)
end
def validate_location_exists
if scheme && field_5.present? && location.nil?
errors.add(:field_5, "Location could be found with provided scheme code")
end
end
def validate_scheme_related def validate_scheme_related
return unless field_4.present? && scheme.present? return unless field_4.present? && scheme.present?
@ -587,6 +611,7 @@ private
attributes["managing_organisation_id"] = managing_organisation_id attributes["managing_organisation_id"] = managing_organisation_id
attributes["renewal"] = renewal attributes["renewal"] = renewal
attributes["scheme"] = scheme attributes["scheme"] = scheme
attributes["location"] = location
attributes["created_by"] = bulk_upload.user attributes["created_by"] = bulk_upload.user
attributes["needstype"] = bulk_upload.needstype attributes["needstype"] = bulk_upload.needstype
attributes["rent_type"] = rent_type attributes["rent_type"] = rent_type

5
spec/factories/location.rb

@ -10,6 +10,7 @@ FactoryBot.define do
startdate { Time.zone.local(2022, 4, 1) } startdate { Time.zone.local(2022, 4, 1) }
confirmed { true } confirmed { true }
scheme scheme
trait :export do trait :export do
postcode { "SW1A 2AA" } postcode { "SW1A 2AA" }
name { "Downing Street" } name { "Downing Street" }
@ -19,5 +20,9 @@ FactoryBot.define do
scheme { FactoryBot.create(:scheme, :export) } scheme { FactoryBot.create(:scheme, :export) }
old_visible_id { "111" } old_visible_id { "111" }
end end
trait :with_old_visible_id do
old_visible_id { rand(9_999_999).to_s }
end
end end
end end

64
spec/services/bulk_upload/lettings/row_parser_spec.rb

@ -12,6 +12,7 @@ RSpec.describe BulkUpload::Lettings::RowParser do
let(:owning_org) { create(:organisation, :with_old_visible_id) } let(:owning_org) { create(:organisation, :with_old_visible_id) }
let(:managing_org) { create(:organisation, :with_old_visible_id) } let(:managing_org) { create(:organisation, :with_old_visible_id) }
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) }
let(:location) { create(:location, :with_old_visible_id, scheme:) }
let(:setup_section_params) do let(:setup_section_params) do
{ {
@ -330,6 +331,59 @@ RSpec.describe BulkUpload::Lettings::RowParser do
end end
end end
describe "#field_5" do
context "when location does not exist" do
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) }
let(:attributes) do
{
bulk_upload:,
field_1: "1",
field_4: scheme.old_visible_id,
field_5: "dontexist",
field_111: owning_org.old_visible_id,
}
end
it "returns an error" do
expect(parser.errors[:field_5]).to be_present
end
end
context "when location exists" do
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) }
let(:attributes) do
{
bulk_upload:,
field_1: "1",
field_4: scheme.old_visible_id,
field_5: location.old_visible_id,
field_111: owning_org.old_visible_id,
}
end
it "does not return an error" do
expect(parser.errors[:field_5]).to be_blank
end
end
context "when location exists but not related" do
let(:location) { create(:scheme, :with_old_visible_id) }
let(:attributes) do
{
bulk_upload:,
field_1: "1",
field_4: scheme.old_visible_id,
field_5: location.old_visible_id,
field_111: owning_org.old_visible_id,
}
end
it "returns an error" do
expect(parser.errors[:field_5]).to be_present
end
end
end
describe "#field_7" do describe "#field_7" do
context "when null" do context "when null" do
let(:attributes) { { bulk_upload:, field_7: nil } } let(:attributes) { { bulk_upload:, field_7: nil } }
@ -620,6 +674,16 @@ RSpec.describe BulkUpload::Lettings::RowParser do
end end
describe "#log" do describe "#log" do
describe "#location" do
context "when lookup is via new core id" do
let(:attributes) { { bulk_upload:, field_4: scheme.old_visible_id, field_5: location.id, field_111: owning_org } }
it "assigns the correct location" do
expect(parser.log.location).to eql(location)
end
end
end
describe "#scheme" do describe "#scheme" do
context "when lookup is via id prefixed with S" do context "when lookup is via id prefixed with S" do
let(:attributes) { { bulk_upload:, field_4: "S#{scheme.id}", field_111: owning_org } } let(:attributes) { { bulk_upload:, field_4: "S#{scheme.id}", field_111: owning_org } }

Loading…
Cancel
Save