diff --git a/app/controllers/organisation_relationships_controller.rb b/app/controllers/organisation_relationships_controller.rb index d622fd17b..f731209a8 100644 --- a/app/controllers/organisation_relationships_controller.rb +++ b/app/controllers/organisation_relationships_controller.rb @@ -23,9 +23,11 @@ class OrganisationRelationshipsController < ApplicationController def managing_agents managing_agents = organisation.managing_agents unpaginated_filtered_managing_agents = filtered_collection(managing_agents, search_term) + organisations = Organisation.where.not(id: @organisation.id).pluck(:id, :name) respond_to do |format| format.html do @pagy, @managing_agents = pagy(unpaginated_filtered_managing_agents) + @organisations = organisations @searched = search_term.presence @total_count = managing_agents.size render "organisation_relationships/managing_agents", layout: "application" @@ -42,6 +44,15 @@ class OrganisationRelationshipsController < ApplicationController end end + def add_managing_agent + @organisations = Organisation.where.not(id: @organisation.id).pluck(:id, :name) + respond_to do |format| + format.html do + render "organisation_relationships/add_managing_agent", layout: "application" + end + end + end + def create_housing_provider child_organisation_id = @organisation.id parent_organisation_id = related_organisation_id @@ -61,21 +72,47 @@ class OrganisationRelationshipsController < ApplicationController redirect_to housing_providers_organisation_path(related_organisation_id:) end + def create_managing_agent + parent_organisation_id = @organisation.id + child_organisation_id = related_organisation_id + relationship_type = OrganisationRelationship::MANAGING + + if related_organisation_id.empty? + @organisation.errors.add :related_organisation_id, "You must choose a managing agent" + @organisations = Organisation.where.not(id: parent_organisation_id).pluck(:id, :name) + render "organisation_relationships/add_managing_agent" + return + elsif OrganisationRelationship.exists?(child_organisation_id:, parent_organisation_id:, relationship_type:) + @organisation.errors.add :related_organisation_id, "You have already added this managing agent" + @organisations = Organisation.where.not(id: parent_organisation_id).pluck(:id, :name) + render "organisation_relationships/add_managing_agent" + return + end + + create!(child_organisation_id:, parent_organisation_id:, relationship_type:) + redirect_to managing_agents_organisation_path(related_organisation_id:) + end + +private + def create!(child_organisation_id:, parent_organisation_id:, relationship_type:) @resource = OrganisationRelationship.new(child_organisation_id:, parent_organisation_id:, relationship_type:) @resource.save! end -private - - def organisation - @organisation ||= Organisation.find(params[:id]) + def create(child_organisation_id, parent_organisation_id, relationship_type) + @resource = OrganisationRelationship.new(child_organisation_id:, parent_organisation_id:, relationship_type:) + @resource.save! end def related_organisation_id params["organisation"]["related_organisation_id"] end + def organisation + @organisation ||= Organisation.find(params[:id]) + end + def search_term params["search"] end diff --git a/app/views/organisation_relationships/add_housing_provider.html.erb b/app/views/organisation_relationships/add_housing_provider.html.erb index 638d0e00f..3cce90db1 100644 --- a/app/views/organisation_relationships/add_housing_provider.html.erb +++ b/app/views/organisation_relationships/add_housing_provider.html.erb @@ -3,8 +3,6 @@ <%= render partial: "organisations/headings", locals: { main: @organisation.name, sub: nil } %> <%= render SubNavigationComponent.new(items: secondary_items(request.path, @organisation.id)) %>
Start typing to search for a housing provider
diff --git a/app/views/organisation_relationships/add_managing_agent.html.erb b/app/views/organisation_relationships/add_managing_agent.html.erb new file mode 100644 index 000000000..bf5c380d5 --- /dev/null +++ b/app/views/organisation_relationships/add_managing_agent.html.erb @@ -0,0 +1,34 @@ +<%= form_with model: @organisation, url: managing_agents_organisation_path, method: "post", local: true do |f| %> + <% if current_user.support? %> + <%= render partial: "organisations/headings", locals: { main: @organisation.name, sub: nil } %> + <%= render SubNavigationComponent.new(items: secondary_items(request.path, @organisation.id)) %> +Start typing to search for a managing agent
+ <% else %> + <% content_for :before_content do %> + <%= govuk_back_link(href: :back) %> + <% end %> + <%= render partial: "organisations/headings", locals: { main: "What is the name of your managing agent?", sub: nil } %> +Start typing to search for your managing agent
+ <% end %> + <% answer_options = { "" => "Select an option" } %> + <% @organisations.each do |organisation| %> + <% answer_options[organisation[0]] = organisation[1] %> + <% end %> + <%= render partial: "organisation_relationships/related_organisation_select_question", locals: { + question: Form::Question.new("", { "answer_options" => answer_options }, nil), + f:, + } %> + <%= f.govuk_submit "Add" %> + <%= govuk_details(summary_text: "Can't find the managing agent you're looking for?") do %> +A managing agent can submit logs for your organisation.
+A managing agent can submit logs for this organisation.
+ <% if @total_count == 0 %> +This organisation does not currently have any managing agents.
+ <% end %> <% else %> - <%= render partial: "organisations/headings", locals: { main: "Your managing agents", sub: nil } %> - -A managing agent can submit logs for your organisation.
-<% end %> -<% if @total_count == 0 %> -You do not currently have any managing agents.
+ <%= render partial: "organisations/headings", locals: { main: "This organisation managing agents", sub: current_user.organisation.name } %> +A managing agent can submit logs for this organisation.
+ <% if @total_count == 0 %> +This organisation does not currently have any managing agents.
+ <% end %> <% end %> -<% if current_user.data_coordinator? || current_user.support? %> - <%= govuk_button_link_to "Add a managing agent", managing_agents_organisation_path(organisation_id: @organisation.id), html: { method: :get } %> +<% if current_user.support? %> + <%= govuk_button_link_to "Add a managing agent", managing_agents_add_organisation_path(organisation_id: @organisation.id), html: { method: :get } %> +<% elsif current_user.data_coordinator? %> + <%= govuk_button_link_to "Add a managing agent", managing_agents_add_organisation_path, html: { method: :get } %> <% end %> <% if @total_count != 0 %> <%= render SearchComponent.new(current_user:, search_label: "Search for a managing agent", value: @searched) %> diff --git a/config/routes.rb b/config/routes.rb index eae1b159c..838887f1b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -83,6 +83,8 @@ Rails.application.routes.draw do get "housing-providers/add", to: "organisation_relationships#add_housing_provider" post "housing-providers", to: "organisation_relationships#create_housing_provider" get "managing-agents", to: "organisation_relationships#managing_agents" + get "managing-agents/add", to: "organisation_relationships#add_managing_agent" + post "managing-agents", to: "organisation_relationships#create_managing_agent" end end diff --git a/spec/requests/organisation_relationships_controller_spec.rb b/spec/requests/organisation_relationships_controller_spec.rb new file mode 100644 index 000000000..9fdb01ec9 --- /dev/null +++ b/spec/requests/organisation_relationships_controller_spec.rb @@ -0,0 +1,473 @@ +require "rails_helper" + +RSpec.describe OrganisationRelationshipsController, type: :request do + let(:organisation) { user.organisation } + let!(:unauthorised_organisation) { FactoryBot.create(:organisation) } + let(:headers) { { "Accept" => "text/html" } } + let(:page) { Capybara::Node::Simple.new(response.body) } + + context "when user is signed in" do + let(:user) { FactoryBot.create(:user, :data_coordinator) } + + context "with a data coordinator user" do + before do + sign_in user + end + + context "when accessing the housing providers tab" do + context "with an organisation that the user belongs to" do + let!(:housing_provider) { FactoryBot.create(:organisation) } + let!(:other_org_housing_provider) { FactoryBot.create(:organisation, name: "Foobar LTD") } + let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") } + + before do + FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning]) + FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning]) + get "/organisations/#{organisation.id}/housing-providers", headers:, params: {} + end + + it "shows the tab navigation" do + expected_html = "