Browse Source

map schemes and locations correctly

pull/1417/head
Phil Lee 3 years ago
parent
commit
f4c9ce0cfd
  1. 35
      app/services/bulk_upload/lettings/year2023/row_parser.rb
  2. 51
      spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb

35
app/services/bulk_upload/lettings/year2023/row_parser.rb

@ -281,7 +281,7 @@ class BulkUpload::Lettings::Year2023::RowParser
validates :field_5, presence: { message: I18n.t("validations.not_answered", question: "letting type") }, validates :field_5, presence: { message: I18n.t("validations.not_answered", question: "letting type") },
inclusion: { in: (1..12).to_a, message: I18n.t("validations.invalid_option", question: "letting type") } inclusion: { in: (1..12).to_a, message: I18n.t("validations.invalid_option", question: "letting type") }
validates :field_15, presence: { if: proc { [2, 4, 6, 8, 10, 12].include?(field_5) } } validates :field_16, presence: { if: proc { [2, 4, 6, 8, 10, 12].include?(field_5) } }
validates :field_46, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 1 must be a number or the letter R" } validates :field_46, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 1 must be a number or the letter R" }
validates :field_52, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 2 must be a number or the letter R" } validates :field_52, format: { with: /\A\d{1,3}\z|\AR\z/, message: "Age of person 2 must be a number or the letter R" }
@ -379,7 +379,7 @@ private
def validate_needs_type_present def validate_needs_type_present
if field_4.blank? if field_4.blank?
errors.add(:field_4, I18n.t("validations.not_answered", question: "what is the needs type?"), category: :setup) errors.add(:field_4, I18n.t("validations.not_answered", question: "needs type"))
end end
end end
@ -500,43 +500,43 @@ private
unless location.scheme == scheme unless location.scheme == scheme
block_log_creation! block_log_creation!
errors.add(:field_16, "Scheme code must relate to a location that is owned by owning organisation or managing organisation") errors.add(:field_17, "Scheme code must relate to a location that is owned by owning organisation or managing organisation")
end end
end end
def validate_location_exists def validate_location_exists
if scheme && field_16.present? && location.nil? if scheme && field_17.present? && location.nil?
errors.add(:field_16, "Location could be found with provided scheme code") errors.add(:field_17, "Location could be found with provided scheme code")
end end
end end
def validate_location_data_given def validate_location_data_given
if bulk_upload.supported_housing? && field_16.blank? if supported_housing? && field_17.blank?
errors.add(:field_16, "The scheme code must be present", category: "setup") errors.add(:field_17, "The scheme code must be present", category: "setup")
end end
end end
def validate_scheme_related def validate_scheme_related
return unless field_15.present? && scheme.present? return unless field_16.present? && scheme.present?
owned_by_owning_org = owning_organisation && scheme.owning_organisation == owning_organisation owned_by_owning_org = owning_organisation && scheme.owning_organisation == owning_organisation
owned_by_managing_org = managing_organisation && scheme.owning_organisation == managing_organisation owned_by_managing_org = managing_organisation && scheme.owning_organisation == managing_organisation
unless owned_by_owning_org || owned_by_managing_org unless owned_by_owning_org || owned_by_managing_org
block_log_creation! block_log_creation!
errors.add(:field_15, "This management group code does not belong to your organisation, or any of your stock owners / managing agents") errors.add(:field_16, "This management group code does not belong to your organisation, or any of your stock owners / managing agents")
end end
end end
def validate_scheme_exists def validate_scheme_exists
if field_15.present? && scheme.nil? if field_16.present? && scheme.nil?
errors.add(:field_15, "The management group code is not correct") errors.add(:field_16, "The management group code is not correct")
end end
end end
def validate_scheme_data_given def validate_scheme_data_given
if bulk_upload.supported_housing? && field_15.blank? if supported_housing? && field_16.blank?
errors.add(:field_15, "The management group code is not correct", category: "setup") errors.add(:field_16, "The management group code is not correct", category: "setup")
end end
end end
@ -605,9 +605,10 @@ private
owning_organisation_id: [:field_1], owning_organisation_id: [:field_1],
managing_organisation_id: [:field_2], managing_organisation_id: [:field_2],
renewal: [:field_6], renewal: [:field_6],
scheme: %i[field_15 field_16], scheme: %i[field_16],
location: %i[field_17],
created_by: [], created_by: [],
needstype: [], needstype: [:field_4],
rent_type: %i[field_5 field_10 field_11], rent_type: %i[field_5 field_10 field_11],
startdate: %i[field_7 field_8 field_9], startdate: %i[field_7 field_8 field_9],
unittype_gn: %i[field_29], unittype_gn: %i[field_29],
@ -979,13 +980,13 @@ private
end end
def scheme def scheme
@scheme ||= Scheme.find_by_id_on_mulitple_fields(field_15) @scheme ||= Scheme.find_by_id_on_mulitple_fields(field_16)
end end
def location def location
return if scheme.nil? return if scheme.nil?
@location ||= scheme.locations.find_by_id_on_mulitple_fields(field_16) @location ||= scheme.locations.find_by_id_on_mulitple_fields(field_17)
end end
def renttype def renttype

51
spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb

@ -88,7 +88,6 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
{ {
bulk_upload:, bulk_upload:,
field_5: "1", field_5: "1",
field_15: scheme.old_visible_id,
field_13: "123", field_13: "123",
field_7: now.day.to_s, field_7: now.day.to_s,
field_8: now.month.to_s, field_8: now.month.to_s,
@ -299,17 +298,17 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
end end
end end
describe "#field_15" do describe "#field_16" do
context "when nullable not permitted" do context "when nullable not permitted" do
let(:attributes) { { bulk_upload:, field_5: "2", field_15: nil } } let(:attributes) { { bulk_upload:, field_5: "2", field_16: nil } }
it "cannot be nulled" do it "cannot be nulled" do
expect(parser.errors[:field_15]).to be_present expect(parser.errors[:field_16]).to be_present
end end
end end
context "when nullable permitted" do context "when nullable permitted" do
let(:attributes) { { bulk_upload:, field_5: "1", field_15: nil } } let(:attributes) { { bulk_upload:, field_5: "1", field_16: nil } }
it "can be nulled" do it "can be nulled" do
expect(parser.errors[:field_15]).to be_blank expect(parser.errors[:field_15]).to be_blank
@ -317,56 +316,56 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
end end
context "when matching scheme cannot be found" do context "when matching scheme cannot be found" do
let(:attributes) { { bulk_upload:, field_5: "1", field_15: "123" } } let(:attributes) { { bulk_upload:, field_5: "1", field_16: "123" } }
it "returns an error" do it "returns an error" do
expect(parser.errors[:field_15]).to be_present expect(parser.errors[:field_16]).to be_present
end end
end end
context "when scheme belongs to someone else" do context "when scheme belongs to someone else" do
let(:other_scheme) { create(:scheme, :with_old_visible_id) } let(:other_scheme) { create(:scheme, :with_old_visible_id) }
let(:attributes) { { bulk_upload:, field_5: "1", field_15: other_scheme.old_visible_id, field_1: owning_org.old_visible_id } } let(:attributes) { { bulk_upload:, field_5: "1", field_16: other_scheme.old_visible_id, field_1: owning_org.old_visible_id } }
it "returns an error" do it "returns an error" do
expect(parser.errors[:field_15]).to include("This management group code does not belong to your organisation, or any of your stock owners / managing agents") expect(parser.errors[:field_16]).to include("This management group code does not belong to your organisation, or any of your stock owners / managing agents")
end end
end end
context "when scheme belongs to owning org" do context "when scheme belongs to owning org" do
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(:attributes) { { bulk_upload:, field_5: "1", field_15: scheme.old_visible_id, field_1: owning_org.old_visible_id } } let(:attributes) { { bulk_upload:, field_5: "1", field_16: scheme.old_visible_id, field_1: owning_org.old_visible_id } }
it "does not return an error" do it "does not return an error" do
expect(parser.errors[:field_15]).to be_blank expect(parser.errors[:field_16]).to be_blank
end end
end end
context "when scheme belongs to managing org" do context "when scheme belongs to managing org" do
let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: managing_org) } let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: managing_org) }
let(:attributes) { { bulk_upload:, field_5: "1", field_15: scheme.old_visible_id, field_2: managing_org.old_visible_id } } let(:attributes) { { bulk_upload:, field_5: "1", field_16: scheme.old_visible_id, field_2: managing_org.old_visible_id } }
it "does not return an error" do it "does not return an error" do
expect(parser.errors[:field_15]).to be_blank expect(parser.errors[:field_16]).to be_blank
end end
end end
end end
describe "#field_16" do describe "#field_17" do
context "when location does not exist" do context "when location does not exist" do
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(:attributes) do let(:attributes) do
{ {
bulk_upload:, bulk_upload:,
field_5: "1", field_5: "1",
field_15: scheme.old_visible_id, field_16: scheme.old_visible_id,
field_16: "dontexist", field_17: "dontexist",
field_1: owning_org.old_visible_id, field_1: owning_org.old_visible_id,
} }
end end
it "returns an error" do it "returns an error" do
expect(parser.errors[:field_16]).to be_present expect(parser.errors[:field_17]).to be_present
end end
end end
@ -376,14 +375,14 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
{ {
bulk_upload:, bulk_upload:,
field_5: "1", field_5: "1",
field_15: scheme.old_visible_id, field_16: scheme.old_visible_id,
field_16: location.old_visible_id, field_17: location.old_visible_id,
field_1: owning_org.old_visible_id, field_1: owning_org.old_visible_id,
} }
end end
it "does not return an error" do it "does not return an error" do
expect(parser.errors[:field_16]).to be_blank expect(parser.errors[:field_17]).to be_blank
end end
end end
@ -393,14 +392,14 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
{ {
bulk_upload:, bulk_upload:,
field_5: "1", field_5: "1",
field_15: scheme.old_visible_id, field_16: scheme.old_visible_id,
field_16: location.old_visible_id, field_17: location.old_visible_id,
field_1: owning_org.old_visible_id, field_1: owning_org.old_visible_id,
} }
end end
it "returns an error" do it "returns an error" do
expect(parser.errors[:field_16]).to be_present expect(parser.errors[:field_17]).to be_present
end end
end end
end end
@ -650,7 +649,7 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
error = errors.find { |e| e.attribute == :field_4 } error = errors.find { |e| e.attribute == :field_4 }
expect(error).to be_present expect(error).to be_present
expect(error.type).to eql("You must answer what is the needs type?") expect(error.type).to eql("You must answer needs type")
end end
end end
end end
@ -728,7 +727,7 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
describe "#location" do describe "#location" do
context "when lookup is via new core id" do context "when lookup is via new core id" do
let(:attributes) { { bulk_upload:, field_15: scheme.old_visible_id, field_16: location.id, field_1: owning_org } } let(:attributes) { { bulk_upload:, field_16: scheme.old_visible_id, field_17: location.id, field_1: owning_org } }
it "assigns the correct location" do it "assigns the correct location" do
expect(parser.log.location).to eql(location) expect(parser.log.location).to eql(location)
@ -738,7 +737,7 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do
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_15: "S#{scheme.id}", field_1: owning_org } } let(:attributes) { { bulk_upload:, field_16: "S#{scheme.id}", field_1: owning_org } }
it "assigns the correct scheme" do it "assigns the correct scheme" do
expect(parser.log.scheme).to eql(scheme) expect(parser.log.scheme).to eql(scheme)

Loading…
Cancel
Save