diff --git a/app/models/form/setup/questions/created_by_id.rb b/app/models/form/setup/questions/created_by_id.rb index a709c43b0..72e251f26 100644 --- a/app/models/form/setup/questions/created_by_id.rb +++ b/app/models/form/setup/questions/created_by_id.rb @@ -18,6 +18,13 @@ class Form::Setup::Questions::CreatedById < ::Form::Question end end + def displayed_answer_options(case_log) + return answer_options unless case_log.owning_organisation + + user_ids = case_log.owning_organisation.users.pluck(:id) + [""] + answer_options.select { |k, _v| user_ids.include?(k) } + end + def label_from_value(value) return unless value diff --git a/app/models/form/setup/questions/owning_organisation_id.rb b/app/models/form/setup/questions/owning_organisation_id.rb index dc059a5fd..f48ee9fdd 100644 --- a/app/models/form/setup/questions/owning_organisation_id.rb +++ b/app/models/form/setup/questions/owning_organisation_id.rb @@ -18,6 +18,13 @@ class Form::Setup::Questions::OwningOrganisationId < ::Form::Question end end + def displayed_answer_options(case_log) + return answer_options unless case_log.created_by + + ids = ["", case_log.created_by.organisation.id] + answer_options.select { |k, _v| ids.include?(k) } + end + def label_from_value(value) return unless value diff --git a/spec/models/form/setup/questions/created_by_id_spec.rb b/spec/models/form/setup/questions/created_by_id_spec.rb index 6ae83d332..8ec615ebd 100644 --- a/spec/models/form/setup/questions/created_by_id_spec.rb +++ b/spec/models/form/setup/questions/created_by_id_spec.rb @@ -73,4 +73,18 @@ RSpec.describe Form::Setup::Questions::CreatedById, type: :model do expect(question.hidden_in_check_answers).to be true end end + + context "when the owning organisation is already set" do + let(:case_log) { FactoryBot.create(:case_log, owning_organisation: user_2.organisation) } + let(:expected_answer_options) do + { + "" => "Select an option", + user_2.id => user_2.name, + } + end + + it "only displays users that belong to that organisation" do + expect(question.displayed_answer_options(case_log)).to eq(expected_answer_options) + end + end end diff --git a/spec/models/form/setup/questions/owning_organisation_id_spec.rb b/spec/models/form/setup/questions/owning_organisation_id_spec.rb index 64eaf8326..81cf21478 100644 --- a/spec/models/form/setup/questions/owning_organisation_id_spec.rb +++ b/spec/models/form/setup/questions/owning_organisation_id_spec.rb @@ -73,4 +73,19 @@ RSpec.describe Form::Setup::Questions::OwningOrganisationId, type: :model do expect(question.hidden_in_check_answers).to be true end end + + context "when the user is already set" do + let(:user) { FactoryBot.create(:user, organisation: organisation_2) } + let(:case_log) { FactoryBot.create(:case_log, created_by: user) } + let(:expected_answer_options) do + { + "" => "Select an option", + organisation_2.id => organisation_2.name, + } + end + + it "only displays users that belong to that organisation" do + expect(question.displayed_answer_options(case_log)).to eq(expected_answer_options) + end + end end