Browse Source

feat: refactor create logic

pull/954/head
natdeanlewissoftwire 3 years ago
parent
commit
910810e1be
  1. 37
      app/controllers/organisation_relationships_controller.rb
  2. 13
      app/views/organisation_relationships/housing_providers.html.erb

37
app/controllers/organisation_relationships_controller.rb

@ -30,37 +30,30 @@ class OrganisationRelationshipsController < ApplicationController
end
def create_housing_provider
create(0)
end
def create_managing_agent
create(1)
end
def create(relationship_type)
child_organisation_id = @organisation.id
parent_organisation_id = related_organisation_id
if related_organisation_id.empty?
@organisation.errors.add :related_organisation_id, "You must choose a housing provider"
@organisations = Organisation.where.not(id: @organisation.id).pluck(:id, :name)
@organisations = Organisation.where.not(id: child_organisation_id).pluck(:id, :name)
render 'organisation_relationships/add_housing_provider'
return
end
case relationship_type
when 0
if OrganisationRelationship.exists?(child_organisation_id: @organisation.id, parent_organisation_id: related_organisation_id, relationship_type: 0)
elsif OrganisationRelationship.exists?(child_organisation_id:, parent_organisation_id:, relationship_type: 0)
@organisation.errors.add :related_organisation_id, "You have already added this housing provider"
@organisations = Organisation.where.not(id: @organisation.id).pluck(:id, :name)
@organisations = Organisation.where.not(id: child_organisation_id).pluck(:id, :name)
render 'organisation_relationships/add_housing_provider'
else
@resource = OrganisationRelationship.new(child_organisation_id: @organisation.id, parent_organisation_id: related_organisation_id, relationship_type: 0)
@resource.save!
return
end
create(child_organisation_id, parent_organisation_id, 0)
redirect_to housing_providers_organisation_path(related_organisation_id:)
end
when 1
@resource = OrganisationRelationship.new(child_organisation_id: related_organisation_id, parent_organisation_id: @organisation.id, relationship_type: 1)
@resource.save!
redirect_to managing_agents_organisation_path
def create_managing_agent
create(related_organisation_id, @organisation.id, 1)
end
def create(child_organisation_id, parent_organisation_id, relationship_type)
@resource = OrganisationRelationship.new(child_organisation_id:, parent_organisation_id:, relationship_type:)
@resource.save!
end
private
@ -79,7 +72,7 @@ private
end
def authenticate_scope!
if current_user.organisation != organisation
if current_user.organisation != organisation && !current_user.support?
render_not_found
end
end

13
app/views/organisation_relationships/housing_providers.html.erb

@ -23,15 +23,22 @@
notification_banner.heading(text: "#{Organisation.find(params["related_organisation_id"]).name} is now one of your housing providers")
end %>
<% end %>
<% if @total_count == 0 %>
<p class="govuk-body">This organisation does not currently have any housing providers.</p>
<% else %>
<p class="govuk-body">This organisation can submit logs for its housing providers.</p>
<% end %>
<% else %>
<%= render partial: "organisations/headings", locals: { main: "Your housing providers", sub: nil } %>
<% if @total_count == 0 %>
<p class="govuk-body">Your organisation does not currently have any housing providers.</p>
<% else %>
<p class="govuk-body">Your organisation can submit logs for its housing providers.</p>
<% end %>
<% end %>
<% if @total_count == 0 %>
<p class="govuk-body">This organisation does not currently have any housing providers.</p>
<% end %>
<% if current_user.support? %>
<%= govuk_button_link_to "Add a housing provider", housing_providers_add_organisation_path(organisation_id: @organisation.id), html: { method: :get } %>
<% elsif current_user.data_coordinator? %>

Loading…
Cancel
Save