diff --git a/app/models/form/question.rb b/app/models/form/question.rb index 5b4440e39..aa9689295 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -129,6 +129,12 @@ class Form::Question "/#{log.model_name.param_key.dasherize}s/#{log.id}/#{page_id.to_s.dasherize}?referrer=check_answers" end + def unanswered?(log) + return answer_options.keys.none? { |key| value_is_yes?(log[key]) } if type == "checkbox" + + log[id].blank? + end + def completed?(log) return answer_options.keys.any? { |key| value_is_yes?(log[key]) } if type == "checkbox" diff --git a/app/models/form/sales/pages/buyers_organisations.rb b/app/models/form/sales/pages/buyers_organisations.rb new file mode 100644 index 000000000..cc6f82984 --- /dev/null +++ b/app/models/form/sales/pages/buyers_organisations.rb @@ -0,0 +1,15 @@ +class Form::Sales::Pages::BuyersOrganisations < ::Form::Page + def initialize(id, hsh, subsection) + super + @id = "buyers_organisations" + @header = "" + @description = "" + @subsection = subsection + end + + def questions + @questions ||= [ + Form::Sales::Questions::BuyersOrganisations.new(nil, nil, self), + ] + end +end diff --git a/app/models/form/sales/questions/buyers_organisations.rb b/app/models/form/sales/questions/buyers_organisations.rb new file mode 100644 index 000000000..dd3d87bd0 --- /dev/null +++ b/app/models/form/sales/questions/buyers_organisations.rb @@ -0,0 +1,19 @@ +class Form::Sales::Questions::BuyersOrganisations < ::Form::Question + def initialize(id, hsh, page) + super + @id = "buyers_organisations" + @check_answer_label = "Organisations buyers were registered with" + @header = "What organisations were the buyers registered with?" + @type = "checkbox" + @hint_text = "Select all that apply" + @page = page + @answer_options = ANSWER_OPTIONS + end + + ANSWER_OPTIONS = { + "pregyrha" => { "value" => "Their private registered provider (PRP) - housing association" }, + "pregother" => { "value" => "Other private registered provider (PRP) - housing association" }, + "pregla" => { "value" => "Local Authority" }, + "pregghb" => { "value" => "Help to Buy Agent" }, + }.freeze +end diff --git a/app/models/form/sales/sections/household.rb b/app/models/form/sales/sections/household.rb index e6f71a26d..3c7eae085 100644 --- a/app/models/form/sales/sections/household.rb +++ b/app/models/form/sales/sections/household.rb @@ -7,6 +7,7 @@ class Form::Sales::Sections::Household < ::Form::Section @form = form @subsections = [ Form::Sales::Subsections::HouseholdCharacteristics.new(nil, nil, self), + Form::Sales::Subsections::HouseholdSituation.new(nil, nil, self), Form::Sales::Subsections::HouseholdNeeds.new(nil, nil, self), ] end diff --git a/app/models/form/sales/subsections/household_situation.rb b/app/models/form/sales/subsections/household_situation.rb new file mode 100644 index 000000000..2be48f68c --- /dev/null +++ b/app/models/form/sales/subsections/household_situation.rb @@ -0,0 +1,15 @@ +class Form::Sales::Subsections::HouseholdSituation < ::Form::Subsection + def initialize(id, hsh, section) + super + @id = "household_situation" + @label = "Household situation" + @section = section + @depends_on = [{ "setup_completed?" => true }] + end + + def pages + @pages ||= [ + Form::Sales::Pages::BuyersOrganisations.new(nil, nil, self), + ] + end +end diff --git a/app/models/form/subsection.rb b/app/models/form/subsection.rb index 0266fc448..d51f8434d 100644 --- a/app/models/form/subsection.rb +++ b/app/models/form/subsection.rb @@ -28,7 +28,7 @@ class Form::Subsection qs = applicable_questions(log) qs_optional_removed = qs.reject { |q| log.optional_fields.include?(q.id) } - return :not_started if qs.count.positive? && qs.all? { |question| log[question.id].blank? || question.read_only? || question.derived? } + return :not_started if qs.count.positive? && qs.all? { |question| question.unanswered?(log) || question.read_only? || question.derived? } return :completed if qs_optional_removed.all? { |question| question.completed?(log) } :in_progress diff --git a/db/migrate/20221222153059_add_buyers_organisations_to_sales_log.rb b/db/migrate/20221222153059_add_buyers_organisations_to_sales_log.rb new file mode 100644 index 000000000..4781fd7b3 --- /dev/null +++ b/db/migrate/20221222153059_add_buyers_organisations_to_sales_log.rb @@ -0,0 +1,10 @@ +class AddBuyersOrganisationsToSalesLog < ActiveRecord::Migration[7.0] + def change + change_table :sales_logs, bulk: true do |t| + t.column :pregyrha, :int + t.column :pregla, :int + t.column :pregghb, :int + t.column :pregother, :integer + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ee62f5656..550d966b3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -427,6 +427,10 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_04_093057) do t.integer "resale" t.decimal "deposit", precision: 10, scale: 2 t.decimal "cashdis", precision: 10, scale: 2 + t.integer "pregyrha" + t.integer "pregla" + t.integer "pregghb" + t.integer "pregother" t.integer "disabled" t.integer "lanomagr" t.integer "soctenant" diff --git a/spec/factories/sales_log.rb b/spec/factories/sales_log.rb index ffe3c852a..71e020f8c 100644 --- a/spec/factories/sales_log.rb +++ b/spec/factories/sales_log.rb @@ -73,6 +73,10 @@ FactoryBot.define do value { 110_000 } grant { 1_000 } proplen { 10 } + pregyrha { 1 } + pregla { 1 } + pregother { 1 } + pregghb { 1 } end end end diff --git a/spec/models/form/sales/pages/buyers_organisations_spec.rb b/spec/models/form/sales/pages/buyers_organisations_spec.rb new file mode 100644 index 000000000..f245fe7f5 --- /dev/null +++ b/spec/models/form/sales/pages/buyers_organisations_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Pages::BuyersOrganisations, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { nil } + let(:page_definition) { nil } + let(:subsection) { instance_double(Form::Subsection) } + + it "has correct subsection" do + expect(page.subsection).to eq(subsection) + end + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[buyers_organisations]) + end + + it "has the correct id" do + expect(page.id).to eq("buyers_organisations") + end + + it "has the correct header" do + expect(page.header).to eq("") + end + + it "has the correct description" do + expect(page.description).to eq("") + end + + it "has correct depends_on" do + expect(page.depends_on).to be nil + end +end diff --git a/spec/models/form/sales/questions/buyers_organisations_spec.rb b/spec/models/form/sales/questions/buyers_organisations_spec.rb new file mode 100644 index 000000000..14bb944f4 --- /dev/null +++ b/spec/models/form/sales/questions/buyers_organisations_spec.rb @@ -0,0 +1,48 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Questions::BuyersOrganisations, 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("buyers_organisations") + end + + it "has the correct header" do + expect(question.header).to eq("What organisations were the buyers registered with?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Organisations buyers were registered with") + end + + it "has the correct type" do + expect(question.type).to eq("checkbox") + end + + it "is not marked as derived" do + expect(question.derived?).to be false + end + + it "has the correct hint" do + expect(question.hint_text).to eq("Select all that apply") + end + + it "has the correct answer_options" do + expect(question.answer_options).to eq( + { + "pregyrha" => { "value" => "Their private registered provider (PRP) - housing association" }, + "pregother" => { "value" => "Other private registered provider (PRP) - housing association" }, + "pregla" => { "value" => "Local Authority" }, + "pregghb" => { "value" => "Help to Buy Agent" }, + }, + ) + end +end diff --git a/spec/models/form/sales/sections/household_spec.rb b/spec/models/form/sales/sections/household_spec.rb index e415e8f0c..cff166510 100644 --- a/spec/models/form/sales/sections/household_spec.rb +++ b/spec/models/form/sales/sections/household_spec.rb @@ -15,6 +15,7 @@ RSpec.describe Form::Sales::Sections::Household, type: :model do expect(household.subsections.map(&:id)).to eq( %w[ household_characteristics + household_situation household_needs ], ) diff --git a/spec/models/form/sales/subsections/household_needs_spec.rb b/spec/models/form/sales/subsections/household_needs_spec.rb new file mode 100644 index 000000000..f7b139ee5 --- /dev/null +++ b/spec/models/form/sales/subsections/household_needs_spec.rb @@ -0,0 +1,34 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Subsections::HouseholdNeeds, type: :model do + subject(:household_characteristics) { described_class.new(subsection_id, subsection_definition, section) } + + let(:subsection_id) { nil } + let(:subsection_definition) { nil } + let(:section) { instance_double(Form::Sales::Sections::Household) } + + it "has correct section" do + expect(household_characteristics.section).to eq(section) + end + + it "has correct pages" do + expect(household_characteristics.pages.map(&:id)).to eq( + %w[ + household_wheelchair + household_disability + ], + ) + end + + it "has the correct id" do + expect(household_characteristics.id).to eq("household_needs") + end + + it "has the correct label" do + expect(household_characteristics.label).to eq("Household needs") + end + + it "has correct depends on" do + expect(household_characteristics.depends_on).to eq([{ "setup_completed?" => true }]) + end +end diff --git a/spec/models/form/sales/subsections/household_situation_spec.rb b/spec/models/form/sales/subsections/household_situation_spec.rb new file mode 100644 index 000000000..8473971ef --- /dev/null +++ b/spec/models/form/sales/subsections/household_situation_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Subsections::HouseholdSituation, type: :model do + subject(:household_characteristics) { described_class.new(subsection_id, subsection_definition, section) } + + let(:subsection_id) { nil } + let(:subsection_definition) { nil } + let(:section) { instance_double(Form::Sales::Sections::Household) } + + it "has correct section" do + expect(household_characteristics.section).to eq(section) + end + + it "has correct pages" do + expect(household_characteristics.pages.map(&:id)).to eq( + %w[ + buyers_organisations + ], + ) + end + + it "has the correct id" do + expect(household_characteristics.id).to eq("household_situation") + end + + it "has the correct label" do + expect(household_characteristics.label).to eq("Household situation") + end + + it "has correct depends on" do + expect(household_characteristics.depends_on).to eq([{ "setup_completed?" => true }]) + end +end