Browse Source

get answer options from methods and allow creating a new case log as a support user

CLDC-1263-create-org-logs
Kat 3 years ago
parent
commit
29808894cb
  1. 5
      app/controllers/case_logs_controller.rb
  2. 6
      app/models/form/question.rb
  3. 3
      app/views/organisations/logs.html.erb
  4. 11
      spec/fixtures/forms/2021_2022.json
  5. 12
      spec/models/form/question_spec.rb
  6. 67
      spec/requests/case_logs_controller_spec.rb

5
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

6
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)

3
app/views/organisations/logs.html.erb

@ -16,6 +16,9 @@
) %>
<div class="app-filter-layout" data-controller="filter-layout">
<div class="govuk-button-group app-filter-toggle">
<%= govuk_button_to "Create a new lettings log", "#{case_logs_path}", params: {:organisation_id => @organisation.id } %>
</div>
<%= render partial: "case_logs/log_filters" %>
<div class="app-filter-layout__content">
<%= render SearchComponent.new(current_user:, search_label: "Search by log ID, tenant code, property reference or postcode", value: @searched) %>

11
spec/fixtures/forms/2021_2022.json vendored

@ -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"
}
}
}
}
}

12
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

67
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

Loading…
Cancel
Save