From 21d1531aa1f708f8bcdb96ce0da5cbb5450bc8b8 Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Mon, 9 Jan 2023 12:32:49 +0000 Subject: [PATCH] add allocations system to bulk upload --- .../bulk_upload/lettings/row_parser.rb | 33 +++++++- .../bulk_upload/lettings/row_parser_spec.rb | 81 ++++++++++++++++++- 2 files changed, 109 insertions(+), 5 deletions(-) diff --git a/app/services/bulk_upload/lettings/row_parser.rb b/app/services/bulk_upload/lettings/row_parser.rb index 373a59ad4..fba6a0de0 100644 --- a/app/services/bulk_upload/lettings/row_parser.rb +++ b/app/services/bulk_upload/lettings/row_parser.rb @@ -173,6 +173,10 @@ class BulkUpload::Lettings::RowParser errors.blank? end + def log + @log ||= LettingsLog.new(attributes_for_log) + end + private def postcode_full @@ -191,10 +195,6 @@ private log.form.subsections.flat_map { |ss| ss.applicable_questions(log) } end - def log - @log ||= LettingsLog.new(attributes_for_log) - end - def validate_nulls field_mapping_for_errors.each do |error_key, fields| question_id = error_key.to_s @@ -281,6 +281,10 @@ private rp_medwel: %i[field_72], rp_hardship: %i[field_73], rp_dontknow: %i[field_74], + + cbl: %i[field_75], + chr: %i[field_76], + cap: %i[field_77], } end @@ -404,9 +408,30 @@ private attributes["rp_hardship"] = field_73 attributes["rp_dontknow"] = field_74 + attributes["cbl"] = cbl + attributes["chr"] = chr + attributes["cap"] = cap + attributes["letting_allocation_unknown"] = letting_allocation_unknown + attributes end + def letting_allocation_unknown + [cbl, chr, cap].all?(0) ? 1 : 0 + end + + def cbl + field_75 == 2 ? 0 : field_75 + end + + def chr + field_76 == 2 ? 0 : field_76 + end + + def cap + field_77 == 2 ? 0 : field_77 + end + def ppostcode_full "#{field_63} #{field_64}".strip.gsub(/\s+/, " ") end diff --git a/spec/services/bulk_upload/lettings/row_parser_spec.rb b/spec/services/bulk_upload/lettings/row_parser_spec.rb index f6d8be7f9..97ae1cd9a 100644 --- a/spec/services/bulk_upload/lettings/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/row_parser_spec.rb @@ -110,6 +110,10 @@ RSpec.describe BulkUpload::Lettings::RowParser do field_72: "1", field_73: "", field_74: "", + + field_75: "1", + field_76: "2", + field_77: "2", } end @@ -117,11 +121,12 @@ RSpec.describe BulkUpload::Lettings::RowParser do expect(parser).to be_valid end - it "instantiates a log with everything completed" do + it "instantiates a log with everything completed", aggregate_failures: true do questions = parser.send(:questions).reject do |q| parser.send(:log).optional_fields.include?(q.id) || q.completed?(parser.send(:log)) end + expect(questions.map(&:id).size).to be(0) expect(questions.map(&:id)).to eql([]) end end @@ -237,4 +242,78 @@ RSpec.describe BulkUpload::Lettings::RowParser do end end end + + describe "#log" do + describe "#cbl" do + context "when field_75 is yes ie 1" do + let(:attributes) { { bulk_upload:, field_75: 1 } } + + it "sets value to 1" do + expect(parser.log.cbl).to be(1) + end + end + + context "when field_75 is no ie 2" do + let(:attributes) { { bulk_upload:, field_75: 2 } } + + it "sets value to 0" do + expect(parser.log.cbl).to be(0) + end + end + end + + describe "#chr" do + context "when field_76 is yes ie 1" do + let(:attributes) { { bulk_upload:, field_76: 1 } } + + it "sets value to 1" do + expect(parser.log.chr).to be(1) + end + end + + context "when field_76 is no ie 2" do + let(:attributes) { { bulk_upload:, field_76: 2 } } + + it "sets value to 0" do + expect(parser.log.chr).to be(0) + end + end + end + + describe "#cap" do + context "when field_77 is yes ie 1" do + let(:attributes) { { bulk_upload:, field_77: 1 } } + + it "sets value to 1" do + expect(parser.log.cap).to be(1) + end + end + + context "when field_77 is no ie 2" do + let(:attributes) { { bulk_upload:, field_77: 2 } } + + it "sets value to 0" do + expect(parser.log.cap).to be(0) + end + end + end + + describe "#letting_allocation_unknown" do + context "when field_75, 76, 77 are no ie 2" do + let(:attributes) { { bulk_upload:, field_75: 2, field_76: 2, field_77: 2 } } + + it "sets value to 1" do + expect(parser.log.letting_allocation_unknown).to be(1) + end + end + + context "when any one of field_75, 76, 77 is yes ie 1" do + let(:attributes) { { bulk_upload:, field_75: 1 } } + + it "sets value to 0" do + expect(parser.log.letting_allocation_unknown).to be(0) + end + end + end + end end