From 7adf58feaef60d4ef8740c0aef080c6836b5ba70 Mon Sep 17 00:00:00 2001 From: Jack S Date: Tue, 18 Oct 2022 15:36:19 +0100 Subject: [PATCH] wip --- .../organisation_relationships_controller.rb | 20 +++++++++++++++++++ app/helpers/navigation_items_helper.rb | 16 +++++++++++++++ app/models/organisation.rb | 6 ++++++ app/models/organisation_relationship.rb | 6 ++++-- .../managing_agents.html.erb | 3 +++ config/initializers/feature_toggle.rb | 6 ++++++ config/routes.rb | 1 + ...3607_rename_organisations_agents_column.rb | 4 ++++ db/seeds.rb | 15 +++++++++++++- spec/models/organisation_spec.rb | 4 ++++ 10 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 app/controllers/organisation_relationships_controller.rb create mode 100644 app/views/organisation_relationships/managing_agents.html.erb create mode 100644 db/migrate/20221018143607_rename_organisations_agents_column.rb diff --git a/app/controllers/organisation_relationships_controller.rb b/app/controllers/organisation_relationships_controller.rb new file mode 100644 index 000000000..e784c23e1 --- /dev/null +++ b/app/controllers/organisation_relationships_controller.rb @@ -0,0 +1,20 @@ +class OrganisationRelationshipsController < ApplicationController + include Pagy::Backend + + before_action :authenticate_user! + + def managing_agents + # kick out if org isn't the current org + + @managing_agents = OrganisationRelationships.where( + owning_organisation_id: organisation.id, + relationship_type: :managing, + ) + end + +private + + def organisation + @organisation ||= Organisation.find(params[:id]) + end +end diff --git a/app/helpers/navigation_items_helper.rb b/app/helpers/navigation_items_helper.rb index 3ac422c2d..a5f701743 100644 --- a/app/helpers/navigation_items_helper.rb +++ b/app/helpers/navigation_items_helper.rb @@ -24,6 +24,7 @@ module NavigationItemsHelper FeatureToggle.sales_log_enabled? ? NavigationItem.new("Sales logs", sales_logs_path, sales_logs_current?(path)) : nil, NavigationItem.new("Users", users_organisation_path(current_user.organisation), subnav_users_path?(path)), NavigationItem.new("About your organisation", "/organisations/#{current_user.organisation.id}", subnav_details_path?(path)), + managing_agents_item, ].compact end end @@ -91,4 +92,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 + 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 diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 9eb039ab1..d21717206 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -13,6 +13,12 @@ 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_agents, + -> { joins(:parent_organisation_relationships).where({ parent_organisation_relationships: { relationship_type: OrganisationRelationship::MANAGING } }) }, + class_name: "Organisation", + through: :parent_organisation_relationships, + source: :child_organisation + scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } scope :search_by, ->(param) { search_by_name(param) } diff --git a/app/models/organisation_relationship.rb b/app/models/organisation_relationship.rb index 5c50c3e0f..acda8d2c4 100644 --- a/app/models/organisation_relationship.rb +++ b/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 diff --git a/app/views/organisation_relationships/managing_agents.html.erb b/app/views/organisation_relationships/managing_agents.html.erb new file mode 100644 index 000000000..a9b698239 --- /dev/null +++ b/app/views/organisation_relationships/managing_agents.html.erb @@ -0,0 +1,3 @@ +<% @managing_agents.each do |managing_agent|%> + <%= managing_agent.inspect %> +<% end %> diff --git a/config/initializers/feature_toggle.rb b/config/initializers/feature_toggle.rb index 0e01531ca..64b75e6b7 100644 --- a/config/initializers/feature_toggle.rb +++ b/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 diff --git a/config/routes.rb b/config/routes.rb index 0be5f5b6a..2dffe036e 100644 --- a/config/routes.rb +++ b/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" end end diff --git a/db/migrate/20221018143607_rename_organisations_agents_column.rb b/db/migrate/20221018143607_rename_organisations_agents_column.rb new file mode 100644 index 000000000..0397f4afd --- /dev/null +++ b/db/migrate/20221018143607_rename_organisations_agents_column.rb @@ -0,0 +1,4 @@ +class RenameOrganisationsAgentsColumn < ActiveRecord::Migration[7.0] + def change + end +end diff --git a/db/seeds.rb b/db/seeds.rb index 8491db6b6..f8d421999 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -13,7 +13,7 @@ unless Rails.env.test? 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", provider_type: "LA", @@ -26,6 +26,19 @@ unless Rails.env.test? end end + Organisation.find_or_create_by!( + name: "DLUHC", + address_line1: "2 Marsham Street", + address_line2: "London", + postcode: "SW1P 4DF", + holds_own_stock: true, + other_stock_owners: "None", + managing_agents: "None", + provider_type: "LA", + child_organisations: [org] + ) + + if Rails.env.development? && User.count.zero? User.create!( name: "Provider", diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index b6011cf20..e2336bd0c 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -41,6 +41,10 @@ RSpec.describe Organisation, type: :model do it "has correct parent" do expect(child_organisation.parent_organisations.first).to eq(organisation) end + + it "has correct managing agents" do + expect(child_organisation.managing_agents).to eq([organisation]) + end end context "with owning association" do