@ -5,13 +5,19 @@ class OrganisationRelationshipsController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_user!
before_action :authenticate_scope!
before_action :authenticate_scope!
before_action :organisations
before_action :target_organisation , only : % i [
remove_housing_provider
remove_managing_agent
delete_housing_provider
delete_managing_agent
]
def housing_providers
def housing_providers
housing_providers = organisation . housing_providers
housing_providers = organisation . housing_providers
unpaginated_filtered_housing_providers = filtered_collection ( housing_providers , search_term )
unpaginated_filtered_housing_providers = filtered_collection ( housing_providers , search_term )
organisations = Organisation . where . not ( id : @organisation . id ) . pluck ( :id , :name )
respond_to :html
@pagy , @housing_providers = pagy ( unpaginated_filtered_housing_providers )
@pagy , @housing_providers = pagy ( unpaginated_filtered_housing_providers )
@organisations = organisations
@searched = search_term . presence
@searched = search_term . presence
@total_count = housing_providers . size
@total_count = housing_providers . size
end
end
@ -19,107 +25,84 @@ class OrganisationRelationshipsController < ApplicationController
def managing_agents
def managing_agents
managing_agents = organisation . managing_agents
managing_agents = organisation . managing_agents
unpaginated_filtered_managing_agents = filtered_collection ( managing_agents , search_term )
unpaginated_filtered_managing_agents = filtered_collection ( managing_agents , search_term )
organisations = Organisation . where . not ( id : @organisation . id ) . pluck ( :id , :name )
respond_to :html
@pagy , @managing_agents = pagy ( unpaginated_filtered_managing_agents )
@pagy , @managing_agents = pagy ( unpaginated_filtered_managing_agents )
@organisations = organisations
@searched = search_term . presence
@searched = search_term . presence
@total_count = managing_agents . size
@total_count = managing_agents . size
end
end
def add_housing_provider
def add_housing_provider
@organisations = Organisation . where . not ( id : @organisation . id ) . pluck ( :id , :name )
@organisation_relationship = organisation . parent_organisation_relationships . new
respond_to :html
end
end
def add_managing_agent
def add_managing_agent
@organisations = Organisation . where . not ( id : @organisation . id ) . pluck ( :id , :name )
@organisation_relationship = organisation . child_organisation_relationships . new
respond_to :html
end
end
def create_housing_provider
def create_housing_provider
child_organisation = @organisation
@organisation_relationship = organisation . parent_organisation_relationships . new ( organisation_relationship_params )
if params [ :organisation ] [ :related_organisation_id ] . empty?
if @organisation_relationship . save ( context : :housing_provider )
@organisation . errors . add :related_organisation_id , " You must choose a housing provider "
flash [ :notice ] = " #{ @organisation_relationship . parent_organisation . name } is now one of #{ current_user . data_coordinator? ? 'your' : " this organisation's " } housing providers "
@organisations = Organisation . where . not ( id : child_organisation . id ) . pluck ( :id , :name )
redirect_to housing_providers_organisation_path
render " organisation_relationships/add_housing_provider "
return
else
else
parent_organisation = related_organisation
@organisations = Organisation . where . not ( id : organisation . id ) . pluck ( :id , :name )
if OrganisationRelationship . exists? ( child_organisation : , parent_organisation : )
render " organisation_relationships/add_housing_provider " , status : :unprocessable_entity
@organisation . errors . add :related_organisation_id , " You have already added this housing provider "
@organisations = Organisation . where . not ( id : child_organisation . id ) . pluck ( :id , :name )
render " organisation_relationships/add_housing_provider "
return
end
end
end
create! ( child_organisation : , parent_organisation : )
flash [ :notice ] = " #{ related_organisation . name } is now one of #{ current_user . data_coordinator? ? 'your' : " this organisation's " } housing providers "
redirect_to housing_providers_organisation_path
end
end
def create_managing_agent
def create_managing_agent
parent_organisation = @organisation
@organisation_relationship = organisation . child_organisation_relationships . new ( organisation_relationship_params )
if params [ :organisation ] [ :related_organisation_id ] . empty?
if @organisation_relationship . save
@organisation . errors . add :related_organisation_id , " You must choose a managing agent "
flash [ :notice ] = " #{ @organisation_relationship . child_organisation . name } is now one of #{ current_user . data_coordinator? ? 'your' : " this organisation's " } managing agents "
@organisations = Organisation . where . not ( id : parent_organisation . id ) . pluck ( :id , :name )
redirect_to managing_agents_organisation_path
render " organisation_relationships/add_managing_agent "
return
else
else
child_organisation = related_organisation
@organisations = Organisation . where . not ( id : organisation . id ) . pluck ( :id , :name )
if OrganisationRelationship . exists? ( child_organisation : , parent_organisation : )
render " organisation_relationships/add_managing_agent " , status : :unprocessable_entity
@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
end
end
end
create! ( child_organisation : , parent_organisation : )
flash [ :notice ] = " #{ related_organisation . name } is now one of #{ current_user . data_coordinator? ? 'your' : " this organisation's " } managing agents "
redirect_to managing_agents_organisation_path
end
def remove_housing_provider
def remove_housing_provider ; end
@target_organisation_id = target_organisation . id
end
def delete_housing_provider
def delete_housing_provider
relationship = OrganisationRelationship . find_by! (
OrganisationRelationship . find_by! (
child_organisation : @ organisation,
child_organisation : organisation ,
parent_organisation : target_organisation ,
parent_organisation : target_organisation ,
)
) . destroy!
relationship . destroy!
flash [ :notice ] = " #{ target_organisation . name } is no longer one of #{ current_user . data_coordinator? ? 'your' : " this organisation's " } housing providers "
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
redirect_to housing_providers_organisation_path
end
end
def remove_managing_agent
def remove_managing_agent ; end
@target_organisation_id = target_organisation . id
end
def delete_managing_agent
def delete_managing_agent
relationship = OrganisationRelationship . find_by! (
OrganisationRelationship . find_by! (
parent_organisation : @ organisation,
parent_organisation : organisation ,
child_organisation : target_organisation ,
child_organisation : target_organisation ,
)
) . destroy!
relationship . destroy!
flash [ :notice ] = " #{ target_organisation . name } is no longer one of #{ current_user . data_coordinator? ? 'your' : " this organisation's " } managing agents "
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
redirect_to managing_agents_organisation_path
end
end
private
private
def create! ( child_organisation : , parent_organisation : )
def organisation
@resource = OrganisationRelationship . new ( child_organisation : , parent_organisation : )
@organisation || = if current_user . support?
@resource . save!
Organisation . find ( params [ :id ] )
else
current_user . organisation
end
end
end
def organisation
def organisations
@organisation || = Organisation . find ( params [ :id ] )
@organisations || = Organisation . where . not ( id : organisation . id ) . pluck ( :id , :name )
end
end
def related_organisation
def parent_organisation
@related_organisation || = Organisation . find ( params [ :organisation ] [ :related_organisation_id ] )
@parent_organisation || = Organisation . find ( params [ :organisation_relationship ] [ :parent_organisation_id ] )
end
def child_organisation
@child_organisation || = Organisation . find ( params [ :organisation_relationship ] [ :child_organisation_id ] )
end
end
def target_organisation
def target_organisation
@ -130,8 +113,12 @@ private
params [ " search " ]
params [ " search " ]
end
end
def organisation_relationship_params
params . require ( :organisation_relationship ) . permit ( :parent_organisation_id , :child_organisation_id )
end
def authenticate_scope!
def authenticate_scope!
if current_user . organisation != organisation && ! current_user . support?
if current_user . organisation != Organisation . find ( params [ :id ] ) && ! current_user . support?
render_not_found
render_not_found
end
end
end
end