diff --git a/app/controllers/organisation_relationships_controller.rb b/app/controllers/organisation_relationships_controller.rb
index 590e41a79..f50349d20 100644
--- a/app/controllers/organisation_relationships_controller.rb
+++ b/app/controllers/organisation_relationships_controller.rb
@@ -86,12 +86,31 @@ class OrganisationRelationshipsController < ApplicationController
end
def delete_housing_provider
- organisation_relationship_to_remove = OrganisationRelationship.find_by!(child_organisation: @organisation, parent_organisation: target_organisation, relationship_type: OrganisationRelationship::OWNING)
- organisation_relationship_to_remove.destroy!
+ relationship = OrganisationRelationship.find_by!(
+ child_organisation: @organisation,
+ parent_organisation: target_organisation,
+ relationship_type: OrganisationRelationship::OWNING,
+ )
+ relationship.destroy!
flash[:notice] = "#{target_organisation.name} is no longer one of #{current_user.data_coordinator? ? 'your' : "this organisation's"} housing providers"
redirect_to housing_providers_organisation_path
end
+ def remove_managing_agent
+ @target_organisation_id = target_organisation.id
+ end
+
+ def delete_managing_agent
+ relationship = OrganisationRelationship.find_by!(
+ parent_organisation: @organisation,
+ child_organisation: target_organisation,
+ relationship_type: OrganisationRelationship::MANAGING,
+ )
+ relationship.destroy!
+ flash[:notice] = "#{target_organisation.name} is no longer one of #{current_user.data_coordinator? ? 'your' : "this organisation's"} managing agents"
+ redirect_to managing_agents_organisation_path
+ end
+
private
def create!(child_organisation:, parent_organisation:, relationship_type:)
diff --git a/app/views/organisation_relationships/_managing_agent_list.erb b/app/views/organisation_relationships/_managing_agent_list.erb
index 3c69bbf71..65195033c 100644
--- a/app/views/organisation_relationships/_managing_agent_list.erb
+++ b/app/views/organisation_relationships/_managing_agent_list.erb
@@ -12,7 +12,7 @@
scope: "row",
class: "govuk-!-text-align-right",
}) do %>
- <%= govuk_link_to("Remove", "managing-agents/#{managing_agent.id}") %>
+ <%= govuk_link_to("Remove", managing_agents_remove_organisation_path(target_organisation_id: managing_agent.id)) %>
<% end %>
<% end %>
<% end %>
diff --git a/app/views/organisation_relationships/managing_agents.html.erb b/app/views/organisation_relationships/managing_agents.html.erb
index db423366c..2f17f0a7e 100644
--- a/app/views/organisation_relationships/managing_agents.html.erb
+++ b/app/views/organisation_relationships/managing_agents.html.erb
@@ -1,32 +1,11 @@
<% item_label = format_label(@pagy.count, "managing agents") %>
-<% if current_user.data_coordinator? %>
- <% if params["related_organisation_id"] %>
- <%= govuk_notification_banner(
- title_text: "Success",
- success: true, title_heading_level: 3,
- title_id: "swanky-notifications"
- ) do |notification_banner|
- notification_banner.heading(text: "#{Organisation.find(params['related_organisation_id']).name} is now one of this organisation's managing agents")
- end %>
- <% end %>
-<% end %>
-
<% if current_user.support? %>
<%= render partial: "organisations/headings", locals: { main: @organisation.name, sub: nil } %>
<%= render SubNavigationComponent.new(
items: secondary_items(request.path, @organisation.id),
) %>
Managing Agents
- <% if params["related_organisation_id"] %>
- <%= govuk_notification_banner(
- title_text: "Success",
- success: true, title_heading_level: 3,
- title_id: "swanky-notifications"
- ) do |notification_banner|
- notification_banner.heading(text: "#{Organisation.find(params['related_organisation_id']).name} is now one of this organisation's managing agents")
- end %>
- <% end %>
A managing agent can submit logs for this organisation.
<% if @total_count == 0 %>
This organisation does not currently have any managing agents.
diff --git a/app/views/organisation_relationships/remove_managing_agent.html.erb b/app/views/organisation_relationships/remove_managing_agent.html.erb
new file mode 100644
index 000000000..2862acf64
--- /dev/null
+++ b/app/views/organisation_relationships/remove_managing_agent.html.erb
@@ -0,0 +1,21 @@
+<%= form_with url: managing_agents_organisation_path(target_organisation_id: @target_organisation.id), method: "delete", 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)) %>
+ Remove Managing Agent
+ <% end %>
+ <% if current_user.support? %>
+ <%= govuk_back_link(href: :back) %>
+ <%= render partial: "organisations/headings", locals: { main: "You are removing ‘#{@target_organisation.name}’ from this organisation's managing agents", sub: nil } %>
+ <% else %>
+ <% content_for :before_content do %>
+ <%= govuk_back_link(href: :back) %>
+ <% end %>
+ <%= render partial: "organisations/headings", locals: { main: "You are removing ‘#{@target_organisation.name}’ from your organisation's managing agents", sub: nil } %>
+ <% end %>
+ <%= govuk_warning_text text: "#{@target_organisation.name} will no longer be able to submit logs for you" %>
+
+ <%= f.govuk_submit "Confirm" %>
+ <%= govuk_button_link_to "Cancel", managing_agents_organisation_path(current_user.organisation), html: { method: :get }, secondary: true %>
+
+<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index 9af179103..8db33eb10 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -86,7 +86,9 @@ Rails.application.routes.draw do
delete "housing-providers", to: "organisation_relationships#delete_housing_provider"
get "managing-agents", to: "organisation_relationships#managing_agents"
get "managing-agents/add", to: "organisation_relationships#add_managing_agent"
+ get "managing-agents/remove", to: "organisation_relationships#remove_managing_agent"
post "managing-agents", to: "organisation_relationships#create_managing_agent"
+ delete "managing-agents", to: "organisation_relationships#delete_managing_agent"
end
end
diff --git a/spec/requests/organisation_relationships_controller_spec.rb b/spec/requests/organisation_relationships_controller_spec.rb
index c00c20fc8..83655156c 100644
--- a/spec/requests/organisation_relationships_controller_spec.rb
+++ b/spec/requests/organisation_relationships_controller_spec.rb
@@ -212,6 +212,34 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
expect(response).to redirect_to("/organisations/#{organisation.id}/housing-providers")
end
end
+
+ describe "organisation_relationships#delete_managing_agent" do
+ let!(:managing_agent) { FactoryBot.create(:organisation) }
+ let(:params) do
+ {
+ "target_organisation_id": managing_agent.id,
+ }
+ end
+ let(:request) { delete "/organisations/#{organisation.id}/managing-agents", headers:, params: }
+
+ before do
+ FactoryBot.create(
+ :organisation_relationship,
+ :managing,
+ parent_organisation: organisation,
+ child_organisation: managing_agent,
+ )
+ end
+
+ it "deletes the new organisation relationship" do
+ expect { request }.to change(OrganisationRelationship, :count).by(-1)
+ end
+
+ it "redirects to the organisation list" do
+ request
+ expect(response).to redirect_to("/organisations/#{organisation.id}/managing-agents")
+ end
+ end
end
context "with a data provider user" do
@@ -415,6 +443,34 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
end
end
+ describe "organisation_relationships#delete_managing_agent" do
+ let!(:managing_agent) { FactoryBot.create(:organisation) }
+ let(:params) do
+ {
+ "target_organisation_id": managing_agent.id,
+ }
+ end
+ let(:request) { delete "/organisations/#{organisation.id}/managing-agents", headers:, params: }
+
+ before do
+ FactoryBot.create(
+ :organisation_relationship,
+ :managing,
+ parent_organisation: organisation,
+ child_organisation: managing_agent,
+ )
+ end
+
+ it "deletes the new organisation relationship" do
+ expect { request }.to change(OrganisationRelationship, :count).by(-1)
+ end
+
+ it "redirects to the organisation list" do
+ request
+ expect(response).to redirect_to("/organisations/#{organisation.id}/managing-agents")
+ end
+ end
+
context "when viewing a specific organisation's housing providers" do
let!(:housing_provider) { FactoryBot.create(:organisation) }
let!(:other_org_housing_provider) { FactoryBot.create(:organisation, name: "Foobar LTD") }
@@ -504,6 +560,10 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
expect(page).to have_content("1 total agents")
end
+ it "shows remove link(s)" do
+ expect(response.body).to include("Remove")
+ end
+
context "when adding a housing provider" do
before do
get "/organisations/#{organisation.id}/managing-agents/add", headers:, params: {}