diff --git a/app/controllers/organisation_relationships_controller.rb b/app/controllers/organisation_relationships_controller.rb index 29c572f62..53952d3d6 100644 --- a/app/controllers/organisation_relationships_controller.rb +++ b/app/controllers/organisation_relationships_controller.rb @@ -29,22 +29,24 @@ class OrganisationRelationshipsController < ApplicationController def add_housing_provider @organisations = Organisation.where.not(id: @organisation.id).pluck(:id, :name) + @organisation_relationship = OrganisationRelationship.new respond_to :html end def add_managing_agent @organisations = Organisation.where.not(id: @organisation.id).pluck(:id, :name) + @organisation_relationship = OrganisationRelationship.new respond_to :html end def create_housing_provider child_organisation = @organisation - if params[:organisation_relationship].present? && params[:organisation_relationship][:related_organisation_id].present? - parent_organisation = related_organisation + if params[:organisation_relationship].present? && params[:organisation_relationship][:parent_organisation_id].present? + parent_organisation = @parent_organisation ||= Organisation.find(params[:organisation_relationship][:parent_organisation_id]) end @organisation_relationship = OrganisationRelationship.new(child_organisation:, parent_organisation:) if @organisation_relationship.save(context: :housing_provider) - flash[:notice] = "#{related_organisation.name} is now one of #{current_user.data_coordinator? ? 'your' : "this organisation's"} housing providers" + flash[:notice] = "#{parent_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) @@ -54,12 +56,12 @@ class OrganisationRelationshipsController < ApplicationController def create_managing_agent parent_organisation = @organisation - if params[:organisation_relationship].present? && params[:organisation_relationship][:related_organisation_id].present? - child_organisation = related_organisation + if params[:organisation_relationship].present? && params[:organisation_relationship][:child_organisation_id].present? + child_organisation = @child_organisation ||= Organisation.find(params[:organisation_relationship][:child_organisation_id]) end @organisation_relationship = OrganisationRelationship.new(child_organisation:, parent_organisation:) if @organisation_relationship.save(context: :managing_agent) - flash[:notice] = "#{related_organisation.name} is now one of #{current_user.data_coordinator? ? 'your' : "this organisation's"} managing agents" + flash[:notice] = "#{child_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) @@ -97,19 +99,10 @@ class OrganisationRelationshipsController < ApplicationController private - def create!(child_organisation:, parent_organisation:) - @resource = OrganisationRelationship.new(child_organisation:, parent_organisation:) - @resource.save! - end - def organisation @organisation ||= Organisation.find(params[:id]) end - def related_organisation - @related_organisation ||= Organisation.find(params[:organisation_relationship][:related_organisation_id]) - end - def target_organisation @target_organisation ||= Organisation.find(params[:target_organisation_id]) end diff --git a/app/models/organisation_relationship.rb b/app/models/organisation_relationship.rb index 72f74cbb9..bf936f408 100644 --- a/app/models/organisation_relationship.rb +++ b/app/models/organisation_relationship.rb @@ -1,26 +1,17 @@ class OrganisationRelationship < ApplicationRecord belongs_to :child_organisation, class_name: "Organisation" belongs_to :parent_organisation, class_name: "Organisation" - validate :validate_housing_provider_relationship, on: :housing_provider - validate :validate_managing_agent_relationship, on: :managing_agent + validates :parent_organisation_id, presence: { message: "You must choose a housing provider" }, on: :housing_provider + validates :child_organisation_id, presence: { message: "You must choose a managing agent" }, on: :managing_agent + validates :parent_organisation_id, uniqueness: { scope: :child_organisation_id, message: "You have already added this housing provider" }, on: :housing_provider + validates :child_organisation_id, uniqueness: { scope: :parent_organisation_id, message: "You have already added this managing agent" }, on: :managing_agent + validate :validate_housing_provider_owns_stock, on: :housing_provider private - def validate_housing_provider_relationship - if parent_organisation_id.blank? - errors.add :related_organisation_id, "You must choose a housing provider" - elsif OrganisationRelationship.exists?(child_organisation:, parent_organisation:) - errors.add :related_organisation_id, "You have already added this housing provider" - elsif parent_organisation_id.present? && !parent_organisation.holds_own_stock - 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? - errors.add :related_organisation_id, "You must choose a managing agent" - elsif OrganisationRelationship.exists?(child_organisation:, parent_organisation:) - errors.add :related_organisation_id, "You have already added this managing agent" + def validate_housing_provider_owns_stock + if parent_organisation_id.present? && !parent_organisation.holds_own_stock + errors.add :parent_organisation_id, I18n.t("validations.scheme.owning_organisation.does_not_own_stock") end end end diff --git a/app/views/organisation_relationships/_related_organisation_select_question.html.erb b/app/views/organisation_relationships/_related_organisation_select_question.html.erb index cc27c2b8b..551bce5ed 100644 --- a/app/views/organisation_relationships/_related_organisation_select_question.html.erb +++ b/app/views/organisation_relationships/_related_organisation_select_question.html.erb @@ -1,3 +1,3 @@ <% answers = question.answer_options.map { |key, value| OpenStruct.new(id: key, name: value) } %> -<%= f.govuk_collection_select :related_organisation_id, answers, :id, :name, label: { hidden: true }, "data-controller": "accessible-autocomplete" do %> +<%= f.govuk_collection_select field, answers, :id, :name, label: { hidden: true }, "data-controller": "accessible-autocomplete" do %> <% end %> diff --git a/app/views/organisation_relationships/add_housing_provider.html.erb b/app/views/organisation_relationships/add_housing_provider.html.erb index 4171e89af..cb95fd6ac 100644 --- a/app/views/organisation_relationships/add_housing_provider.html.erb +++ b/app/views/organisation_relationships/add_housing_provider.html.erb @@ -18,6 +18,7 @@ <% answer_options[organisation[0]] = organisation[1] %> <% end %> <%= render partial: "organisation_relationships/related_organisation_select_question", locals: { + field: :parent_organisation_id, question: Form::Question.new("", { "answer_options" => answer_options }, nil), f:, } %> diff --git a/app/views/organisation_relationships/add_managing_agent.html.erb b/app/views/organisation_relationships/add_managing_agent.html.erb index 5bf3ac354..173e845ad 100644 --- a/app/views/organisation_relationships/add_managing_agent.html.erb +++ b/app/views/organisation_relationships/add_managing_agent.html.erb @@ -18,6 +18,7 @@ <% answer_options[organisation[0]] = organisation[1] %> <% end %> <%= render partial: "organisation_relationships/related_organisation_select_question", locals: { + field: :child_organisation_id, question: Form::Question.new("", { "answer_options" => answer_options }, nil), f:, } %>