diff --git a/app/models/form/lettings/questions/managing_organisation.rb b/app/models/form/lettings/questions/managing_organisation.rb index e22bf0c3d..2e955fd22 100644 --- a/app/models/form/lettings/questions/managing_organisation.rb +++ b/app/models/form/lettings/questions/managing_organisation.rb @@ -23,8 +23,8 @@ 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)})" + elsif user.organisation.absorbed_organisations.exists? && user.organisation.available_from.present? + opts[user.organisation.id] = "#{user.organisation.name} (Your organisation, active as of #{user.organisation.available_from.to_fs(:govuk_date)})" else opts[user.organisation.id] = "#{user.organisation.name} (Your organisation)" end diff --git a/app/models/form/lettings/questions/stock_owner.rb b/app/models/form/lettings/questions/stock_owner.rb index 8fc42e66d..745350aa4 100644 --- a/app/models/form/lettings/questions/stock_owner.rb +++ b/app/models/form/lettings/questions/stock_owner.rb @@ -16,20 +16,38 @@ class Form::Lettings::Questions::StockOwner < ::Form::Question return answer_opts unless log if log.owning_organisation_id.present? - answer_opts = answer_opts.merge({ log.owning_organisation.id => log.owning_organisation.name }) + answer_opts[log.owning_organisation.id] = log.owning_organisation.name end + recently_absorbed_organisations = user.organisation.absorbed_organisations.merged_during_open_collection_period if !user.support? && user.organisation.holds_own_stock? - answer_opts[user.organisation.id] = "#{user.organisation.name} (Your organisation)" + answer_opts[user.organisation.id] = if recently_absorbed_organisations.exists? && user.organisation.available_from.present? + "#{user.organisation.name} (Your organisation, active as of #{user.organisation.available_from.to_fs(:govuk_date)})" + else + "#{user.organisation.name} (Your organisation)" + end end - user_answer_options = if user.support? - Organisation.where(holds_own_stock: true) - else - user.organisation.stock_owners + user.organisation.absorbed_organisations.where(holds_own_stock: true) - end.pluck(:id, :name).to_h + if user.support? + Organisation.where(holds_own_stock: true).find_each do |org| + if org.merge_date.present? + answer_opts[org.id] = "#{org.name} (inactive as of #{org.merge_date.to_fs(:govuk_date)})" if org.merge_date >= FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period + elsif org.absorbed_organisations.merged_during_open_collection_period.exists? + answer_opts[org.id] = "#{org.name} (active as of #{org.created_at.to_fs(:govuk_date)})" + else + answer_opts[org.id] = org.name + end + end + else + user.organisation.stock_owners.each do |stock_owner| + answer_opts[stock_owner.id] = stock_owner.name + end + recently_absorbed_organisations.each do |absorbed_org| + answer_opts[absorbed_org.id] = merged_organisation_label(absorbed_org.name, absorbed_org.merge_date) if absorbed_org.holds_own_stock? + end + end - answer_opts.merge(user_answer_options) + answer_opts end def displayed_answer_options(log, user = nil) @@ -71,4 +89,8 @@ private def selected_answer_option_is_derived?(_log) true end + + def merged_organisation_label(name, merge_date) + "#{name} (inactive as of #{merge_date.to_fs(:govuk_date)})" + end end diff --git a/app/models/form/sales/questions/owning_organisation_id.rb b/app/models/form/sales/questions/owning_organisation_id.rb index 43e97add6..529d14f66 100644 --- a/app/models/form/sales/questions/owning_organisation_id.rb +++ b/app/models/form/sales/questions/owning_organisation_id.rb @@ -22,8 +22,8 @@ class Form::Sales::Questions::OwningOrganisationId < ::Form::Question recently_absorbed_organisations = user.organisation.absorbed_organisations.merged_during_open_collection_period if !user.support? && user.organisation.holds_own_stock? - answer_opts[user.organisation.id] = if recently_absorbed_organisations.exists? - "#{user.organisation.name} (Your organisation, active as of #{user.organisation.created_at.to_fs(:govuk_date)})" + answer_opts[user.organisation.id] = if recently_absorbed_organisations.exists? && user.organisation.available_from.present? + "#{user.organisation.name} (Your organisation, active as of #{user.organisation.available_from.to_fs(:govuk_date)})" else "#{user.organisation.name} (Your organisation)" end diff --git a/spec/models/form/lettings/questions/managing_organisation_spec.rb b/spec/models/form/lettings/questions/managing_organisation_spec.rb index 64bb84181..dcd142151 100644 --- a/spec/models/form/lettings/questions/managing_organisation_spec.rb +++ b/spec/models/form/lettings/questions/managing_organisation_spec.rb @@ -163,7 +163,7 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do 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(:absorbing_org) { create(:organisation, name: "Absorbing org", holds_own_stock: true) } let!(:merged_org) { create(:organisation, name: "Merged org", holds_own_stock: false) } let(:user) { create(:user, :data_coordinator, organisation: absorbing_org) } @@ -173,6 +173,17 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do end it "displays merged organisation on the list of choices" do + options = { + "" => "Select an option", + absorbing_org.id => "Absorbing org (Your organisation)", + 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 active date for absorbing organisation if available from is given" do + absorbing_org.update!(available_from: Time.zone.local(2023, 8, 3)) options = { "" => "Select an option", absorbing_org.id => "Absorbing org (Your organisation, active as of 3 August 2023)", @@ -189,7 +200,7 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do 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)", + absorbing_org.id => "Absorbing org (Your organisation)", managing_agent.id => "Managing org 1", } diff --git a/spec/models/form/lettings/questions/stock_owner_spec.rb b/spec/models/form/lettings/questions/stock_owner_spec.rb index ed05c5139..6957a293a 100644 --- a/spec/models/form/lettings/questions/stock_owner_spec.rb +++ b/spec/models/form/lettings/questions/stock_owner_spec.rb @@ -100,6 +100,93 @@ RSpec.describe Form::Lettings::Questions::StockOwner, type: :model do expect(question.displayed_answer_options(log, user)).to eq(options) end end + + context "when user's org has recently absorbed other orgs and has available_from date" do + let(:merged_organisation) { create(:organisation, name: "Merged org") } + let(:options) do + { + "" => "Select an option", + user.organisation.id => "User org (Your organisation, active as of 2 February 2021)", + owning_org_2.id => "Owning org 2", + owning_org_1.id => "Owning org 1", + merged_organisation.id => "Merged org (inactive as of 2 February 2023)", + } + end + + before do + merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation: user.organisation) + user.organisation.update!(available_from: Time.zone.local(2021, 2, 2)) + end + + it "shows merged organisation as an option" do + expect(question.displayed_answer_options(log, user)).to eq(options) + end + end + + context "when user's org has recently absorbed other orgs and does not have available from date" do + let(:merged_organisation) { create(:organisation, name: "Merged org") } + let(:options) do + { + "" => "Select an option", + user.organisation.id => "User org (Your organisation)", + owning_org_2.id => "Owning org 2", + owning_org_1.id => "Owning org 1", + merged_organisation.id => "Merged org (inactive as of 2 February 2023)", + } + end + + before do + merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation: user.organisation) + end + + it "shows merged organisation as an option" do + expect(question.displayed_answer_options(log, user)).to eq(options) + end + end + + context "when user's org has recently absorbed other orgs with parent organisations" do + let(:merged_organisation) { create(:organisation, name: "Merged org") } + let(:options) do + { + "" => "Select an option", + user.organisation.id => "User org (Your organisation, active as of 2 February 2021)", + owning_org_1.id => "Owning org 1", + merged_organisation.id => "Merged org (inactive as of 2 February 2023)", + } + end + + before do + org_rel.update!(child_organisation: merged_organisation) + merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation: user.organisation) + user.organisation.update!(available_from: Time.zone.local(2021, 2, 2)) + end + + it "does not show merged organisations stock owners as options" do + expect(question.displayed_answer_options(log, user)).to eq(options) + end + end + + context "when user's org has absorbed other orgs with parent organisations during closed collection periods" do + let(:merged_organisation) { create(:organisation, name: "Merged org") } + let(:options) do + { + "" => "Select an option", + user.organisation.id => "User org (Your organisation)", + owning_org_1.id => "Owning org 1", + } + end + + before do + Timecop.freeze(Time.zone.local(2023, 4, 2)) + org_rel.update!(child_organisation: merged_organisation) + merged_organisation.update!(merge_date: Time.zone.local(2021, 6, 2), absorbing_organisation: user.organisation) + user.organisation.update!(available_from: Time.zone.local(2021, 2, 2)) + end + + it "shows merged organisation as an option" do + expect(question.displayed_answer_options(log, user)).to eq(options) + end + end end context "when user is support" do diff --git a/spec/models/form/sales/questions/owning_organisation_id_spec.rb b/spec/models/form/sales/questions/owning_organisation_id_spec.rb index 9cfefbf8b..c82508324 100644 --- a/spec/models/form/sales/questions/owning_organisation_id_spec.rb +++ b/spec/models/form/sales/questions/owning_organisation_id_spec.rb @@ -98,7 +98,7 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do let(:options) do { "" => "Select an option", - user.organisation.id => "User org (Your organisation, active as of 2 February 2021)", + user.organisation.id => "User org (Your organisation)", owning_org_1.id => "Owning org 1", merged_organisation.id => "Merged org (inactive as of 2 February 2023)", } @@ -106,7 +106,6 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do before do merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation: user.organisation) - user.organisation.update!(created_at: Time.zone.local(2021, 2, 2)) end it "shows merged organisation as an option" do @@ -114,7 +113,7 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do end end - context "when user's org has recently absorbed other orgs with parent organisations" do + context "when user's org has recently absorbed other orgs and it has available from date" do let(:merged_organisation) { create(:organisation, name: "Merged org") } let(:options) do { @@ -125,10 +124,30 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do } end + before do + merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation: user.organisation) + user.organisation.update!(available_from: Time.zone.local(2021, 2, 2)) + end + + it "shows available from date if it is given" do + expect(question.displayed_answer_options(log, user)).to eq(options) + end + end + + context "when user's org has recently absorbed other orgs with parent organisations" do + let(:merged_organisation) { create(:organisation, name: "Merged org") } + let(:options) do + { + "" => "Select an option", + user.organisation.id => "User org (Your organisation)", + owning_org_1.id => "Owning org 1", + merged_organisation.id => "Merged org (inactive as of 2 February 2023)", + } + end + before do org_rel.update!(child_organisation: merged_organisation) merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation: user.organisation) - user.organisation.update!(created_at: Time.zone.local(2021, 2, 2)) end it "does not show merged organisations stock owners as options" do @@ -150,7 +169,7 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do Timecop.freeze(Time.zone.local(2023, 4, 2)) org_rel.update!(child_organisation: merged_organisation) merged_organisation.update!(merge_date: Time.zone.local(2021, 6, 2), absorbing_organisation: user.organisation) - user.organisation.update!(created_at: Time.zone.local(2021, 2, 2)) + user.organisation.update!(available_from: Time.zone.local(2021, 2, 2)) end it "shows merged organisation as an option" do