diff --git a/app/models/form/sales/pages/managing_organisation.rb b/app/models/form/sales/pages/managing_organisation.rb index aa34c2cfe..3d8e59383 100644 --- a/app/models/form/sales/pages/managing_organisation.rb +++ b/app/models/form/sales/pages/managing_organisation.rb @@ -12,9 +12,20 @@ class Form::Sales::Pages::ManagingOrganisation < ::Form::Page def routed_to?(log, current_user) return false unless current_user - return false unless current_user.support? - return false unless log.owning_organisation - log.owning_organisation.managing_agents.count >= 1 + if form.start_year_after_2024? + organisation = current_user.support? ? log.owning_organisation : current_user.organisation + + return false unless organisation + return false if log.owning_organisation != organisation && !organisation.holds_own_stock? + return true unless organisation.holds_own_stock? + + organisation.managing_agents.count >= 1 + else + return false unless current_user.support? + return false unless log.owning_organisation + + log.owning_organisation.managing_agents.count >= 1 + end end end diff --git a/spec/models/form/sales/pages/managing_organisation_spec.rb b/spec/models/form/sales/pages/managing_organisation_spec.rb index 0a34554cb..8c411401d 100644 --- a/spec/models/form/sales/pages/managing_organisation_spec.rb +++ b/spec/models/form/sales/pages/managing_organisation_spec.rb @@ -5,9 +5,13 @@ RSpec.describe Form::Sales::Pages::ManagingOrganisation, type: :model do let(:page_id) { nil } let(:page_definition) { nil } - let(:subsection) { instance_double(Form::Subsection) } + let(:subsection) { instance_double(Form::Subsection, form:) } let(:form) { instance_double(Form) } + before do + allow(form).to receive(:start_year_after_2024?).and_return(false) + end + it "has correct subsection" do expect(page.subsection).to eq(subsection) end @@ -32,8 +36,8 @@ RSpec.describe Form::Sales::Pages::ManagingOrganisation, type: :model do expect(page.depends_on).to be nil end - describe "#routed_to?" do - let(:log) { create(:lettings_log) } + describe "#routed_to? with 2023 logs" do + let(:log) { create(:sales_log) } let(:organisation) { create(:organisation) } context "when user nil" do @@ -54,7 +58,7 @@ RSpec.describe Form::Sales::Pages::ManagingOrganisation, type: :model do let(:user) { create(:user, :support) } context "when owning_organisation not set" do - let(:log) { create(:lettings_log, owning_organisation: nil) } + let(:log) { create(:sales_log, owning_organisation: nil) } it "is not shown" do expect(page.routed_to?(log, user)).to eq(false) @@ -103,4 +107,146 @@ RSpec.describe Form::Sales::Pages::ManagingOrganisation, type: :model do end end end + + describe "#routed_to? with 2024 logs" do + let(:log) { create(:sales_log) } + let(:organisation) { create(:organisation) } + + before do + allow(form).to receive(:start_year_after_2024?).and_return(true) + end + + context "when user nil" do + it "is not shown" do + expect(page.routed_to?(log, nil)).to eq(false) + end + end + + context "when support" do + context "when does not hold own stock" do + let(:user) do + create(:user, :support, organisation: create(:organisation, holds_own_stock: false)) + end + let(:log) { create(:sales_log, owning_organisation: user.organisation) } + + it "is shown" do + expect(page.routed_to?(log, user)).to eq(true) + end + end + + context "when owning_organisation not set" do + let(:user) { create(:user, :support) } + let(:log) { create(:sales_log, owning_organisation: nil) } + + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(false) + end + end + + context "when holds own stock" do + let(:user) do + create(:user, :support, organisation: create(:organisation, holds_own_stock: true)) + end + + context "with 0 managing_agents" do + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(false) + end + end + + context "with >1 managing_agents" do + before do + create(:organisation_relationship, parent_organisation: log.owning_organisation) + create(:organisation_relationship, parent_organisation: log.owning_organisation) + end + + it "is shown" do + expect(page.routed_to?(log, user)).to eq(true) + end + end + + context "with 1 managing_agents" do + let(:managing_agent) { create(:organisation) } + + before do + create( + :organisation_relationship, + child_organisation: managing_agent, + parent_organisation: log.owning_organisation, + ) + end + + it "is shown" do + expect(page.routed_to?(log, user)).to eq(true) + end + end + end + end + + context "when not support" do + context "when does not hold own stock" do + let(:user) { create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: false)) } + + context "and the user's organisation is selected as owning organisation" do + let(:log) { create(:sales_log, owning_organisation: user.organisation) } + + it "is shown" do + expect(page.routed_to?(log, user)).to eq(true) + end + end + + context "and a different than the user's organisation is selected as owning organisation" do + let(:stock_owner) { create(:organisation, holds_own_stock: true) } + let(:log) { create(:sales_log, owning_organisation: stock_owner) } + + before do + create(:organisation_relationship, parent_organisation: stock_owner, child_organisation: user.organisation) + end + + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(false) + end + end + end + + context "when holds own stock" do + let(:user) do + create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: true)) + end + + context "with 0 managing_agents" do + it "is not shown" do + expect(page.routed_to?(log, user)).to eq(false) + end + end + + context "with >1 managing_agents" do + before do + create(:organisation_relationship, parent_organisation: user.organisation) + create(:organisation_relationship, parent_organisation: user.organisation) + end + + it "is shown" do + expect(page.routed_to?(log, user)).to eq(true) + end + end + + context "with 1 managing_agents" do + let(:managing_agent) { create(:organisation) } + + before do + create( + :organisation_relationship, + child_organisation: managing_agent, + parent_organisation: user.organisation, + ) + end + + it "is shown" do + expect(page.routed_to?(log, user)).to eq(true) + end + end + end + end + end end