Browse Source

CLDC-2106 Add merged organisations to managing org dropdown (#1819)

* Add availability to the merged org options

* Do not delete merged organisation relationships

* displays managing agents of merged organisation selected as owning org

* Add absorbing org dates, fix copy

* set managing org as owning org it is being absorbed by the users org
pull/1833/head
kosiakkatrina 1 year ago committed by GitHub
parent
commit
d9954d9723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/controllers/form_controller.rb
  2. 10
      app/models/form/lettings/questions/managing_organisation.rb
  3. 8
      app/services/merge/merge_organisations_service.rb
  4. 36
      spec/models/form/lettings/questions/managing_organisation_spec.rb
  5. 30
      spec/requests/form_controller_spec.rb

6
app/controllers/form_controller.rb

@ -102,9 +102,11 @@ private
result[question.id] = question_params
end
if current_user.support? && question.id == "owning_organisation_id" && @log.lettings? && @log.managing_organisation.blank?
if question.id == "owning_organisation_id" && @log.lettings?
owning_organisation = Organisation.find(result["owning_organisation_id"])
if owning_organisation&.managing_agents&.empty?
if current_user.support? && @log.managing_organisation.blank? && owning_organisation&.managing_agents&.empty?
result["managing_organisation_id"] = owning_organisation.id
elsif owning_organisation&.absorbing_organisation == current_user.organisation
result["managing_organisation_id"] = owning_organisation.id
end
end

10
app/models/form/lettings/questions/managing_organisation.rb

@ -23,16 +23,24 @@ class Form::Lettings::Questions::ManagingOrganisation < ::Form::Question
if log.owning_organisation.holds_own_stock?
opts[log.owning_organisation.id] = "#{log.owning_organisation.name} (Owning organisation)"
end
elsif user.organisation.absorbed_organisations.exists?
opts[user.organisation.id] = "#{user.organisation.name} (Your organisation, active as of #{user.organisation.created_at.to_fs(:govuk_date)})"
else
opts[user.organisation.id] = "#{user.organisation.name} (Your organisation)"
end
orgs = if user.support?
log.owning_organisation.managing_agents
elsif user.organisation.absorbed_organisations.include?(log.owning_organisation)
user.organisation.managing_agents + log.owning_organisation.managing_agents
else
user.organisation.managing_agents + user.organisation.absorbed_organisations
user.organisation.managing_agents
end.pluck(:id, :name).to_h
user.organisation.absorbed_organisations.each do |absorbed_org|
opts[absorbed_org.id] = "#{absorbed_org.name} (inactive as of #{absorbed_org.merge_date.to_fs(:govuk_date)})"
end
opts.merge(orgs)
end

8
app/services/merge/merge_organisations_service.rb

@ -43,14 +43,14 @@ private
if parent_relationship_exists_on_absorbing_organisation?(parent_organisation_relationship)
parent_organisation_relationship.destroy!
else
parent_organisation_relationship.update!(child_organisation: @absorbing_organisation)
OrganisationRelationship.create!(parent_organisation: parent_organisation_relationship.parent_organisation, child_organisation: @absorbing_organisation)
end
end
merging_organisation.child_organisation_relationships.each do |child_organisation_relationship|
if child_relationship_exists_on_absorbing_organisation?(child_organisation_relationship)
child_organisation_relationship.destroy!
else
child_organisation_relationship.update!(parent_organisation: @absorbing_organisation)
OrganisationRelationship.create!(parent_organisation: @absorbing_organisation, child_organisation: child_organisation_relationship.child_organisation)
end
end
end
@ -122,10 +122,10 @@ private
end
def parent_relationship_exists_on_absorbing_organisation?(parent_organisation_relationship)
parent_organisation_relationship.parent_organisation == @absorbing_organisation || @absorbing_organisation.parent_organisation_relationships.where(parent_organisation: parent_organisation_relationship.parent_organisation).exists?
parent_organisation_relationship.parent_organisation == @absorbing_organisation || @merging_organisations.include?(parent_organisation_relationship.parent_organisation) || @absorbing_organisation.parent_organisation_relationships.where(parent_organisation: parent_organisation_relationship.parent_organisation).exists?
end
def child_relationship_exists_on_absorbing_organisation?(child_organisation_relationship)
child_organisation_relationship.child_organisation == @absorbing_organisation || @absorbing_organisation.child_organisation_relationships.where(child_organisation: child_organisation_relationship.child_organisation).exists?
child_organisation_relationship.child_organisation == @absorbing_organisation || @merging_organisations.include?(child_organisation_relationship.child_organisation) || @absorbing_organisation.child_organisation_relationships.where(child_organisation: child_organisation_relationship.child_organisation).exists?
end
end

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

@ -161,6 +161,42 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
expect(question.displayed_answer_options(log, user)).to eq(options)
end
end
context "when organisation has merged" do
let(:absorbing_org) { create(:organisation, name: "Absorbing org", holds_own_stock: true, created_at: Time.zone.local(2023, 8, 3)) }
let!(:merged_org) { create(:organisation, name: "Merged org", holds_own_stock: false) }
let(:user) { create(:user, :data_coordinator, organisation: absorbing_org) }
let(:log) do
merged_org.update!(merge_date: Time.zone.local(2023, 8, 2), absorbing_organisation_id: absorbing_org.id)
create(:lettings_log, owning_organisation: absorbing_org, managing_organisation: nil)
end
it "displays merged organisation on the list of choices" do
options = {
"" => "Select an option",
absorbing_org.id => "Absorbing org (Your organisation, active as of 3 August 2023)",
merged_org.id => "Merged org (inactive as of 2 August 2023)",
merged_org.id => "Merged org (inactive as of 2 August 2023)",
}
expect(question.displayed_answer_options(log, user)).to eq(options)
end
it "displays managing agents of merged organisation selected as owning org" do
managing_agent = create(:organisation, name: "Managing org 1")
create(:organisation_relationship, parent_organisation: merged_org, child_organisation: managing_agent)
options = {
"" => "Select an option",
merged_org.id => "Merged org (inactive as of 2 August 2023)",
absorbing_org.id => "Absorbing org (Your organisation, active as of 3 August 2023)",
managing_agent.id => "Managing org 1",
}
log.update!(owning_organisation: merged_org)
expect(question.displayed_answer_options(log, user)).to eq(options)
end
end
end
it "is marked as derived" do

30
spec/requests/form_controller_spec.rb

@ -417,7 +417,7 @@ RSpec.describe FormController, type: :request do
describe "Submit Form" do
context "with a form page" do
let(:user) { create(:user) }
let(:user) { create(:user, :data_coordinator) }
let(:support_user) { FactoryBot.create(:user, :support) }
let(:organisation) { user.organisation }
let(:lettings_log) do
@ -733,6 +733,34 @@ RSpec.describe FormController, type: :request do
end
end
context "when owning organisation is an organisation merged into user organisation" do
let(:params) do
{
id: lettings_log.id,
lettings_log: {
page: "stock_owner",
owning_organisation_id: merged_org.id,
},
}
end
let(:merged_org) { create(:organisation) }
before do
lettings_log.update!(owning_organisation: nil)
lettings_log.reload
merged_org.update!(merge_date: Time.zone.today, absorbing_organisation: organisation)
create(:organisation_relationship, parent_organisation: organisation)
end
it "sets managing organisation to owning organisation" do
post "/lettings-logs/#{lettings_log.id}/stock-owner", params: params
expect(response).to redirect_to("/lettings-logs/#{lettings_log.id}/managing-organisation")
follow_redirect!
lettings_log.reload
expect(lettings_log.managing_organisation).to eq(merged_org)
end
end
context "when the question was accessed from a duplicate logs screen" do
let(:lettings_log) { create(:lettings_log, :duplicate, created_by: user) }
let(:duplicate_log) { create(:lettings_log, :duplicate, created_by: user) }

Loading…
Cancel
Save