diff --git a/app/models/form/lettings/pages/managing_organisation.rb b/app/models/form/lettings/pages/managing_organisation.rb index f1d1197f7..fc2ef5e36 100644 --- a/app/models/form/lettings/pages/managing_organisation.rb +++ b/app/models/form/lettings/pages/managing_organisation.rb @@ -15,9 +15,17 @@ class Form::Lettings::Pages::ManagingOrganisation < ::Form::Page def routed_to?(log, current_user) return false unless current_user - return true unless current_user.organisation.holds_own_stock? - managing_agents = current_user.organisation.managing_agents + organisation = if current_user.support? + log.owning_organisation + else + current_user.organisation + end + + return false unless organisation + return true unless organisation.holds_own_stock? + + managing_agents = organisation.managing_agents return false if managing_agents.count.zero? return true if managing_agents.count > 1 diff --git a/spec/models/form/lettings/pages/managing_organisation_spec.rb b/spec/models/form/lettings/pages/managing_organisation_spec.rb index e4f27516f..4a44aa25d 100644 --- a/spec/models/form/lettings/pages/managing_organisation_spec.rb +++ b/spec/models/form/lettings/pages/managing_organisation_spec.rb @@ -51,6 +51,7 @@ RSpec.describe Form::Lettings::Pages::ManagingOrganisation, type: :model do let(:user) do create(:user, :support, organisation: create(:organisation, holds_own_stock: false)) end + let(:log) { create(:lettings_log, owning_organisation: user.organisation) } it "is shown" do expect(page.routed_to?(log, user)).to eq(true) @@ -61,6 +62,19 @@ RSpec.describe Form::Lettings::Pages::ManagingOrganisation, type: :model do end end + context "when owning_organisation not set" do + let(:user) { create(:user, :support) } + let(:log) { create(:lettings_log, owning_organisation: nil) } + + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(false) + end + + it "does not update managing_organisation_id" do + expect { page.routed_to?(log, user) }.not_to change(log.reload, :managing_organisation) + end + end + context "when holds own stock" do let(:user) do create(:user, :support, organisation: create(:organisation, holds_own_stock: true)) @@ -78,8 +92,8 @@ RSpec.describe Form::Lettings::Pages::ManagingOrganisation, type: :model do context "with >1 managing_agents" do before do - create(:organisation_relationship, :managing, parent_organisation: user.organisation) - create(:organisation_relationship, :managing, parent_organisation: user.organisation) + create(:organisation_relationship, :managing, parent_organisation: log.owning_organisation) + create(:organisation_relationship, :managing, parent_organisation: log.owning_organisation) end it "is shown" do @@ -99,7 +113,7 @@ RSpec.describe Form::Lettings::Pages::ManagingOrganisation, type: :model do :organisation_relationship, :managing, child_organisation: managing_agent, - parent_organisation: user.organisation, + parent_organisation: log.owning_organisation, ) end