diff --git a/app/models/form/setup/questions/scheme_id.rb b/app/models/form/setup/questions/scheme_id.rb index fe811f28a..cf6173591 100644 --- a/app/models/form/setup/questions/scheme_id.rb +++ b/app/models/form/setup/questions/scheme_id.rb @@ -20,11 +20,11 @@ class Form::Setup::Questions::SchemeId < ::Form::Question end def displayed_answer_options(case_log) - return {} unless case_log.created_by - - user_org_scheme_ids = Scheme.select(:id).where(owning_organisation_id: case_log.created_by.organisation_id).joins(:locations).merge(Location.where("startdate <= ? or startdate IS NULL", Time.zone.today)).map(&:id) + organisation = case_log.owning_organisation || case_log.created_by&.organisation + schemes = organisation ? Scheme.select(:id).where(owning_organisation_id: organisation.id) : Scheme.select(:id) + filtered_scheme_ids = schemes.joins(:locations).merge(Location.where("startdate <= ? or startdate IS NULL", Time.zone.today)).map(&:id) answer_options.select do |k, _v| - user_org_scheme_ids.include?(k.to_i) || k.blank? + filtered_scheme_ids.include?(k.to_i) || k.blank? end end diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index 0ad7494e0..d6b4a5a30 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -93,6 +93,33 @@ RSpec.describe FormController, type: :request do expect(response.body).to match("What counts as income?") end end + + context "when viewing the setup section schemes page" do + context "when the user is support" do + let(:user) { FactoryBot.create(:user, :support) } + + context "when organisation and user have not been selected yet" do + let(:case_log) do + FactoryBot.create( + :case_log, + owning_organisation: nil, + managing_organisation: nil, + created_by: nil, + needstype: 2, + ) + end + + before do + FactoryBot.create_list(:location, 5) + end + + it "returns an unfiltered list of schemes" do + get "/logs/#{case_log.id}/scheme", headers: headers, params: {} + expect(response.body.scan("