diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 17fd9af77..d8ebf9db3 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -92,6 +92,7 @@ private Date.new(0, 1, 1) end end + next unless question_params if %w[checkbox validation_override].include?(question.type) @@ -102,6 +103,12 @@ private result[question.id] = question_params end + if %w[checkbox].include?(question.type) && + !question.default_answer.nil? && + question.answer_keys_without_dividers.all? { |answer_key| result[answer_key] != 1 } + result[question.default_answer] = 1 + end + if question.id == "owning_organisation_id" owning_organisation = result["owning_organisation_id"].present? ? Organisation.find(result["owning_organisation_id"]) : nil if current_user.support? && @log.managing_organisation.blank? && owning_organisation&.managing_agents&.empty? @@ -229,6 +236,8 @@ private end def question_missing_response?(responses_for_page, question) + return unless question.default_answer.nil? + if %w[checkbox validation_override].include?(question.type) answered = question.answer_keys_without_dividers.map do |option| session["fields"][option] = @log[option] = params[@log.model_name.param_key][question.id].include?(option) ? 1 : 0 diff --git a/app/models/form/question.rb b/app/models/form/question.rb index eac75dc5c..35843cccd 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -1,6 +1,6 @@ class Form::Question attr_accessor :id, :header, :hint_text, :description, :questions, :disable_clearing_if_not_routed_or_dynamic_answer_options, - :type, :min, :max, :step, :width, :fields_to_add, :result_field, + :type, :min, :max, :step, :width, :fields_to_add, :result_field, :default_answer, :conditional_for, :readonly, :answer_options, :page, :check_answer_label, :inferred_answers, :hidden_in_check_answers, :inferred_check_answers_value, :top_guidance_partial, :bottom_guidance_partial, :prefix, :suffix, :requires_js, :fields_added, :derived, @@ -40,6 +40,7 @@ class Form::Question @plain_label = hsh["plain_label"] @error_label = hsh["error_label"] @disable_clearing_if_not_routed_or_dynamic_answer_options = hsh["disable_clearing_if_not_routed_or_dynamic_answer_options"] + @default_answer = hsh["default_answer"] end end diff --git a/app/models/form/sales/questions/buyers_organisations.rb b/app/models/form/sales/questions/buyers_organisations.rb index 1ab8a81e8..bb17c3d4d 100644 --- a/app/models/form/sales/questions/buyers_organisations.rb +++ b/app/models/form/sales/questions/buyers_organisations.rb @@ -5,8 +5,9 @@ class Form::Sales::Questions::BuyersOrganisations < ::Form::Question @check_answer_label = "Organisations buyers were registered with" @header = "What organisations were the buyers registered with?" @type = "checkbox" - @hint_text = "Select all that apply" + @hint_text = "Select all that apply. This question is optional. If no options are applicable, leave the options blank, and select save and continue." @answer_options = ANSWER_OPTIONS + @default_answer = "pregblank" @question_number = 59 end @@ -15,7 +16,7 @@ class Form::Sales::Questions::BuyersOrganisations < ::Form::Question "pregother" => { "value" => "Other private registered provider (PRP) - housing association" }, "pregla" => { "value" => "Local Authority" }, "pregghb" => { "value" => "Help to Buy Agent" }, - "pregblank" => { "value" => "None of the above" }, + "pregblank" => { "value" => "None selected" }, }.freeze def displayed_answer_options(_log, _user = nil) @@ -26,8 +27,4 @@ class Form::Sales::Questions::BuyersOrganisations < ::Form::Question "pregghb" => { "value" => "Help to Buy Agent" }, } end - - def unanswered_error_message - "At least one option must be selected of these four" - end end diff --git a/app/services/bulk_upload/sales/year2023/row_parser.rb b/app/services/bulk_upload/sales/year2023/row_parser.rb index a8f322828..4566ee852 100644 --- a/app/services/bulk_upload/sales/year2023/row_parser.rb +++ b/app/services/bulk_upload/sales/year2023/row_parser.rb @@ -469,7 +469,6 @@ class BulkUpload::Sales::Year2023::RowParser validate :validate_if_log_already_exists, on: :after_log, if: -> { FeatureToggle.bulk_upload_duplicate_log_check_enabled? } validate :validate_data_protection_answered, on: :after_log - validate :validate_buyers_organisations, on: :after_log def self.question_for_field(field) QUESTIONS[field] @@ -571,15 +570,6 @@ private end end - def validate_buyers_organisations - organisations_fields = %i[field_67 field_68 field_69 field_70] - if organisations_fields.all? { |field| attributes[field.to_s].blank? } - organisations_fields.each do |field| - errors.add(field, "At least one option must be selected of these four", category: :not_answered) - end - end - end - def prevtenbuy2 case field_72 when "R" @@ -864,6 +854,8 @@ private attributes["pregla"] = field_69 attributes["pregghb"] = field_70 attributes["pregother"] = field_68 + organisations_fields = %i[field_67 field_68 field_69 field_70] + attributes["pregblank"] = organisations_fields.all? { |field| attributes[field.to_s].blank? } attributes["disabled"] = field_76 attributes["wheel"] = field_77 diff --git a/app/services/bulk_upload/sales/year2024/row_parser.rb b/app/services/bulk_upload/sales/year2024/row_parser.rb index 2c1b6089a..e01841f92 100644 --- a/app/services/bulk_upload/sales/year2024/row_parser.rb +++ b/app/services/bulk_upload/sales/year2024/row_parser.rb @@ -468,8 +468,6 @@ class BulkUpload::Sales::Year2024::RowParser validate :validate_address_fields, on: :after_log validate :validate_if_log_already_exists, on: :after_log, if: -> { FeatureToggle.bulk_upload_duplicate_log_check_enabled? } - validate :validate_buyers_organisations, on: :after_log - def self.question_for_field(field) QUESTIONS[field] end @@ -564,15 +562,6 @@ class BulkUpload::Sales::Year2024::RowParser private - def validate_buyers_organisations - organisations_fields = %i[field_66 field_67 field_68 field_69] - if organisations_fields.all? { |field| attributes[field.to_s].blank? } - organisations_fields.each do |field| - errors.add(field, "At least one option must be selected of these four") - end - end - end - def prevtenbuy2 case field_71 when "R" @@ -860,6 +849,8 @@ private attributes["pregla"] = field_68 attributes["pregghb"] = field_69 attributes["pregother"] = field_67 + organisations_fields = %i[field_67 field_68 field_69 field_70] + attributes["pregblank"] = organisations_fields.all? { |field| attributes[field.to_s].blank? } attributes["disabled"] = field_75 attributes["wheel"] = field_76 diff --git a/spec/models/form/sales/questions/buyers_organisations_spec.rb b/spec/models/form/sales/questions/buyers_organisations_spec.rb index 88af9917d..9e772a24d 100644 --- a/spec/models/form/sales/questions/buyers_organisations_spec.rb +++ b/spec/models/form/sales/questions/buyers_organisations_spec.rb @@ -32,11 +32,11 @@ RSpec.describe Form::Sales::Questions::BuyersOrganisations, type: :model do end it "has the correct hint" do - expect(question.hint_text).to eq("Select all that apply") + expect(question.hint_text).to eq("Select all that apply. This question is optional. If no options are applicable, leave the options blank, and select save and continue.") end - it "has the correct unanswered_error_message" do - expect(question.unanswered_error_message).to eq("At least one option must be selected of these four") + it "has the correct default answer" do + expect(question.default_answer).to eq("pregblank") end it "has the correct answer_options" do @@ -46,7 +46,7 @@ RSpec.describe Form::Sales::Questions::BuyersOrganisations, type: :model do "pregother" => { "value" => "Other private registered provider (PRP) - housing association" }, "pregla" => { "value" => "Local Authority" }, "pregghb" => { "value" => "Help to Buy Agent" }, - "pregblank" => { "value" => "None of the above" }, + "pregblank" => { "value" => "None selected" }, }, ) end diff --git a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb index c4f4a26a6..8913a49d9 100644 --- a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb @@ -962,11 +962,8 @@ RSpec.describe BulkUpload::Sales::Year2023::RowParser do context "when all nil" do let(:attributes) { setup_section_params.merge(field_67: nil, field_68: nil, field_69: nil, field_70: nil) } - it "returns correct errors" do - expect(parser.errors[:field_67]).to be_present - expect(parser.errors[:field_68]).to be_present - expect(parser.errors[:field_69]).to be_present - expect(parser.errors[:field_70]).to be_present + it "sets pregblank field" do + expect(parser.log.pregblank).to be(1) end end end diff --git a/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb index 5a1a9e4df..68a420d44 100644 --- a/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb @@ -969,11 +969,8 @@ RSpec.describe BulkUpload::Sales::Year2024::RowParser do context "when all nil" do let(:attributes) { setup_section_params.merge(field_66: nil, field_67: nil, field_68: nil, field_69: nil) } - it "returns correct errors" do - expect(parser.errors[:field_66]).to be_present - expect(parser.errors[:field_67]).to be_present - expect(parser.errors[:field_68]).to be_present - expect(parser.errors[:field_69]).to be_present + it "sets pregblank field" do + expect(parser.log.pregblank).to be(1) end end end