diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb
index 22e6cb599..d0aff494a 100644
--- a/app/controllers/case_logs_controller.rb
+++ b/app/controllers/case_logs_controller.rb
@@ -112,9 +112,10 @@ private
end
def org_params
+ organisation_id = current_user.support? && params["organisation_id"].present? ? params["organisation_id"] : current_user.organisation.id
{
- "owning_organisation_id" => current_user.organisation.id,
- "managing_organisation_id" => current_user.organisation.id,
+ "owning_organisation_id" => organisation_id,
+ "managing_organisation_id" => organisation_id,
"created_by_id" => current_user.id,
}
end
diff --git a/app/models/form/question.rb b/app/models/form/question.rb
index fd92d21f7..bbc36c458 100644
--- a/app/models/form/question.rb
+++ b/app/models/form/question.rb
@@ -19,7 +19,7 @@ class Form::Question
@fields_to_add = hsh["fields-to-add"]
@result_field = hsh["result-field"]
@readonly = hsh["readonly"]
- @answer_options = hsh["answer_options"]
+ @answer_options = hsh["answer_options"].is_a?(String) ? public_send(hsh["answer_options"]) : hsh["answer_options"]
@conditional_for = hsh["conditional_for"]
@inferred_answers = hsh["inferred_answers"]
@inferred_check_answers_value = hsh["inferred_check_answers_value"]
@@ -162,6 +162,10 @@ class Form::Question
type == "radio" && RADIO_REFUSED_VALUE[id.to_sym]&.include?(value)
end
+ def get_all_orgs_answer_options
+ Organisation.all.map { |org| { "#{org.id}": org.name.to_s } }.reduce({}, :merge)
+ end
+
private
def selected_answer_option_is_derived?(case_log)
diff --git a/app/views/organisations/logs.html.erb b/app/views/organisations/logs.html.erb
index a5e62dbf8..6c0339866 100644
--- a/app/views/organisations/logs.html.erb
+++ b/app/views/organisations/logs.html.erb
@@ -16,6 +16,9 @@
) %>
+
+ <%= govuk_button_to "Create a new lettings log", "#{case_logs_path}", params: {:organisation_id => @organisation.id } %>
+
<%= render partial: "case_logs/log_filters" %>
<%= render SearchComponent.new(current_user:, search_label: "Search by log ID, tenant code, property reference or postcode", value: @searched) %>
diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json
index 3b83b3499..68e5c54f9 100644
--- a/spec/fixtures/forms/2021_2022.json
+++ b/spec/fixtures/forms/2021_2022.json
@@ -351,6 +351,17 @@
"illness": 1
}
]
+ },
+ "accessible_select_three": {
+ "questions": {
+ "organisation_id": {
+ "header": "Select options",
+ "hint_text": "What is the managing organisation",
+ "type": "select",
+ "check_answer_label": "Accessible Select",
+ "answer_options": "get_all_orgs_answer_options"
+ }
+ }
}
}
}
diff --git a/spec/models/form/question_spec.rb b/spec/models/form/question_spec.rb
index 4057e4687..0d82bc9c3 100644
--- a/spec/models/form/question_spec.rb
+++ b/spec/models/form/question_spec.rb
@@ -166,6 +166,18 @@ RSpec.describe Form::Question, type: :model do
expect(question.label_from_value(9999)).to eq("9999")
end
end
+
+ context "when the answer options are dynamic" do
+ let(:page_id) { "accessible_select_three" }
+ let(:question_id) { "organisation_id" }
+ let(:organisation) { FactoryBot.create(:organisation) }
+
+ it "gets the answer options from given method" do
+ answer_options = { "#{organisation.id}": organisation.name.to_s }
+ allow(Organisation).to receive(:all).and_return([organisation])
+ expect(question.answer_options).to eq(answer_options)
+ end
+ end
end
context "when type is checkbox" do
diff --git a/spec/requests/case_logs_controller_spec.rb b/spec/requests/case_logs_controller_spec.rb
index ef78e0b5f..6aa4d3ee5 100644
--- a/spec/requests/case_logs_controller_spec.rb
+++ b/spec/requests/case_logs_controller_spec.rb
@@ -121,20 +121,69 @@ RSpec.describe CaseLogsController, type: :request do
end
context "when UI" do
+ let(:organisation) { FactoryBot.create(:organisation) }
let(:user) { FactoryBot.create(:user) }
+ let(:support_user) { FactoryBot.create(:user, :support) }
let(:headers) { { "Accept" => "text/html" } }
+ let(:params) { { "organisation_id" => organisation.id } }
- before do
- RequestHelper.stub_http_requests
- sign_in user
- post "/logs", headers:
+ context("and created by a user from the same organisation") do
+ before do
+ RequestHelper.stub_http_requests
+ sign_in user
+ post "/logs", headers:
+ end
+
+ it "tracks who created the record" do
+ created_id = response.location.match(/[0-9]+/)[0]
+ case_log = CaseLog.find_by(id: created_id)
+ whodunnit_actor = case_log.versions.last.actor
+ expect(whodunnit_actor).to be_a(User)
+ expect(whodunnit_actor.id).to eq(user.id)
+ expect(case_log.created_by_id).to eq(user.id)
+ end
end
- it "tracks who created the record" do
- created_id = response.location.match(/[0-9]+/)[0]
- whodunnit_actor = CaseLog.find_by(id: created_id).versions.last.actor
- expect(whodunnit_actor).to be_a(User)
- expect(whodunnit_actor.id).to eq(user.id)
+ context("and created by a support user") do
+ let(:created_id) do
+ response.location.match(/[0-9]+/)[0]
+ end
+
+ before do
+ RequestHelper.stub_http_requests
+ allow(support_user).to receive(:need_two_factor_authentication?).and_return(false)
+ sign_in support_user
+ end
+
+ context "when organisaition params are provided" do
+ before do
+ post "/logs", headers:, params: params
+ end
+
+ it "tracks who created the record" do
+ whodunnit_actor = CaseLog.find_by(id: created_id).versions.last.actor
+ expect(whodunnit_actor).to be_a(User)
+ expect(whodunnit_actor.id).to eq(support_user.id)
+ end
+
+ it "creates the record for the correct organisation" do
+ case_log = CaseLog.find_by(id: created_id)
+ expect(case_log.owning_organisation_id).to eq(organisation.id)
+ expect(case_log.managing_organisation_id).to eq(organisation.id)
+ end
+ end
+
+ context "with no organisation params" do
+ before do
+ post "/logs", headers:
+ end
+
+ it "created the record with the support user's organisation" do
+ case_log = CaseLog.find_by(id: created_id)
+ expect(case_log.owning_organisation_id).to eq(support_user.organisation.id)
+ expect(case_log.managing_organisation_id).to eq(support_user.organisation.id)
+ end
+ end
end
end
end