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 10ac1fb63..745350aa4 100644 --- a/app/models/form/lettings/questions/stock_owner.rb +++ b/app/models/form/lettings/questions/stock_owner.rb @@ -21,8 +21,8 @@ class Form::Lettings::Questions::StockOwner < ::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/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 bccf1bba8..6957a293a 100644 --- a/spec/models/form/lettings/questions/stock_owner_spec.rb +++ b/spec/models/form/lettings/questions/stock_owner_spec.rb @@ -101,7 +101,7 @@ RSpec.describe Form::Lettings::Questions::StockOwner, type: :model do end end - context "when user's org has recently absorbed other orgs" do + 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 { @@ -115,7 +115,28 @@ RSpec.describe Form::Lettings::Questions::StockOwner, 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)) + 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 @@ -137,7 +158,7 @@ RSpec.describe Form::Lettings::Questions::StockOwner, type: :model do 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)) + user.organisation.update!(available_from: Time.zone.local(2021, 2, 2)) end it "does not show merged organisations stock owners as options" do @@ -159,7 +180,7 @@ RSpec.describe Form::Lettings::Questions::StockOwner, 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 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