diff --git a/app/models/form/sales/pages/mortgage_length.rb b/app/models/form/sales/pages/mortgage_length.rb index 979369bdd..55b286bd3 100644 --- a/app/models/form/sales/pages/mortgage_length.rb +++ b/app/models/form/sales/pages/mortgage_length.rb @@ -8,6 +8,7 @@ class Form::Sales::Pages::MortgageLength < ::Form::Page def questions @questions ||= [ + Form::Sales::Questions::MortgageLengthKnown.new(nil, nil, self), Form::Sales::Questions::MortgageLength.new(nil, nil, self), ] end diff --git a/app/models/form/sales/questions/mortgage_length.rb b/app/models/form/sales/questions/mortgage_length.rb index 3f1603b75..45b1ebb25 100644 --- a/app/models/form/sales/questions/mortgage_length.rb +++ b/app/models/form/sales/questions/mortgage_length.rb @@ -10,5 +10,9 @@ class Form::Sales::Questions::MortgageLength < ::Form::Question @width = 5 @suffix = " years" @hint_text = "You should round up to the nearest year. Value should not exceed 60 years." + @inferred_check_answers_value = [{ + "condition" => { "mortlen_known" => 1 }, + "value" => "Not known", + }] end end diff --git a/app/models/form/sales/questions/mortgage_length_known.rb b/app/models/form/sales/questions/mortgage_length_known.rb new file mode 100644 index 000000000..3455d17a8 --- /dev/null +++ b/app/models/form/sales/questions/mortgage_length_known.rb @@ -0,0 +1,28 @@ +class Form::Sales::Questions::MortgageLengthKnown < ::Form::Question + def initialize(id, hsh, page) + super + @id = "mortlen_known" + @check_answer_label = "Mortgage length known" + @header = "Do you know the mortgage length?" + @type = "radio" + @answer_options = ANSWER_OPTIONS + @conditional_for = { + "mortlen" => [0], + } + @hidden_in_check_answers = { + "depends_on" => [ + { + "mortlen_known" => 0, + }, + { + "mortlen_known" => 1, + }, + ], + } + end + + ANSWER_OPTIONS = { + "0" => { "value" => "Yes" }, + "1" => { "value" => "No" }, + }.freeze +end diff --git a/app/services/imports/sales_logs_import_service.rb b/app/services/imports/sales_logs_import_service.rb index deea3fab6..440ae5f71 100644 --- a/app/services/imports/sales_logs_import_service.rb +++ b/app/services/imports/sales_logs_import_service.rb @@ -118,6 +118,7 @@ module Imports attributes["mortgagelender"] = mortgage_lender(xml_doc, attributes) attributes["mortgagelenderother"] = mortgage_lender_other(xml_doc, attributes) attributes["mortlen"] = mortgage_length(xml_doc, attributes) + attributes["mortlen_known"] = 0 if attributes["mortlen"].present? attributes["extrabor"] = borrowing(xml_doc, attributes) attributes["totadult"] = safe_string_as_integer(xml_doc, "TOTADULT") # would get overridden attributes["totchild"] = safe_string_as_integer(xml_doc, "TOTCHILD") # would get overridden @@ -404,6 +405,7 @@ module Imports attributes["hb"] ||= 4 attributes["prevown"] ||= 3 attributes["savingsnk"] ||= attributes["savings"].present? ? 0 : 1 + attributes["mortlen_known"] ||= 1 if attributes["mortgage"].blank? # attributes["noint"] = 1 # not interviewed # buyer 1 characteristics diff --git a/db/migrate/20230228112317_add_mortgage_length_known.rb b/db/migrate/20230228112317_add_mortgage_length_known.rb new file mode 100644 index 000000000..7d2d8a0b2 --- /dev/null +++ b/db/migrate/20230228112317_add_mortgage_length_known.rb @@ -0,0 +1,5 @@ +class AddMortgageLengthKnown < ActiveRecord::Migration[7.0] + def change + add_column :sales_logs, :mortlen_known, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index d1668b738..be59aaf92 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_02_15_112932) do +ActiveRecord::Schema[7.0].define(version: 2023_02_28_112317) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -490,7 +490,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_02_15_112932) do t.integer "prevten" t.integer "mortgageused" t.integer "wchair" - t.integer "income2_value_check" t.integer "armedforcesspouse" t.datetime "hodate", precision: nil t.integer "hoday" @@ -527,10 +526,12 @@ ActiveRecord::Schema[7.0].define(version: 2023_02_15_112932) do t.integer "details_known_6" t.integer "ethnic_group2" t.integer "ethnicbuy2" - t.integer "proplen_asked" t.integer "prevshared" t.integer "staircasesale" t.string "old_id" + t.integer "income2_value_check" + t.integer "proplen_asked" + t.integer "mortlen_known" t.index ["bulk_upload_id"], name: "index_sales_logs_on_bulk_upload_id" t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id" t.index ["old_id"], name: "index_sales_logs_on_old_id", unique: true diff --git a/spec/factories/sales_log.rb b/spec/factories/sales_log.rb index f1da841a4..d2294dbd9 100644 --- a/spec/factories/sales_log.rb +++ b/spec/factories/sales_log.rb @@ -95,6 +95,7 @@ FactoryBot.define do armedforcesspouse { 5 } mscharge_known { 1 } mscharge { 100 } + mortlen_known { 0 } mortlen { 10 } pcodenk { 1 } is_la_inferred { false } diff --git a/spec/models/form/sales/pages/mortgage_length_spec.rb b/spec/models/form/sales/pages/mortgage_length_spec.rb index ef2573e57..394ec94ab 100644 --- a/spec/models/form/sales/pages/mortgage_length_spec.rb +++ b/spec/models/form/sales/pages/mortgage_length_spec.rb @@ -12,7 +12,7 @@ RSpec.describe Form::Sales::Pages::MortgageLength, type: :model do end it "has correct questions" do - expect(page.questions.map(&:id)).to eq(%w[mortlen]) + expect(page.questions.map(&:id)).to eq(%w[mortlen_known mortlen]) end it "has the correct id" do diff --git a/spec/models/form/sales/questions/mortgage_length_known_spec.rb b/spec/models/form/sales/questions/mortgage_length_known_spec.rb new file mode 100644 index 000000000..b58a142d5 --- /dev/null +++ b/spec/models/form/sales/questions/mortgage_length_known_spec.rb @@ -0,0 +1,55 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Questions::MortgageLengthKnown, type: :model do + subject(:question) { described_class.new(question_id, question_definition, page) } + + let(:question_id) { nil } + let(:question_definition) { nil } + let(:page) { instance_double(Form::Page) } + + it "has correct page" do + expect(question.page).to eq(page) + end + + it "has the correct id" do + expect(question.id).to eq("mortlen_known") + end + + it "has the correct header" do + expect(question.header).to eq("Do you know the mortgage length?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Mortgage length known") + end + + it "has the correct type" do + expect(question.type).to eq("radio") + end + + it "is not marked as derived" do + expect(question.derived?).to be false + end + + it "has the correct answer_options" do + expect(question.answer_options).to eq({ + "0" => { "value" => "Yes" }, + "1" => { "value" => "No" }, + }) + end + + it "has correct conditional for" do + expect(question.conditional_for).to eq({ + "mortlen" => [0], + }) + end + + it "has correct hidden in check answers" do + expect(question.hidden_in_check_answers).to eq({ + "depends_on" => [{ + "mortlen_known" => 0, + }, + { "mortlen_known" => 1 }], + }) + end +end diff --git a/spec/models/form/sales/questions/mortgage_length_spec.rb b/spec/models/form/sales/questions/mortgage_length_spec.rb index 0f3193244..75db84658 100644 --- a/spec/models/form/sales/questions/mortgage_length_spec.rb +++ b/spec/models/form/sales/questions/mortgage_length_spec.rb @@ -52,4 +52,13 @@ RSpec.describe Form::Sales::Questions::MortgageLength, type: :model do it "has correct max" do expect(question.max).to eq(60) end + + it "has correct inferred check answers value" do + expect(question.inferred_check_answers_value).to eq([ + { + "condition" => { "mortlen_known" => 1 }, + "value" => "Not known", + }, + ]) + end end