Browse Source

feat: move org relationship errors to model

CLDC-1730-stock-owning-validation2
natdeanlewissoftwire 2 years ago
parent
commit
5535d778ec
  1. 53
      app/controllers/organisation_relationships_controller.rb
  2. 24
      app/models/organisation_relationship.rb
  3. 2
      app/views/organisation_relationships/managing_agents.html.erb

53
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

24
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

2
app/views/organisation_relationships/managing_agents.html.erb

@ -11,7 +11,7 @@
<p class="govuk-body">This organisation does not currently have any managing agents.</p>
<% 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 } %>
<p class="govuk-body">A managing agent can submit logs for this organisation.</p>
<% if @total_count == 0 %>
<p class="govuk-body">This organisation does not currently have any managing agents.</p>

Loading…
Cancel
Save