From e603c590f63f6f5259d34331f98119ec18b9ff03 Mon Sep 17 00:00:00 2001 From: Jack S Date: Mon, 21 Nov 2022 11:24:03 +0000 Subject: [PATCH] Correctly handle housing provider question --- .../form/lettings/pages/housing_provider.rb | 14 ++++- .../lettings/pages/housing_provider_spec.rb | 59 +++++++++++++++++-- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/app/models/form/lettings/pages/housing_provider.rb b/app/models/form/lettings/pages/housing_provider.rb index b63f71a7e..ea801b49c 100644 --- a/app/models/form/lettings/pages/housing_provider.rb +++ b/app/models/form/lettings/pages/housing_provider.rb @@ -16,11 +16,19 @@ class Form::Lettings::Pages::HousingProvider < ::Form::Page def routed_to?(log, current_user) return false unless current_user return true if current_user.support? - return true unless current_user.organisation.holds_own_stock? - return true if current_user.organisation.housing_providers.count.positive? + housing_providers = current_user.organisation.housing_providers - log.update!(owning_organisation: current_user.organisation) + if current_user.organisation.holds_own_stock? + return true if housing_providers.count >= 1 + + log.update!(owning_organisation: current_user.organisation) + else + return false if housing_providers.count.zero? + return true if housing_providers.count > 1 + + log.update!(owning_organisation: housing_providers.first) + end false end diff --git a/spec/models/form/lettings/pages/housing_provider_spec.rb b/spec/models/form/lettings/pages/housing_provider_spec.rb index 9cf3bce6a..014ffbd66 100644 --- a/spec/models/form/lettings/pages/housing_provider_spec.rb +++ b/spec/models/form/lettings/pages/housing_provider_spec.rb @@ -63,12 +63,63 @@ RSpec.describe Form::Lettings::Pages::HousingProvider, type: :model do create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: false)) end - it "is shown" do - expect(page.routed_to?(log, user)).to eq(true) + context "with 0 housing_providers" do + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(false) + end + + it "does not update owning_organisation_id" do + expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation) + end end - it "does not update owning_organisation_id" do - expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation).from(nil) + context "with 1 housing_providers" do + let(:housing_provider) { create(:organisation) } + + before do + create( + :organisation_relationship, + :owning, + child_organisation: user.organisation, + parent_organisation: housing_provider, + ) + end + + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(false) + end + + it "updates owning_organisation_id" do + expect { page.routed_to?(log, user) }.to change(log.reload, :owning_organisation).from(nil).to(housing_provider) + end + end + + context "with >1 housing_providers" do + let(:housing_provider1) { create(:organisation) } + let(:housing_provider2) { create(:organisation) } + + before do + create( + :organisation_relationship, + :owning, + child_organisation: user.organisation, + parent_organisation: housing_provider1, + ) + create( + :organisation_relationship, + :owning, + child_organisation: user.organisation, + parent_organisation: housing_provider2, + ) + end + + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(true) + end + + it "updates owning_organisation_id" do + expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation) + end end end