Browse Source

Merge branch 'CLDC-1659-new-managing-agent-page' into CLDC-1661-new-page-for-housing-providers

# Conflicts:
#	config/routes.rb
CLDC-1661-new-page-for-housing-providers
natdeanlewissoftwire 2 years ago
parent
commit
a8af8a2ba9
  1. 17
      app/controllers/organisation_relationships_controller.rb
  2. 20
      app/helpers/navigation_items_helper.rb
  3. 6
      app/models/organisation.rb
  4. 6
      app/models/organisation_relationship.rb
  5. 8
      app/views/organisation_relationships/managing_agents.html.erb
  6. 6
      config/initializers/feature_toggle.rb
  7. 1
      config/routes.rb
  8. 4
      db/migrate/20221018143607_rename_organisations_agents_column.rb
  9. 5
      db/migrate/20221019082625_rename_managing_agents_column.rb
  10. 4
      db/schema.rb
  11. 23
      db/seeds.rb
  12. 5
      spec/factories/organisation.rb
  13. 14
      spec/factories/organisation_relationship.rb
  14. 62
      spec/models/organisation_spec.rb

17
app/controllers/organisation_relationships_controller.rb

@ -0,0 +1,17 @@
class OrganisationRelationshipsController < ApplicationController
include Pagy::Backend
before_action :authenticate_user!
def managing_agents
# kick out if cannot access org
@managing_agents = organisation.managing_agents
end
private
def organisation
@organisation ||= Organisation.find(params[:id])
end
end

20
app/helpers/navigation_items_helper.rb

@ -2,7 +2,7 @@ module NavigationItemsHelper
NavigationItem = Struct.new(:text, :href, :current, :classes)
def primary_items(path, current_user)
if current_user.support?
items = if current_user.support?
[
NavigationItem.new("Organisations", organisations_path, organisations_current?(path)),
NavigationItem.new("Users", "/users", users_current?(path)),
@ -27,6 +27,9 @@ module NavigationItemsHelper
NavigationItem.new("Housing providers", housing_providers_organisation_path(current_user.organisation), housing_providers_current?(path)),
].compact
end
# figure out correct rules
items << managing_agents_item(path)
end
def secondary_items(path, current_organisation_id)
@ -105,4 +108,19 @@ private
def subnav_details_path?(path)
path.include?("/organisations") && path.include?("/details")
end
def managing_agents_path?(path)
path.include?("/managing-agents")
end
def managing_agents_item(path)
return unless FeatureToggle.managing_agents_enabled?
return unless current_user.organisation.holds_own_stock?
NavigationItem.new(
"Managing agents",
"/organisations/#{current_user.organisation.id}/managing-agents",
managing_agents_path?(path),
)
end
end

6
app/models/organisation.rb

@ -13,9 +13,11 @@ class Organisation < ApplicationRecord
has_many :child_organisation_relationships, foreign_key: :parent_organisation_id, class_name: "OrganisationRelationship"
has_many :child_organisations, through: :child_organisation_relationships
has_many :managing_agent_relationships, -> { where(relationship_type: OrganisationRelationship::MANAGING) }, foreign_key: :child_organisation_id, class_name: "OrganisationRelationship"
has_many :managing_agents, through: :managing_agent_relationships, source: :parent_organisation
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }
scope :search_by, ->(param) { search_by_name(param) }
has_paper_trail
auto_strip_attributes :name
@ -83,7 +85,7 @@ class Organisation < ApplicationRecord
{ name: "Rent_periods", value: rent_period_labels, editable: false, format: :bullet },
{ name: "Owns housing stock", value: holds_own_stock ? "Yes" : "No", editable: false },
{ name: "Other stock owners", value: other_stock_owners, editable: false },
{ name: "Managing agents", value: managing_agents, editable: false },
{ name: "Managing agents", value: managing_agents_label, editable: false },
{ name: "Data protection agreement", value: data_protection_agreement_string, editable: false },
]
end

6
app/models/organisation_relationship.rb

@ -2,9 +2,11 @@ class OrganisationRelationship < ApplicationRecord
belongs_to :child_organisation, class_name: "Organisation"
belongs_to :parent_organisation, class_name: "Organisation"
OWNING = "owning".freeze
MANAGING = "managing".freeze
RELATIONSHIP_TYPE = {
"owning": 0,
"managing": 1,
OWNING => 0,
MANAGING => 1,
}.freeze
enum relationship_type: RELATIONSHIP_TYPE

8
app/views/organisation_relationships/managing_agents.html.erb

@ -0,0 +1,8 @@
<% title = format_title(nil, current_user.support? ? "About this organisation" : "Your managing agents", current_user, nil, nil, @organisation.name) %>
<% content_for :title, title %>
<%= render partial: "organisations/headings", locals: { main: "Your managing agents", sub: nil } %>
<% @managing_agents.each do |managing_agent|%>
<%= managing_agent.name %>
<% end %>

6
config/initializers/feature_toggle.rb

@ -8,4 +8,10 @@ class FeatureToggle
false
end
def self.managing_agents_enabled?
return true unless Rails.env.production?
false
end
end

1
config/routes.rb

@ -79,6 +79,7 @@ Rails.application.routes.draw do
post "logs/email-csv", to: "organisations#email_csv"
get "logs/csv-confirmation", to: "lettings_logs#csv_confirmation"
get "schemes", to: "organisations#schemes"
get "managing-agents", to: "organisation_relationships#managing_agents"
get "housing-providers", to: "organisations#housing_providers"
end
end

4
db/migrate/20221018143607_rename_organisations_agents_column.rb

@ -0,0 +1,4 @@
class RenameOrganisationsAgentsColumn < ActiveRecord::Migration[7.0]
def change
end
end

5
db/migrate/20221019082625_rename_managing_agents_column.rb

@ -0,0 +1,5 @@
class RenameManagingAgentsColumn < ActiveRecord::Migration[7.0]
def change
rename_column :organisations, :managing_agents, :managing_agents_label
end
end

4
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_10_17_095918) do
ActiveRecord::Schema[7.0].define(version: 2022_10_19_082625) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -297,7 +297,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_10_17_095918) do
t.string "postcode"
t.boolean "holds_own_stock"
t.string "other_stock_owners"
t.string "managing_agents"
t.string "managing_agents_label"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "active"

23
db/seeds.rb

@ -8,14 +8,25 @@
# rubocop:disable Rails/Output
unless Rails.env.test?
managing_agent = Organisation.find_or_create_by!(
name: "Managing Agent",
address_line1: "2 Marsham Street",
address_line2: "London",
postcode: "SW1P 4DF",
holds_own_stock: true,
other_stock_owners: "None",
managing_agents_label: "None",
provider_type: "LA",
)
org = Organisation.find_or_create_by!(
name: "DLUHC",
address_line1: "2 Marsham Street",
address_line2: "London",
postcode: "SW1P 4DF",
holds_own_stock: false,
holds_own_stock: true,
other_stock_owners: "None",
managing_agents: "None",
managing_agents_label: "None",
provider_type: "LA",
) do
info = "Seeded DLUHC Organisation"
@ -26,6 +37,12 @@ unless Rails.env.test?
end
end
OrganisationRelationship.create!(
child_organisation: org,
parent_organisation: managing_agent,
relationship_type: OrganisationRelationship::MANAGING,
)
if Rails.env.development? && User.count.zero?
User.create!(
name: "Provider",
@ -65,7 +82,7 @@ unless Rails.env.test?
postcode: "BA21 4AT",
holds_own_stock: false,
other_stock_owners: "None",
managing_agents: "None",
managing_agents_label: "None",
provider_type: "LA",
)

5
spec/factories/organisation.rb

@ -17,9 +17,4 @@ FactoryBot.define do
created_at { Time.zone.now }
updated_at { Time.zone.now }
end
factory :organisation_relationship do
child_organisation { FactoryBot.create(:organisation) }
parent_organisation { FactoryBot.create(:organisation) }
end
end

14
spec/factories/organisation_relationship.rb

@ -0,0 +1,14 @@
FactoryBot.define do
factory :organisation_relationship do
child_organisation { FactoryBot.create(:organisation) }
parent_organisation { FactoryBot.create(:organisation) }
trait :owning do
relationship_type { OrganisationRelationship::OWNING }
end
trait :managing do
relationship_type { OrganisationRelationship::MANAGING }
end
end
end

62
spec/models/organisation_spec.rb

@ -27,35 +27,67 @@ RSpec.describe Organisation, type: :model do
.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Provider type #{I18n.t('validations.organisation.provider_type_missing')}")
end
context "with managing association" do
let(:child_organisation) { FactoryBot.create(:organisation, name: "DLUHC Child") }
context "with parent/child associations", :aggregate_failures do
let!(:child_organisation) { FactoryBot.create(:organisation, name: "DLUHC Child") }
let!(:grandchild_organisation) { FactoryBot.create(:organisation, name: "DLUHC Grandchild") }
before do
FactoryBot.create(:organisation_relationship, child_organisation:, parent_organisation: organisation, relationship_type: 0)
FactoryBot.create(
:organisation_relationship,
:managing,
child_organisation:,
parent_organisation: organisation,
)
FactoryBot.create(
:organisation_relationship,
:managing,
child_organisation: grandchild_organisation,
parent_organisation: child_organisation,
)
end
it "has correct child" do
expect(organisation.child_organisations.first).to eq(child_organisation)
it "has correct child_organisations" do
expect(organisation.child_organisations).to eq([child_organisation])
expect(child_organisation.child_organisations).to eq([grandchild_organisation])
end
it "has correct parent" do
expect(child_organisation.parent_organisations.first).to eq(organisation)
it "has correct parent_organisations" do
expect(child_organisation.parent_organisations).to eq([organisation])
expect(grandchild_organisation.parent_organisations).to eq([child_organisation])
end
end
context "with owning association" do
let(:child_organisation) { FactoryBot.create(:organisation, name: "DLUHC Child") }
context "with managing association", :aggregate_failures do
let!(:child_organisation) { FactoryBot.create(:organisation, name: "DLUHC Child") }
let!(:grandchild_organisation) { FactoryBot.create(:organisation, name: "DLUHC Grandchild") }
before do
FactoryBot.create(:organisation_relationship, child_organisation:, parent_organisation: organisation, relationship_type: 1)
end
FactoryBot.create(
:organisation_relationship,
:managing,
child_organisation:,
parent_organisation: organisation,
)
FactoryBot.create(
:organisation_relationship,
:owning,
child_organisation:,
parent_organisation: organisation,
)
it "has correct child" do
expect(organisation.child_organisations.first).to eq(child_organisation)
FactoryBot.create(
:organisation_relationship,
:managing,
child_organisation: grandchild_organisation,
parent_organisation: child_organisation,
)
end
it "has correct parent" do
expect(child_organisation.parent_organisations.first).to eq(organisation)
it "has correct managing_agents" do
expect(child_organisation.managing_agents).to eq([organisation])
expect(grandchild_organisation.managing_agents).to eq([child_organisation])
end
end

Loading…
Cancel
Save