Browse Source

Correctly handle housing provider question

pull/1012/head
Jack S 4 years ago
parent
commit
e603c590f6
  1. 12
      app/models/form/lettings/pages/housing_provider.rb
  2. 57
      spec/models/form/lettings/pages/housing_provider_spec.rb

12
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
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

57
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).from(nil)
expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation)
end
end
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

Loading…
Cancel
Save