From a1527e48f7df1d0bbafc225ab903ff876eb40d47 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 28 Sep 2023 14:12:32 +0100 Subject: [PATCH] Add duplicate log error if the chcharges are the same (#1942) --- .../lettings/year2022/row_parser.rb | 3 + .../lettings/year2023/row_parser.rb | 4 + .../lettings/year2022/row_parser_spec.rb | 154 ++++++++++++++++++ .../lettings/year2023/row_parser_spec.rb | 104 ++++++++++++ 4 files changed, 265 insertions(+) diff --git a/app/services/bulk_upload/lettings/year2022/row_parser.rb b/app/services/bulk_upload/lettings/year2022/row_parser.rb index 1f8839989..ce950b6db 100644 --- a/app/services/bulk_upload/lettings/year2022/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/row_parser.rb @@ -526,6 +526,7 @@ private "tcharge", bulk_upload.needstype != 2 ? "postcode_full" : nil, bulk_upload.needstype != 1 ? "location" : nil, + log.chcharge.present? ? "chcharge" : nil, ].compact end @@ -853,6 +854,8 @@ private errors.add(:field_20, error_message) # sex1 errors.add(:field_35, error_message) # ecstat1 errors.add(:field_84, error_message) # tcharge + errors.add(:field_85, error_message) if log.chcharge.present? # chcharge + errors.add(:field_86, error_message) if bulk_upload.needstype != 1 # household_charge errors.add(:field_96, error_message) # startdate errors.add(:field_97, error_message) # startdate errors.add(:field_98, error_message) # startdate diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index 9839846f8..9e7d8eecf 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -563,6 +563,7 @@ private field_4 != 2 ? "postcode_full" : nil, field_4 != 1 ? "location" : nil, "tenancycode", + log.chcharge.present? ? "chcharge" : nil, ].compact end @@ -858,6 +859,8 @@ private errors.add(:field_47, error_message) # sex1 errors.add(:field_50, error_message) # ecstat1 errors.add(:field_132, error_message) # tcharge + errors.add(:field_127, error_message) if log.chcharge.present? # chcharge + errors.add(:field_125, error_message) if bulk_upload.needstype != 1 # household_charge end end @@ -1170,6 +1173,7 @@ private attributes["supcharg"] = field_131 attributes["tcharge"] = field_132 attributes["chcharge"] = field_127 + attributes["is_carehome"] = field_127.present? ? 1 : 0 attributes["household_charge"] = field_125 attributes["hbrentshortfall"] = field_133 attributes["tshortfall_known"] = tshortfall_known 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 0a9f65d32..f699b730a 100644 --- a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb @@ -304,6 +304,160 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do end end + context "when a supported housing log with chcharges already exists in the db" do + let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } + let(:location) { create(:location, :with_old_visible_id, scheme:) } + let(:bulk_upload) { create(:bulk_upload, :lettings, user:, needstype: 2) } + let(:attributes) do + valid_attributes.merge({ field_4: scheme.old_visible_id, + field_1: "2", + field_5: location.old_visible_id, + field_111: owning_org.old_visible_id, + field_86: 0, + field_85: "88" }) + end + + before do + parser.log.save! + parser.instance_variable_set(:@valid, nil) + end + + it "is not a valid row" do + expect(parser).not_to be_valid + end + + it "adds an error to all the fields used to determine duplicates" do + parser.valid? + + [ + :field_5, # location + :field_7, # tenancycode + :field_12, # age1 + :field_20, # sex1 + :field_35, # ecstat1 + :field_85, # chcharge + :field_86, # household_charge + :field_96, # startdate + :field_97, # startdate + :field_98, # startdate + :field_111, # owning_organisation + ].each do |field| + expect(parser.errors[field]).to include("This is a duplicate log") + end + + expect(parser.errors[:field_109]).not_to include("This is a duplicate log") + expect(parser.errors[:field_108]).not_to include("This is a duplicate log") + end + end + + context "when a supported housing log with different chcharges already exists in the db" do + let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } + let(:location) { create(:location, :with_old_visible_id, scheme:) } + let(:bulk_upload) { create(:bulk_upload, :lettings, user:, needstype: 2) } + let(:attributes) do + valid_attributes.merge({ field_4: scheme.old_visible_id, + field_1: "2", + field_5: location.old_visible_id, + field_111: owning_org.old_visible_id, + field_86: 0, + field_117: 4, + field_85: 88 }) + end + + let(:attributes_too) do + valid_attributes.merge({ field_4: scheme.old_visible_id, + field_1: "2", + field_5: location.old_visible_id, + field_111: owning_org.old_visible_id, + field_86: 0, + field_117: 4, + field_85: 87 }) + end + let(:parser_too) { described_class.new(attributes_too) } + + before do + parser.log.save! + parser.instance_variable_set(:@valid, nil) + end + + it "is a valid row" do + expect(parser_too).to be_valid + end + + it "does not add an error to all the fields used to determine duplicates" do + parser_too.valid? + + [ + :field_5, # location + :field_7, # tenancycode + :field_12, # age1 + :field_20, # sex1 + :field_35, # ecstat1 + :field_85, # chcharge + :field_86, # household_charge + :field_96, # startdate + :field_97, # startdate + :field_98, # startdate + :field_111, # owning_organisation + ].each do |field| + expect(parser_too.errors[field]).not_to include("This is a duplicate log") + end + end + end + + context "when a supported housing log with different household_charge already exists in the db" do + let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } + let(:location) { create(:location, :with_old_visible_id, scheme:) } + let(:bulk_upload) { create(:bulk_upload, :lettings, user:, needstype: 2) } + let(:attributes) do + valid_attributes.merge({ field_4: scheme.old_visible_id, + field_1: "2", + field_5: location.old_visible_id, + field_111: owning_org.old_visible_id, + field_86: 1, + field_117: 4 }) + end + + let(:attributes_too) do + valid_attributes.merge({ field_4: scheme.old_visible_id, + field_1: "2", + field_5: location.old_visible_id, + field_111: owning_org.old_visible_id, + field_86: 0, + field_117: 4 }) + end + let(:parser_too) { described_class.new(attributes_too) } + + before do + parser.log.save! + parser.instance_variable_set(:@valid, nil) + end + + it "is a valid row" do + expect(parser_too).to be_valid + end + + it "does not add an error to all the fields used to determine duplicates" do + parser_too.valid? + + [ + :field_5, # location + :field_7, # tenancycode + :field_12, # age1 + :field_20, # sex1 + :field_35, # ecstat1 + :field_85, # chcharge + :field_86, # household_charge + :field_96, # startdate + :field_97, # startdate + :field_98, # startdate + :field_111, # owning_organisation + ].each do |field| + expect(parser_too.errors[field]).not_to include("This is a duplicate log") + end + end + end + context "when a hidden log already exists in db" do before do parser.log.status = "pending" 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 a3b9152c5..d532c8094 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -342,6 +342,110 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do end end + context "when a supported housing log with chcharges already exists in the db" do + let(:bulk_upload) { create(:bulk_upload, :lettings, user:, needstype: 2) } + let(:attributes) do + valid_attributes.merge({ field_16: scheme.old_visible_id, + field_4: "2", + field_5: "2", + field_17: location.old_visible_id, + field_1: owning_org.old_visible_id, + field_125: 0, + field_44: 4, + field_127: "88" }) + end + + before do + parser.log.save! + parser.instance_variable_set(:@valid, nil) + end + + it "is not a valid row" do + expect(parser).not_to be_valid + end + + it "adds an error to all the fields used to determine duplicates" do + parser.valid? + + error_message = "This is a duplicate log" + + [ + :field_1, # owning_organisation + :field_7, # startdate + :field_8, # startdate + :field_9, # startdate + :field_13, # tenancycode + :field_17, # location + :field_46, # age1 + :field_47, # sex1 + :field_50, # ecstat1 + :field_127, # chcharge + :field_125, # household_charge + ].each do |field| + expect(parser.errors[field]).to include(error_message) + end + + expect(parser.errors[:field_23]).not_to include(error_message) + expect(parser.errors[:field_24]).not_to include(error_message) + expect(parser.errors[:field_25]).not_to include(error_message) + end + end + + context "when a supported housing log different chcharges already exists in the db" do + let(:bulk_upload) { create(:bulk_upload, :lettings, user:, needstype: 2) } + let(:attributes) do + valid_attributes.merge({ field_16: scheme.old_visible_id, + field_4: "2", + field_5: "2", + field_17: location.old_visible_id, + field_1: owning_org.old_visible_id, + field_125: 0, + field_44: 4, + field_127: "88" }) + end + let(:attributes_too) do + valid_attributes.merge({ field_16: scheme.old_visible_id, + field_4: "2", + field_5: "2", + field_17: location.old_visible_id, + field_1: owning_org.old_visible_id, + field_125: 0, + field_44: 4, + field_127: "98" }) + end + let(:parser_too) { described_class.new(attributes_too) } + + before do + parser.log.save! + parser.instance_variable_set(:@valid, nil) + end + + it "is a valid row" do + expect(parser_too).to be_valid + end + + it "does not add an error to all the fields used to determine duplicates" do + parser_too.valid? + + error_message = "This is a duplicate log" + + [ + :field_1, # owning_organisation + :field_7, # startdate + :field_8, # startdate + :field_9, # startdate + :field_13, # tenancycode + :field_17, # location + :field_46, # age1 + :field_47, # sex1 + :field_50, # ecstat1 + :field_132, # tcharge + ].each do |field| + expect(parser_too.errors[field]).not_to include(error_message) + end + end + end + context "when a hidden log already exists in db" do before do parser.log.status = "pending"