Browse Source

Only display orgs merged during open collection period

pull/1821/head
Kat 3 years ago
parent
commit
ef8a33bfa3
  1. 19
      app/models/form/sales/questions/owning_organisation_id.rb
  2. 1
      app/models/organisation.rb
  3. 22
      spec/models/form/sales/questions/owning_organisation_id_spec.rb

19
app/models/form/sales/questions/owning_organisation_id.rb

@ -16,16 +16,21 @@ class Form::Sales::Questions::OwningOrganisationId < ::Form::Question
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] = organisation_label(user.organisation)
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)})"
else
"#{user.organisation.name} (Your organisation)"
end
end
user_organisation_options = user.support? ? Organisation.where(holds_own_stock: true) : user.organisation.stock_owners
user_answer_options = user_organisation_options.pluck(:id, :name).to_h
unless user.support?
user.organisation.absorbed_organisations.where(holds_own_stock: true).find_each do |absorbed_org|
answer_opts[absorbed_org.id] = merged_organisation_label(absorbed_org.name, absorbed_org.merge_date)
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?
absorbed_org.stock_owners.each do |stock_owner|
user_answer_options[stock_owner.id] = merged_organisation_label(stock_owner.name, absorbed_org.merge_date)
end
@ -71,14 +76,6 @@ private
true
end
def organisation_label(organisation)
if organisation.absorbed_organisations.exists?
"#{organisation.name} (Your organisation, active as of #{organisation.created_at.to_fs(:govuk_date)})"
else
"#{organisation.name} (Your organisation)"
end
end
def merged_organisation_label(name, merge_date)
"#{name} (inactive as of #{merge_date.to_fs(:govuk_date)})"
end

1
app/models/organisation.rb

@ -32,6 +32,7 @@ class Organisation < ApplicationRecord
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }
scope :search_by, ->(param) { search_by_name(param) }
scope :merged_during_open_collection_period, -> { where("merge_date >= ?", FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period) }
has_paper_trail

22
spec/models/form/sales/questions/owning_organisation_id_spec.rb

@ -152,6 +152,28 @@ RSpec.describe Form::Sales::Questions::OwningOrganisationId, type: :model do
user.organisation.update!(created_at: Time.zone.local(2021, 2, 2))
end
it "shows 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!(created_at: 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

Loading…
Cancel
Save