diff --git a/app/controllers/organisation_relationships_controller.rb b/app/controllers/organisation_relationships_controller.rb index 95678e852..e903f676a 100644 --- a/app/controllers/organisation_relationships_controller.rb +++ b/app/controllers/organisation_relationships_controller.rb @@ -39,49 +39,32 @@ class OrganisationRelationshipsController < ApplicationController def create_housing_provider child_organisation = @organisation - if params[:organisation][:related_organisation_id].empty? - @organisation.errors.add :related_organisation_id, "You must choose a housing provider" - @organisations = Organisation.where.not(id: child_organisation.id).pluck(:id, :name) - render "organisation_relationships/add_housing_provider" - return - else + if params[:organisation][:related_organisation_id].present? parent_organisation = related_organisation - if OrganisationRelationship.exists?(child_organisation:, parent_organisation:) - @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 - elsif !parent_organisation.holds_own_stock - @organisation.errors.add :related_organisation_id, I18n.t("validations.scheme.owning_organisation.does_not_own_stock") - @organisations = Organisation.where.not(id: child_organisation.id).pluck(:id, :name) - render "organisation_relationships/add_housing_provider" - return - 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 + @resource = OrganisationRelationship.new(child_organisation:, parent_organisation:) + if @resource.save(context: :housing_provider) + 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 + else + @organisations = Organisation.where.not(id: child_organisation.id).pluck(:id, :name) + render "organisation_relationships/add_housing_provider", status: :unprocessable_entity + end end def create_managing_agent parent_organisation = @organisation - if params[:organisation][: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 - else + if params[:organisation][:related_organisation_id].present? child_organisation = related_organisation - if OrganisationRelationship.exists?(child_organisation:, parent_organisation:) - @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 - 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 + @resource = OrganisationRelationship.new(child_organisation:, parent_organisation:) + if @resource.save(context: :managing_agent) + 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 + else + @organisations = Organisation.where.not(id: parent_organisation.id).pluck(:id, :name) + render "organisation_relationships/add_managing_agent", status: :unprocessable_entity + end end def remove_housing_provider diff --git a/app/models/organisation_relationship.rb b/app/models/organisation_relationship.rb index 034fc5d0e..687277f92 100644 --- a/app/models/organisation_relationship.rb +++ b/app/models/organisation_relationship.rb @@ -1,4 +1,28 @@ class OrganisationRelationship < ApplicationRecord belongs_to :child_organisation, class_name: "Organisation" belongs_to :parent_organisation, class_name: "Organisation" + validates :parent_organisation_id, uniqueness: { scope: [:child_organisation_id] } + validate :validate_housing_provider_relationship, on: :housing_provider + validate :validate_managing_agent_relationship, on: :managing_agent + +private + + def validate_housing_provider_relationship + if parent_organisation_id.blank? + child_organisation.errors.add :related_organisation_id, "You must choose a housing provider" + elsif OrganisationRelationship.exists?(child_organisation:, parent_organisation:) + child_organisation.errors.add :related_organisation_id, "You have already added this housing provider" + elsif !parent_organisation.holds_own_stock + errors.add :parent_organisation_id + child_organisation.errors.add :related_organisation_id, I18n.t("validations.scheme.owning_organisation.does_not_own_stock") + end + end + + def validate_managing_agent_relationship + if child_organisation_id.blank? + parent_organisation.errors.add :related_organisation_id, "You must choose a managing agent" + elsif OrganisationRelationship.exists?(child_organisation:, parent_organisation:) + parent_organisation.errors.add :related_organisation_id, "You have already added this managing agent" + end + end end diff --git a/app/views/organisation_relationships/managing_agents.html.erb b/app/views/organisation_relationships/managing_agents.html.erb index 2f17f0a7e..ae84d8eac 100644 --- a/app/views/organisation_relationships/managing_agents.html.erb +++ b/app/views/organisation_relationships/managing_agents.html.erb @@ -11,7 +11,7 @@
This organisation does not currently have any managing agents.
<% end %> <% else %> - <%= render partial: "organisations/headings", locals: { main: "This organisation managing agents", sub: current_user.organisation.name } %> + <%= render partial: "organisations/headings", locals: { main: "Your 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.