Browse Source

[1711] Orgs parent-child relationship now atomic (#1019)

* remove Organisation#relationship_type

* add indexes and fk constraints to org relations

* remove relationship_type from seeds

- as these have now been removed

* seeding is now idempotent
pull/1031/head
Phil Lee 2 years ago committed by GitHub
parent
commit
3b99974627
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      app/controllers/organisation_relationships_controller.rb
  2. 1
      app/controllers/users_controller.rb
  3. 5
      app/models/organisation.rb
  4. 12
      app/models/organisation_relationship.rb
  5. 5
      db/migrate/20221122122311_delete_relationship_type.rb
  6. 10
      db/migrate/20221122130928_add_org_relation_indexes.rb
  7. 8
      db/schema.rb
  8. 12
      db/seeds.rb
  9. 8
      spec/factories/organisation_relationship.rb
  10. 7
      spec/models/form/lettings/pages/housing_provider_spec.rb
  11. 10
      spec/models/form/lettings/pages/managing_organisation_spec.rb
  12. 8
      spec/models/form/lettings/questions/housing_provider_spec.rb
  13. 16
      spec/models/form/lettings/questions/managing_organisation_spec.rb
  14. 25
      spec/models/organisation_relationship_spec.rb
  15. 20
      spec/models/organisation_spec.rb
  16. 38
      spec/requests/organisation_relationships_controller_spec.rb

16
app/controllers/organisation_relationships_controller.rb

@ -39,7 +39,6 @@ class OrganisationRelationshipsController < ApplicationController
def create_housing_provider def create_housing_provider
child_organisation = @organisation child_organisation = @organisation
relationship_type = OrganisationRelationship::OWNING
if params[:organisation][:related_organisation_id].empty? if params[:organisation][:related_organisation_id].empty?
@organisation.errors.add :related_organisation_id, "You must choose a housing provider" @organisation.errors.add :related_organisation_id, "You must choose a housing provider"
@organisations = Organisation.where.not(id: child_organisation.id).pluck(:id, :name) @organisations = Organisation.where.not(id: child_organisation.id).pluck(:id, :name)
@ -47,21 +46,20 @@ class OrganisationRelationshipsController < ApplicationController
return return
else else
parent_organisation = related_organisation parent_organisation = related_organisation
if OrganisationRelationship.exists?(child_organisation:, parent_organisation:, relationship_type:) if OrganisationRelationship.exists?(child_organisation:, parent_organisation:)
@organisation.errors.add :related_organisation_id, "You have already added this housing provider" @organisation.errors.add :related_organisation_id, "You have already added this housing provider"
@organisations = Organisation.where.not(id: child_organisation.id).pluck(:id, :name) @organisations = Organisation.where.not(id: child_organisation.id).pluck(:id, :name)
render "organisation_relationships/add_housing_provider" render "organisation_relationships/add_housing_provider"
return return
end end
end end
create!(child_organisation:, parent_organisation:, relationship_type:) create!(child_organisation:, parent_organisation:)
flash[:notice] = "#{related_organisation.name} is now one of #{current_user.data_coordinator? ? 'your' : "this organisation's"} housing providers" 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 redirect_to housing_providers_organisation_path
end end
def create_managing_agent def create_managing_agent
parent_organisation = @organisation parent_organisation = @organisation
relationship_type = OrganisationRelationship::MANAGING
if params[:organisation][:related_organisation_id].empty? if params[:organisation][:related_organisation_id].empty?
@organisation.errors.add :related_organisation_id, "You must choose a managing agent" @organisation.errors.add :related_organisation_id, "You must choose a managing agent"
@organisations = Organisation.where.not(id: parent_organisation.id).pluck(:id, :name) @organisations = Organisation.where.not(id: parent_organisation.id).pluck(:id, :name)
@ -69,14 +67,14 @@ class OrganisationRelationshipsController < ApplicationController
return return
else else
child_organisation = related_organisation child_organisation = related_organisation
if OrganisationRelationship.exists?(child_organisation:, parent_organisation:, relationship_type:) if OrganisationRelationship.exists?(child_organisation:, parent_organisation:)
@organisation.errors.add :related_organisation_id, "You have already added this managing agent" @organisation.errors.add :related_organisation_id, "You have already added this managing agent"
@organisations = Organisation.where.not(id: parent_organisation.id).pluck(:id, :name) @organisations = Organisation.where.not(id: parent_organisation.id).pluck(:id, :name)
render "organisation_relationships/add_managing_agent" render "organisation_relationships/add_managing_agent"
return return
end end
end end
create!(child_organisation:, parent_organisation:, relationship_type:) create!(child_organisation:, parent_organisation:)
flash[:notice] = "#{related_organisation.name} is now one of #{current_user.data_coordinator? ? 'your' : "this organisation's"} managing agents" 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 redirect_to managing_agents_organisation_path
end end
@ -89,7 +87,6 @@ class OrganisationRelationshipsController < ApplicationController
relationship = OrganisationRelationship.find_by!( relationship = OrganisationRelationship.find_by!(
child_organisation: @organisation, child_organisation: @organisation,
parent_organisation: target_organisation, parent_organisation: target_organisation,
relationship_type: OrganisationRelationship::OWNING,
) )
relationship.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"
@ -104,7 +101,6 @@ class OrganisationRelationshipsController < ApplicationController
relationship = OrganisationRelationship.find_by!( relationship = OrganisationRelationship.find_by!(
parent_organisation: @organisation, parent_organisation: @organisation,
child_organisation: target_organisation, child_organisation: target_organisation,
relationship_type: OrganisationRelationship::MANAGING,
) )
relationship.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"
@ -113,8 +109,8 @@ class OrganisationRelationshipsController < ApplicationController
private private
def create!(child_organisation:, parent_organisation:, relationship_type:) def create!(child_organisation:, parent_organisation:)
@resource = OrganisationRelationship.new(child_organisation:, parent_organisation:, relationship_type:) @resource = OrganisationRelationship.new(child_organisation:, parent_organisation:)
@resource.save! @resource.save!
end end

1
app/controllers/users_controller.rb

@ -3,6 +3,7 @@ class UsersController < ApplicationController
include Devise::Controllers::SignInOut include Devise::Controllers::SignInOut
include Helpers::Email include Helpers::Email
include Modules::SearchFilter include Modules::SearchFilter
before_action :authenticate_user! before_action :authenticate_user!
before_action :find_resource, except: %i[new create] before_action :find_resource, except: %i[new create]
before_action :authenticate_scope!, except: %i[new] before_action :authenticate_scope!, except: %i[new]

5
app/models/organisation.rb

@ -13,9 +13,10 @@ class Organisation < ApplicationRecord
has_many :child_organisation_relationships, foreign_key: :parent_organisation_id, class_name: "OrganisationRelationship" has_many :child_organisation_relationships, foreign_key: :parent_organisation_id, class_name: "OrganisationRelationship"
has_many :child_organisations, through: :child_organisation_relationships has_many :child_organisations, through: :child_organisation_relationships
has_many :housing_provider_relationships, -> { where(relationship_type: OrganisationRelationship::OWNING) }, foreign_key: :child_organisation_id, class_name: "OrganisationRelationship" has_many :housing_provider_relationships, foreign_key: :child_organisation_id, class_name: "OrganisationRelationship"
has_many :housing_providers, through: :housing_provider_relationships, source: :parent_organisation has_many :housing_providers, through: :housing_provider_relationships, source: :parent_organisation
has_many :managing_agent_relationships, -> { where(relationship_type: OrganisationRelationship::MANAGING) }, foreign_key: :parent_organisation_id, class_name: "OrganisationRelationship"
has_many :managing_agent_relationships, foreign_key: :parent_organisation_id, class_name: "OrganisationRelationship"
has_many :managing_agents, through: :managing_agent_relationships, source: :child_organisation has_many :managing_agents, through: :managing_agent_relationships, source: :child_organisation
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }

12
app/models/organisation_relationship.rb

@ -1,16 +1,4 @@
class OrganisationRelationship < ApplicationRecord class OrganisationRelationship < ApplicationRecord
belongs_to :child_organisation, class_name: "Organisation" belongs_to :child_organisation, class_name: "Organisation"
belongs_to :parent_organisation, class_name: "Organisation" belongs_to :parent_organisation, class_name: "Organisation"
scope :owning, -> { where(relationship_type: OWNING) }
scope :managing, -> { where(relationship_type: MANAGING) }
OWNING = "owning".freeze
MANAGING = "managing".freeze
RELATIONSHIP_TYPE = {
OWNING => 0,
MANAGING => 1,
}.freeze
enum relationship_type: RELATIONSHIP_TYPE
end end

5
db/migrate/20221122122311_delete_relationship_type.rb

@ -0,0 +1,5 @@
class DeleteRelationshipType < ActiveRecord::Migration[7.0]
def change
remove_column :organisation_relationships, :relationship_type, :integer, null: false
end
end

10
db/migrate/20221122130928_add_org_relation_indexes.rb

@ -0,0 +1,10 @@
class AddOrgRelationIndexes < ActiveRecord::Migration[7.0]
def change
add_index :organisation_relationships, :child_organisation_id
add_index :organisation_relationships, :parent_organisation_id
add_index :organisation_relationships, %i[parent_organisation_id child_organisation_id], unique: true, name: "index_org_rel_parent_child_uniq"
add_foreign_key :organisation_relationships, :organisations, column: :parent_organisation_id
add_foreign_key :organisation_relationships, :organisations, column: :child_organisation_id
end
end

8
db/schema.rb

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_11_17_103855) do ActiveRecord::Schema[7.0].define(version: 2022_11_22_130928) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -286,7 +286,9 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_17_103855) do
t.integer "parent_organisation_id" t.integer "parent_organisation_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "relationship_type", null: false t.index ["child_organisation_id"], name: "index_organisation_relationships_on_child_organisation_id"
t.index ["parent_organisation_id", "child_organisation_id"], name: "index_org_rel_parent_child_uniq", unique: true
t.index ["parent_organisation_id"], name: "index_organisation_relationships_on_parent_organisation_id"
end end
create_table "organisation_rent_periods", force: :cascade do |t| create_table "organisation_rent_periods", force: :cascade do |t|
@ -477,6 +479,8 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_17_103855) do
add_foreign_key "lettings_logs", "organisations", column: "owning_organisation_id", on_delete: :cascade add_foreign_key "lettings_logs", "organisations", column: "owning_organisation_id", on_delete: :cascade
add_foreign_key "lettings_logs", "schemes" add_foreign_key "lettings_logs", "schemes"
add_foreign_key "locations", "schemes" add_foreign_key "locations", "schemes"
add_foreign_key "organisation_relationships", "organisations", column: "child_organisation_id"
add_foreign_key "organisation_relationships", "organisations", column: "parent_organisation_id"
add_foreign_key "sales_logs", "organisations", column: "owning_organisation_id", on_delete: :cascade add_foreign_key "sales_logs", "organisations", column: "owning_organisation_id", on_delete: :cascade
add_foreign_key "schemes", "organisations", column: "managing_organisation_id" add_foreign_key "schemes", "organisations", column: "managing_organisation_id"
add_foreign_key "schemes", "organisations", column: "owning_organisation_id", on_delete: :cascade add_foreign_key "schemes", "organisations", column: "owning_organisation_id", on_delete: :cascade

12
db/seeds.rb

@ -67,25 +67,21 @@ unless Rails.env.test?
end end
end end
OrganisationRelationship.create!( OrganisationRelationship.find_or_create_by!(
child_organisation: org, child_organisation: org,
parent_organisation: housing_provider1, parent_organisation: housing_provider1,
relationship_type: OrganisationRelationship::OWNING,
) )
OrganisationRelationship.create!( OrganisationRelationship.find_or_create_by!(
child_organisation: org, child_organisation: org,
parent_organisation: housing_provider2, parent_organisation: housing_provider2,
relationship_type: OrganisationRelationship::OWNING,
) )
OrganisationRelationship.create!( OrganisationRelationship.find_or_create_by!(
child_organisation: managing_agent1, child_organisation: managing_agent1,
parent_organisation: org, parent_organisation: org,
relationship_type: OrganisationRelationship::MANAGING,
) )
OrganisationRelationship.create!( OrganisationRelationship.find_or_create_by!(
child_organisation: managing_agent2, child_organisation: managing_agent2,
parent_organisation: org, parent_organisation: org,
relationship_type: OrganisationRelationship::MANAGING,
) )
if (Rails.env.development? || Rails.env.review?) && User.count.zero? if (Rails.env.development? || Rails.env.review?) && User.count.zero?

8
spec/factories/organisation_relationship.rb

@ -2,13 +2,5 @@ FactoryBot.define do
factory :organisation_relationship do factory :organisation_relationship do
child_organisation { FactoryBot.create(:organisation) } child_organisation { FactoryBot.create(:organisation) }
parent_organisation { FactoryBot.create(:organisation) } parent_organisation { FactoryBot.create(:organisation) }
trait :owning do
relationship_type { OrganisationRelationship::OWNING }
end
trait :managing do
relationship_type { OrganisationRelationship::MANAGING }
end
end end
end end

7
spec/models/form/lettings/pages/housing_provider_spec.rb

@ -79,7 +79,6 @@ RSpec.describe Form::Lettings::Pages::HousingProvider, type: :model do
before do before do
create( create(
:organisation_relationship, :organisation_relationship,
:owning,
child_organisation: user.organisation, child_organisation: user.organisation,
parent_organisation: housing_provider, parent_organisation: housing_provider,
) )
@ -101,13 +100,11 @@ RSpec.describe Form::Lettings::Pages::HousingProvider, type: :model do
before do before do
create( create(
:organisation_relationship, :organisation_relationship,
:owning,
child_organisation: user.organisation, child_organisation: user.organisation,
parent_organisation: housing_provider1, parent_organisation: housing_provider1,
) )
create( create(
:organisation_relationship, :organisation_relationship,
:owning,
child_organisation: user.organisation, child_organisation: user.organisation,
parent_organisation: housing_provider2, parent_organisation: housing_provider2,
) )
@ -140,8 +137,8 @@ RSpec.describe Form::Lettings::Pages::HousingProvider, type: :model do
context "with >0 housing_providers" do context "with >0 housing_providers" do
before do before do
create(:organisation_relationship, :owning, child_organisation: user.organisation) create(:organisation_relationship, child_organisation: user.organisation)
create(:organisation_relationship, :owning, child_organisation: user.organisation) create(:organisation_relationship, child_organisation: user.organisation)
end end
it "is shown" do it "is shown" do

10
spec/models/form/lettings/pages/managing_organisation_spec.rb

@ -76,8 +76,8 @@ RSpec.describe Form::Lettings::Pages::ManagingOrganisation, type: :model do
context "with >1 managing_agents" do context "with >1 managing_agents" do
before do before do
create(:organisation_relationship, :managing, parent_organisation: log.owning_organisation) create(:organisation_relationship, parent_organisation: log.owning_organisation)
create(:organisation_relationship, :managing, parent_organisation: log.owning_organisation) create(:organisation_relationship, parent_organisation: log.owning_organisation)
end end
it "is shown" do it "is shown" do
@ -91,7 +91,6 @@ RSpec.describe Form::Lettings::Pages::ManagingOrganisation, type: :model do
before do before do
create( create(
:organisation_relationship, :organisation_relationship,
:managing,
child_organisation: managing_agent, child_organisation: managing_agent,
parent_organisation: log.owning_organisation, parent_organisation: log.owning_organisation,
) )
@ -128,8 +127,8 @@ RSpec.describe Form::Lettings::Pages::ManagingOrganisation, type: :model do
context "with >1 managing_agents" do context "with >1 managing_agents" do
before do before do
create(:organisation_relationship, :managing, parent_organisation: user.organisation) create(:organisation_relationship, parent_organisation: user.organisation)
create(:organisation_relationship, :managing, parent_organisation: user.organisation) create(:organisation_relationship, parent_organisation: user.organisation)
end end
it "is shown" do it "is shown" do
@ -143,7 +142,6 @@ RSpec.describe Form::Lettings::Pages::ManagingOrganisation, type: :model do
before do before do
create( create(
:organisation_relationship, :organisation_relationship,
:managing,
child_organisation: managing_agent, child_organisation: managing_agent,
parent_organisation: user.organisation, parent_organisation: user.organisation,
) )

8
spec/models/form/lettings/questions/housing_provider_spec.rb

@ -107,7 +107,7 @@ RSpec.describe Form::Lettings::Questions::HousingProvider, type: :model do
context "when housing providers != 0" do context "when housing providers != 0" do
before do before do
create(:organisation_relationship, :owning, child_organisation: user.organisation) create(:organisation_relationship, child_organisation: user.organisation)
end end
it "is visible in check answers" do it "is visible in check answers" do
@ -122,7 +122,7 @@ RSpec.describe Form::Lettings::Questions::HousingProvider, type: :model do
context "when housing providers <= 1" do context "when housing providers <= 1" do
before do before do
create(:organisation_relationship, :owning, child_organisation: user.organisation) create(:organisation_relationship, child_organisation: user.organisation)
end end
it "is hidden in check answers" do it "is hidden in check answers" do
@ -133,8 +133,8 @@ RSpec.describe Form::Lettings::Questions::HousingProvider, type: :model do
context "when housing providers >= 2" do context "when housing providers >= 2" do
before do before do
create(:organisation_relationship, :owning, child_organisation: user.organisation) create(:organisation_relationship, child_organisation: user.organisation)
create(:organisation_relationship, :owning, child_organisation: user.organisation) create(:organisation_relationship, child_organisation: user.organisation)
end end
it "is visible in check answers" do it "is visible in check answers" do

16
spec/models/form/lettings/questions/managing_organisation_spec.rb

@ -56,8 +56,8 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
let(:user) { create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: true)) } let(:user) { create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: true)) }
let(:log) { create(:lettings_log) } let(:log) { create(:lettings_log) }
let!(:org_rel1) { create(:organisation_relationship, :managing, parent_organisation: user.organisation) } let!(:org_rel1) { create(:organisation_relationship, parent_organisation: user.organisation) }
let!(:org_rel2) { create(:organisation_relationship, :managing, parent_organisation: user.organisation) } let!(:org_rel2) { create(:organisation_relationship, parent_organisation: user.organisation) }
let(:options) do let(:options) do
{ {
@ -77,8 +77,8 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
let(:user) { create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: false)) } let(:user) { create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: false)) }
let(:log) { create(:lettings_log) } let(:log) { create(:lettings_log) }
let!(:org_rel1) { create(:organisation_relationship, :managing, parent_organisation: user.organisation) } let!(:org_rel1) { create(:organisation_relationship, parent_organisation: user.organisation) }
let!(:org_rel2) { create(:organisation_relationship, :managing, parent_organisation: user.organisation) } let!(:org_rel2) { create(:organisation_relationship, parent_organisation: user.organisation) }
let(:options) do let(:options) do
{ {
@ -98,8 +98,8 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
let(:user) { create(:user, :support) } let(:user) { create(:user, :support) }
let(:log_owning_org) { create(:organisation, holds_own_stock: false) } let(:log_owning_org) { create(:organisation, holds_own_stock: false) }
let(:log) { create(:lettings_log, owning_organisation: log_owning_org) } let(:log) { create(:lettings_log, owning_organisation: log_owning_org) }
let!(:org_rel1) { create(:organisation_relationship, :managing, parent_organisation: log_owning_org) } let!(:org_rel1) { create(:organisation_relationship, parent_organisation: log_owning_org) }
let!(:org_rel2) { create(:organisation_relationship, :managing, parent_organisation: log_owning_org) } let!(:org_rel2) { create(:organisation_relationship, parent_organisation: log_owning_org) }
let(:options) do let(:options) do
{ {
@ -118,8 +118,8 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
let(:user) { create(:user, :support) } let(:user) { create(:user, :support) }
let(:log_owning_org) { create(:organisation, holds_own_stock: true) } let(:log_owning_org) { create(:organisation, holds_own_stock: true) }
let(:log) { create(:lettings_log, owning_organisation: log_owning_org) } let(:log) { create(:lettings_log, owning_organisation: log_owning_org) }
let!(:org_rel1) { create(:organisation_relationship, :managing, parent_organisation: log_owning_org) } let!(:org_rel1) { create(:organisation_relationship, parent_organisation: log_owning_org) }
let!(:org_rel2) { create(:organisation_relationship, :managing, parent_organisation: log_owning_org) } let!(:org_rel2) { create(:organisation_relationship, parent_organisation: log_owning_org) }
let(:options) do let(:options) do
{ {

25
spec/models/organisation_relationship_spec.rb

@ -0,0 +1,25 @@
require "rails_helper"
RSpec.describe OrganisationRelationship do
let(:parent_organisation) { create(:organisation) }
let(:child_organisation) { create(:organisation) }
context "when a relationship exists" do
subject!(:relationship) do
described_class.create!(parent_organisation:,
child_organisation:)
end
describe "parent#managing_agents" do
it "includes child" do
expect(parent_organisation.managing_agents).to include(child_organisation)
end
end
describe "child#housing_providers" do
it "includes parent" do
expect(child_organisation.housing_providers).to include(parent_organisation)
end
end
end
end

20
spec/models/organisation_spec.rb

@ -34,14 +34,12 @@ RSpec.describe Organisation, type: :model do
before do before do
FactoryBot.create( FactoryBot.create(
:organisation_relationship, :organisation_relationship,
:owning,
child_organisation:, child_organisation:,
parent_organisation: organisation, parent_organisation: organisation,
) )
FactoryBot.create( FactoryBot.create(
:organisation_relationship, :organisation_relationship,
:owning,
child_organisation: grandchild_organisation, child_organisation: grandchild_organisation,
parent_organisation: child_organisation, parent_organisation: child_organisation,
) )
@ -65,21 +63,12 @@ RSpec.describe Organisation, type: :model do
before do before do
FactoryBot.create( FactoryBot.create(
:organisation_relationship, :organisation_relationship,
:managing,
child_organisation:, child_organisation:,
parent_organisation: organisation, parent_organisation: organisation,
) )
FactoryBot.create( FactoryBot.create(
:organisation_relationship, :organisation_relationship,
:owning,
child_organisation:,
parent_organisation: organisation,
)
FactoryBot.create(
:organisation_relationship,
:owning,
child_organisation: grandchild_organisation, child_organisation: grandchild_organisation,
parent_organisation: child_organisation, parent_organisation: child_organisation,
) )
@ -98,21 +87,12 @@ RSpec.describe Organisation, type: :model do
before do before do
FactoryBot.create( FactoryBot.create(
:organisation_relationship, :organisation_relationship,
:managing,
child_organisation:,
parent_organisation: organisation,
)
FactoryBot.create(
:organisation_relationship,
:owning,
child_organisation:, child_organisation:,
parent_organisation: organisation, parent_organisation: organisation,
) )
FactoryBot.create( FactoryBot.create(
:organisation_relationship, :organisation_relationship,
:managing,
child_organisation: grandchild_organisation, child_organisation: grandchild_organisation,
parent_organisation: child_organisation, parent_organisation: child_organisation,
) )

38
spec/requests/organisation_relationships_controller_spec.rb

@ -21,8 +21,8 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") } let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") }
before do before do
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning]) FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider)
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning]) FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider)
get "/organisations/#{organisation.id}/housing-providers", headers:, params: {} get "/organisations/#{organisation.id}/housing-providers", headers:, params: {}
end end
@ -83,8 +83,8 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") } let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") }
before do before do
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing]) FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent)
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing]) FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent)
get "/organisations/#{organisation.id}/managing-agents", headers:, params: {} get "/organisations/#{organisation.id}/managing-agents", headers:, params: {}
end end
@ -153,7 +153,7 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
it "sets the organisation relationship attributes correctly" do it "sets the organisation relationship attributes correctly" do
request request
expect(OrganisationRelationship).to exist(child_organisation_id: organisation.id, parent_organisation_id: housing_provider.id, relationship_type: OrganisationRelationship::OWNING) expect(OrganisationRelationship).to exist(child_organisation_id: organisation.id, parent_organisation_id: housing_provider.id)
end end
it "redirects to the organisation list" do it "redirects to the organisation list" do
@ -181,7 +181,7 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
it "sets the organisation relationship attributes correctly" do it "sets the organisation relationship attributes correctly" do
request request
expect(OrganisationRelationship).to exist(parent_organisation_id: organisation.id, child_organisation_id: managing_agent.id, relationship_type: OrganisationRelationship::MANAGING) expect(OrganisationRelationship).to exist(parent_organisation_id: organisation.id, child_organisation_id: managing_agent.id)
end end
it "redirects to the organisation list" do it "redirects to the organisation list" do
@ -200,7 +200,7 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let(:request) { delete "/organisations/#{organisation.id}/housing-providers", headers:, params: } let(:request) { delete "/organisations/#{organisation.id}/housing-providers", headers:, params: }
before do before do
FactoryBot.create(:organisation_relationship, :owning, child_organisation: organisation, parent_organisation: housing_provider) FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider)
end end
it "deletes the new organisation relationship" do it "deletes the new organisation relationship" do
@ -225,7 +225,6 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
before do before do
FactoryBot.create( FactoryBot.create(
:organisation_relationship, :organisation_relationship,
:managing,
parent_organisation: organisation, parent_organisation: organisation,
child_organisation: managing_agent, child_organisation: managing_agent,
) )
@ -256,8 +255,8 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") } let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") }
before do before do
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning]) FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider)
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning]) FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider)
get "/organisations/#{organisation.id}/housing-providers", headers:, params: {} get "/organisations/#{organisation.id}/housing-providers", headers:, params: {}
end end
@ -304,8 +303,8 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") } let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD") }
before do before do
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing]) FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent)
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing]) FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent)
get "/organisations/#{organisation.id}/managing-agents", headers:, params: {} get "/organisations/#{organisation.id}/managing-agents", headers:, params: {}
end end
@ -383,7 +382,7 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
it "sets the organisation relationship attributes correctly" do it "sets the organisation relationship attributes correctly" do
request request
expect(OrganisationRelationship).to exist(child_organisation_id: organisation.id, parent_organisation_id: housing_provider.id, relationship_type: OrganisationRelationship::OWNING) expect(OrganisationRelationship).to exist(child_organisation_id: organisation.id, parent_organisation_id: housing_provider.id)
end end
it "redirects to the organisation list" do it "redirects to the organisation list" do
@ -411,7 +410,7 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
it "sets the organisation relationship attributes correctly" do it "sets the organisation relationship attributes correctly" do
request request
expect(OrganisationRelationship).to exist(parent_organisation_id: organisation.id, child_organisation_id: managing_agent.id, relationship_type: OrganisationRelationship::MANAGING) expect(OrganisationRelationship).to exist(parent_organisation_id: organisation.id, child_organisation_id: managing_agent.id)
end end
it "redirects to the organisation list" do it "redirects to the organisation list" do
@ -430,7 +429,7 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let(:request) { delete "/organisations/#{organisation.id}/housing-providers", headers:, params: } let(:request) { delete "/organisations/#{organisation.id}/housing-providers", headers:, params: }
before do before do
FactoryBot.create(:organisation_relationship, :owning, child_organisation: organisation, parent_organisation: housing_provider) FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider)
end end
it "deletes the new organisation relationship" do it "deletes the new organisation relationship" do
@ -455,7 +454,6 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
before do before do
FactoryBot.create( FactoryBot.create(
:organisation_relationship, :organisation_relationship,
:managing,
parent_organisation: organisation, parent_organisation: organisation,
child_organisation: managing_agent, child_organisation: managing_agent,
) )
@ -477,8 +475,8 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") } let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") }
before do before do
FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning]) FactoryBot.create(:organisation_relationship, child_organisation: organisation, parent_organisation: housing_provider)
FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider, relationship_type: OrganisationRelationship.relationship_types[:owning]) FactoryBot.create(:organisation_relationship, child_organisation: other_organisation, parent_organisation: other_org_housing_provider)
get "/organisations/#{organisation.id}/housing-providers", headers:, params: {} get "/organisations/#{organisation.id}/housing-providers", headers:, params: {}
end end
@ -531,8 +529,8 @@ RSpec.describe OrganisationRelationshipsController, type: :request do
let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") } let!(:other_organisation) { FactoryBot.create(:organisation, name: "Foobar LTD 2") }
before do before do
FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing]) FactoryBot.create(:organisation_relationship, parent_organisation: organisation, child_organisation: managing_agent)
FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent, relationship_type: OrganisationRelationship.relationship_types[:managing]) FactoryBot.create(:organisation_relationship, parent_organisation: other_organisation, child_organisation: other_org_managing_agent)
get "/organisations/#{organisation.id}/managing-agents", headers:, params: {} get "/organisations/#{organisation.id}/managing-agents", headers:, params: {}
end end

Loading…
Cancel
Save