Browse Source

CLDC-2504 Add org select to sales if merged (#1830)

* feat: wip update sales org select

* feat: update routing and hidden in check answers methods

* feat: set sales org id as derived

* feat: update tests

* refactor: linting

* feat: update tests and radio options

* feat: update test

* feat: add value helper test for non-constant value question

* refactor: lint

* feat: freeze time in unrelated test

* refactor: linting

* feat: wip commit

* feat: tidy up active readouts

* feat: revert previous commits

* feat: put merge orgs behaviour behind feature flag

* feat: put merge orgs behaviour behind feature flag

* refactor: lint

* refactor: simplify

* feat: add one more feature toggle
pull/1819/head
natdeanlewissoftwire 1 year ago committed by GitHub
parent
commit
ab55b67e3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      app/models/form/lettings/pages/stock_owner.rb
  2. 6
      app/models/form/lettings/questions/stock_owner.rb
  3. 23
      app/models/form/sales/pages/organisation.rb
  4. 80
      app/models/form/sales/questions/owning_organisation_id.rb

8
app/models/form/lettings/pages/stock_owner.rb

@ -14,10 +14,14 @@ class Form::Lettings::Pages::StockOwner < ::Form::Page
return false unless current_user return false unless current_user
return true if current_user.support? return true if current_user.support?
stock_owners = current_user.organisation.stock_owners + current_user.organisation.absorbed_organisations.where(holds_own_stock: true) stock_owners = if FeatureToggle.merge_organisations_enabled?
current_user.organisation.stock_owners + current_user.organisation.absorbed_organisations.where(holds_own_stock: true)
else
current_user.organisation.stock_owners
end
if current_user.organisation.holds_own_stock? if current_user.organisation.holds_own_stock?
if current_user.organisation.absorbed_organisations.any?(&:holds_own_stock?) if FeatureToggle.merge_organisations_enabled? && current_user.organisation.absorbed_organisations.any?(&:holds_own_stock?)
return true return true
end end
return true if stock_owners.count >= 1 return true if stock_owners.count >= 1

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

@ -49,7 +49,11 @@ class Form::Lettings::Questions::StockOwner < ::Form::Question
def hidden_in_check_answers?(_log, user = nil) def hidden_in_check_answers?(_log, user = nil)
return false if user.support? return false if user.support?
stock_owners = user.organisation.stock_owners + user.organisation.absorbed_organisations.where(holds_own_stock: true) stock_owners = if FeatureToggle.merge_organisations_enabled?
user.organisation.stock_owners + user.organisation.absorbed_organisations.where(holds_own_stock: true)
else
user.organisation.stock_owners
end
if user.organisation.holds_own_stock? if user.organisation.holds_own_stock?
stock_owners.count.zero? stock_owners.count.zero?

23
app/models/form/sales/pages/organisation.rb

@ -11,18 +11,23 @@ class Form::Sales::Pages::Organisation < ::Form::Page
end end
def routed_to?(_log, current_user) def routed_to?(_log, current_user)
return false unless current_user if FeatureToggle.merge_organisations_enabled?
return true if current_user.support?
absorbed_stock_owners = current_user.organisation.absorbed_organisations.where(holds_own_stock: true) return false unless current_user
return true if current_user.support?
if current_user.organisation.holds_own_stock? absorbed_stock_owners = current_user.organisation.absorbed_organisations.where(holds_own_stock: true)
return true if absorbed_stock_owners.count >= 1
if current_user.organisation.holds_own_stock?
return true if absorbed_stock_owners.count >= 1
else
return false if absorbed_stock_owners.count.zero?
return true if absorbed_stock_owners.count > 1
end
false
else else
return false if absorbed_stock_owners.count.zero? !!current_user&.support?
return true if absorbed_stock_owners.count > 1
end end
false
end end
end end

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

@ -11,39 +11,47 @@ class Form::Sales::Questions::OwningOrganisationId < ::Form::Question
answer_opts = { "" => "Select an option" } answer_opts = { "" => "Select an option" }
return answer_opts unless ActiveRecord::Base.connected? return answer_opts unless ActiveRecord::Base.connected?
return answer_opts unless user
return answer_opts unless log
if log.owning_organisation_id.present? if FeatureToggle.merge_organisations_enabled?
answer_opts[log.owning_organisation.id] = log.owning_organisation.name return answer_opts unless user
end return answer_opts unless log
recently_absorbed_organisations = user.organisation.absorbed_organisations.merged_during_open_collection_period if log.owning_organisation_id.present?
if !user.support? && user.organisation.holds_own_stock? answer_opts[log.owning_organisation.id] = log.owning_organisation.name
answer_opts[user.organisation.id] = if recently_absorbed_organisations.exists? end
"#{user.organisation.name} (Your organisation, active as of #{user.organisation.created_at.to_fs(:govuk_date)})"
else
"#{user.organisation.name} (Your organisation)"
end
end
if user.support? recently_absorbed_organisations = user.organisation.absorbed_organisations.merged_during_open_collection_period
Organisation.where(holds_own_stock: true).find_each do |org| if !user.support? && user.organisation.holds_own_stock?
if org.merge_date.present? answer_opts[user.organisation.id] = if recently_absorbed_organisations.exists?
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 "#{user.organisation.name} (Your organisation, active as of #{user.organisation.created_at.to_fs(:govuk_date)})"
elsif org.absorbed_organisations.merged_during_open_collection_period.exists? else
answer_opts[org.id] = "#{org.name} (active as of #{org.created_at.to_fs(:govuk_date)})" "#{user.organisation.name} (Your organisation)"
else end
answer_opts[org.id] = org.name end
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
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
end end
answer_opts
else else
recently_absorbed_organisations.each do |absorbed_org| Organisation.select(:id, :name).each_with_object(answer_opts) do |organisation, hsh|
answer_opts[absorbed_org.id] = merged_organisation_label(absorbed_org.name, absorbed_org.merge_date) if absorbed_org.holds_own_stock? hsh[organisation.id] = organisation.name
hsh
end end
end end
answer_opts
end end
def displayed_answer_options(log, user = nil) def displayed_answer_options(log, user = nil)
@ -57,18 +65,22 @@ class Form::Sales::Questions::OwningOrganisationId < ::Form::Question
end end
def derived? def derived?
true true if FeatureToggle.merge_organisations_enabled?
end end
def hidden_in_check_answers?(_log, user = nil) def hidden_in_check_answers?(_log, user = nil)
return false if user.support? if FeatureToggle.merge_organisations_enabled?
return false if user.support?
stock_owners = user.organisation.stock_owners + user.organisation.absorbed_organisations.where(holds_own_stock: true) stock_owners = user.organisation.stock_owners + user.organisation.absorbed_organisations.where(holds_own_stock: true)
if user.organisation.holds_own_stock? if user.organisation.holds_own_stock?
stock_owners.count.zero? stock_owners.count.zero?
else
stock_owners.count <= 1
end
else else
stock_owners.count <= 1 !current_user.support?
end end
end end
@ -79,7 +91,11 @@ class Form::Sales::Questions::OwningOrganisationId < ::Form::Question
private private
def selected_answer_option_is_derived?(_log) def selected_answer_option_is_derived?(_log)
true if FeatureToggle.merge_organisations_enabled?
true
else
false
end
end end
def merged_organisation_label(name, merge_date) def merged_organisation_label(name, merge_date)

Loading…
Cancel
Save