Browse Source

CLDC-2105 Display merged orgs for owning organisation question (#1997)

* Display merged orgs for owning org

* Display available from date for absorbing orgs
pull/2023/head v0.3.75
kosiakkatrina 1 year ago committed by GitHub
parent
commit
996a6d0757
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/models/form/lettings/questions/managing_organisation.rb
  2. 36
      app/models/form/lettings/questions/stock_owner.rb
  3. 4
      app/models/form/sales/questions/owning_organisation_id.rb
  4. 15
      spec/models/form/lettings/questions/managing_organisation_spec.rb
  5. 87
      spec/models/form/lettings/questions/stock_owner_spec.rb
  6. 29
      spec/models/form/sales/questions/owning_organisation_id_spec.rb

4
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? if log.owning_organisation.holds_own_stock?
opts[log.owning_organisation.id] = "#{log.owning_organisation.name} (Owning organisation)" opts[log.owning_organisation.id] = "#{log.owning_organisation.name} (Owning organisation)"
end end
elsif user.organisation.absorbed_organisations.exists? 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.created_at.to_fs(:govuk_date)})" opts[user.organisation.id] = "#{user.organisation.name} (Your organisation, active as of #{user.organisation.available_from.to_fs(:govuk_date)})"
else else
opts[user.organisation.id] = "#{user.organisation.name} (Your organisation)" opts[user.organisation.id] = "#{user.organisation.name} (Your organisation)"
end end

36
app/models/form/lettings/questions/stock_owner.rb

@ -16,20 +16,38 @@ class Form::Lettings::Questions::StockOwner < ::Form::Question
return answer_opts unless log return answer_opts unless log
if log.owning_organisation_id.present? 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 end
recently_absorbed_organisations = user.organisation.absorbed_organisations.merged_during_open_collection_period
if !user.support? && user.organisation.holds_own_stock? 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 end
user_answer_options = if user.support? if user.support?
Organisation.where(holds_own_stock: true) 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 else
user.organisation.stock_owners + user.organisation.absorbed_organisations.where(holds_own_stock: true) answer_opts[org.id] = org.name
end.pluck(:id, :name).to_h 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 end
def displayed_answer_options(log, user = nil) def displayed_answer_options(log, user = nil)
@ -71,4 +89,8 @@ private
def selected_answer_option_is_derived?(_log) def selected_answer_option_is_derived?(_log)
true true
end end
def merged_organisation_label(name, merge_date)
"#{name} (inactive as of #{merge_date.to_fs(:govuk_date)})"
end
end end

4
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 recently_absorbed_organisations = user.organisation.absorbed_organisations.merged_during_open_collection_period
if !user.support? && user.organisation.holds_own_stock? if !user.support? && user.organisation.holds_own_stock?
answer_opts[user.organisation.id] = if recently_absorbed_organisations.exists? 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.created_at.to_fs(:govuk_date)})" "#{user.organisation.name} (Your organisation, active as of #{user.organisation.available_from.to_fs(:govuk_date)})"
else else
"#{user.organisation.name} (Your organisation)" "#{user.organisation.name} (Your organisation)"
end end

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

@ -163,7 +163,7 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
end end
context "when organisation has merged" do 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!(:merged_org) { create(:organisation, name: "Merged org", holds_own_stock: false) }
let(:user) { create(:user, :data_coordinator, organisation: absorbing_org) } let(:user) { create(:user, :data_coordinator, organisation: absorbing_org) }
@ -173,6 +173,17 @@ RSpec.describe Form::Lettings::Questions::ManagingOrganisation, type: :model do
end end
it "displays merged organisation on the list of choices" do 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 = { options = {
"" => "Select an option", "" => "Select an option",
absorbing_org.id => "Absorbing org (Your organisation, active as of 3 August 2023)", 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 = { options = {
"" => "Select an option", "" => "Select an option",
merged_org.id => "Merged org (inactive as of 2 August 2023)", 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", managing_agent.id => "Managing org 1",
} }

87
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) expect(question.displayed_answer_options(log, user)).to eq(options)
end end
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 end
context "when user is support" do context "when user is support" do

29
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 let(:options) do
{ {
"" => "Select an option", "" => "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", owning_org_1.id => "Owning org 1",
merged_organisation.id => "Merged org (inactive as of 2 February 2023)", 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 before do
merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation: user.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 end
it "shows merged organisation as an option" do it "shows merged organisation as an option" do
@ -114,7 +113,7 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do
end end
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(:merged_organisation) { create(:organisation, name: "Merged org") }
let(:options) do let(:options) do
{ {
@ -125,10 +124,30 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do
} }
end 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 before do
org_rel.update!(child_organisation: merged_organisation) org_rel.update!(child_organisation: merged_organisation)
merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation: user.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 end
it "does not show merged organisations stock owners as options" do 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)) Timecop.freeze(Time.zone.local(2023, 4, 2))
org_rel.update!(child_organisation: merged_organisation) org_rel.update!(child_organisation: merged_organisation)
merged_organisation.update!(merge_date: Time.zone.local(2021, 6, 2), absorbing_organisation: user.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 end
it "shows merged organisation as an option" do it "shows merged organisation as an option" do

Loading…
Cancel
Save