From e6cc95a269252bc30e9da3d387043903cf2de938 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 14 Jul 2022 11:07:53 +0100 Subject: [PATCH] Display the schemes with active locations only when selecting a scheme for a case log --- app/models/form/setup/questions/scheme_id.rb | 2 +- spec/features/schemes_spec.rb | 47 +++++++++++++++++++ .../form/setup/questions/scheme_id_spec.rb | 18 +++++-- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/app/models/form/setup/questions/scheme_id.rb b/app/models/form/setup/questions/scheme_id.rb index e69cd05a3..e446ebb57 100644 --- a/app/models/form/setup/questions/scheme_id.rb +++ b/app/models/form/setup/questions/scheme_id.rb @@ -22,7 +22,7 @@ class Form::Setup::Questions::SchemeId < ::Form::Question 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).map(&:id) + 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.now)).map(&:id) answer_options.select do |k, _v| user_org_scheme_ids.include?(k.to_i) end diff --git a/spec/features/schemes_spec.rb b/spec/features/schemes_spec.rb index cb5dd6770..9230ab143 100644 --- a/spec/features/schemes_spec.rb +++ b/spec/features/schemes_spec.rb @@ -772,4 +772,51 @@ RSpec.describe "Schemes scheme Features" do end end end + + context "when selecting a scheme" do + let!(:user) { FactoryBot.create(:user, :data_coordinator, last_sign_in_at: Time.zone.now) } + let!(:schemes) { FactoryBot.create_list(:scheme, 5, owning_organisation: user.organisation) } + let(:location) { FactoryBot.create(:location, scheme: schemes[2]) } + let!(:case_log) { FactoryBot.create(:case_log, created_by: user, needstype: 2) } + + before do + Timecop.freeze(Time.utc(2022, 6, 3)) + location.update!(startdate: nil) + FactoryBot.create(:location, scheme: schemes[0], startdate: nil) + FactoryBot.create(:location, scheme: schemes[1], startdate: nil) + FactoryBot.create(:location, scheme: schemes[1], startdate: nil) + FactoryBot.create(:location, scheme: schemes[1], startdate: Time.utc(2023, 6, 3)) + visit("/logs") + fill_in("user[email]", with: user.email) + fill_in("user[password]", with: user.password) + click_button("Sign in") + end + + after do + Timecop.unfreeze + end + + it "does not display the schemes without a location" do + visit("/logs/#{case_log.id}/scheme") + expect(find("#case-log-scheme-id-field").all("option").count).to eq(3) + end + + it "does not display the schemes with a location with a startdate in the future" do + location.update!(startdate: Time.utc(2022, 7, 4)) + visit("/logs/#{case_log.id}/scheme") + expect(find("#case-log-scheme-id-field").all("option").count).to eq(2) + end + + it "does display the schemes with a location with a startdate in the past" do + location.update!(startdate: Time.utc(2022, 5, 2)) + visit("/logs/#{case_log.id}/scheme") + expect(find("#case-log-scheme-id-field").all("option").count).to eq(3) + end + + it "does display the schemes with a location with a startdate being today" do + location.update!(startdate: Time.utc(2022, 6, 3)) + visit("/logs/#{case_log.id}/scheme") + expect(find("#case-log-scheme-id-field").all("option").count).to eq(3) + end + end end diff --git a/spec/models/form/setup/questions/scheme_id_spec.rb b/spec/models/form/setup/questions/scheme_id_spec.rb index 9dde00b84..2b016f463 100644 --- a/spec/models/form/setup/questions/scheme_id_spec.rb +++ b/spec/models/form/setup/questions/scheme_id_spec.rb @@ -50,9 +50,21 @@ RSpec.describe Form::Setup::Questions::SchemeId, type: :model do FactoryBot.create(:scheme, owning_organisation: organisation_2) end - it "has the correct answer_options based on the schemes the user's organisation owns or manages" do - expected_answer = { scheme.id.to_s => scheme } - expect(question.displayed_answer_options(case_log)).to eq(expected_answer) + context "when a scheme with at least 1 location exists" do + before do + FactoryBot.create(:location, scheme:) + end + + it "has the correct answer_options based on the schemes the user's organisation owns or manages" do + expected_answer = { scheme.id.to_s => scheme } + expect(question.displayed_answer_options(case_log)).to eq(expected_answer) + end + end + + context "when there are no schemes with locations" do + it "returns an empty hash" do + expect(question.displayed_answer_options(case_log)).to eq({}) + end end end end