From 3b88f2a390d3ab560371cb71c680b110a9a786d4 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 29 Nov 2023 14:58:15 +0000 Subject: [PATCH] Don't display available from if it doesn't exist (#2059) --- .../form/lettings/questions/stock_owner.rb | 4 +-- .../sales/questions/owning_organisation_id.rb | 4 +-- .../lettings/questions/stock_owner_spec.rb | 27 ++++++++++++++++++ .../questions/owning_organisation_id_spec.rb | 28 ++++++++++++++++++- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/app/models/form/lettings/questions/stock_owner.rb b/app/models/form/lettings/questions/stock_owner.rb index 745350aa4..c78c4a080 100644 --- a/app/models/form/lettings/questions/stock_owner.rb +++ b/app/models/form/lettings/questions/stock_owner.rb @@ -32,8 +32,8 @@ class Form::Lettings::Questions::StockOwner < ::Form::Question 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)})" + elsif org.absorbed_organisations.merged_during_open_collection_period.exists? && org.available_from.present? + answer_opts[org.id] = "#{org.name} (active as of #{org.available_from.to_fs(:govuk_date)})" else answer_opts[org.id] = org.name end diff --git a/app/models/form/sales/questions/owning_organisation_id.rb b/app/models/form/sales/questions/owning_organisation_id.rb index 529d14f66..a53f99dc3 100644 --- a/app/models/form/sales/questions/owning_organisation_id.rb +++ b/app/models/form/sales/questions/owning_organisation_id.rb @@ -33,8 +33,8 @@ class Form::Sales::Questions::OwningOrganisationId < ::Form::Question 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)})" + elsif org.absorbed_organisations.merged_during_open_collection_period.exists? && org.available_from.present? + answer_opts[org.id] = "#{org.name} (active as of #{org.available_from.to_fs(:govuk_date)})" else answer_opts[org.id] = org.name end diff --git a/spec/models/form/lettings/questions/stock_owner_spec.rb b/spec/models/form/lettings/questions/stock_owner_spec.rb index 985a12432..74ca0d088 100644 --- a/spec/models/form/lettings/questions/stock_owner_spec.rb +++ b/spec/models/form/lettings/questions/stock_owner_spec.rb @@ -221,6 +221,33 @@ RSpec.describe Form::Lettings::Questions::StockOwner, type: :model do expect(question.displayed_answer_options(log, user)).to eq(expected_opts) expect(question.displayed_answer_options(log, user)).not_to include(non_stock_organisation.id) end + + context "and org has recently absorbed other orgs and does not have available from date" do + let(:merged_organisation) { create(:organisation, name: "Merged org") } + let(:org) { create(:organisation, name: "User org") } + let(:options) do + { + "" => "Select an option", + org.id => "User org", + user.organisation.id => user.organisation.name, + log.owning_organisation.id => log.owning_organisation.name, + 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: org) + Timecop.freeze(Time.zone.local(2023, 11, 10)) + end + + after do + Timecop.return + end + + it "shows merged organisation as an option" do + expect(question.displayed_answer_options(log, user)).to eq(options) + end + end end end 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 79d8be710..ceb081459 100644 --- a/spec/models/form/sales/questions/owning_organisation_id_spec.rb +++ b/spec/models/form/sales/questions/owning_organisation_id_spec.rb @@ -224,7 +224,7 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do before do merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation: organisation_1) - organisation_1.update!(created_at: Time.zone.local(2021, 2, 2)) + organisation_1.update!(created_at: Time.zone.local(2021, 3, 2), available_from: Time.zone.local(2021, 2, 2)) Timecop.freeze(Time.zone.local(2023, 11, 10)) end @@ -257,6 +257,32 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do expect(question.displayed_answer_options(log, user)).to eq(options) end end + + context "when an existing org has recently absorbed other orgs" do + let(:merged_organisation) { create(:organisation, name: "Merged org") } + let(:options) do + { + "" => "Select an option", + organisation_1.id => "first test org", + organisation_2.id => "second test org", + 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: organisation_1) + organisation_1.update!(created_at: Time.zone.local(2021, 2, 2), available_from: nil) + Timecop.freeze(Time.zone.local(2023, 11, 10)) + end + + after do + Timecop.return + end + + it "does not show abailable from for absorbing organisation" do + expect(question.displayed_answer_options(log, user)).to eq(options) + end + end end end