diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 44341b48f..92669bcbe 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -273,7 +273,7 @@ private required_params[:sensitive] = required_params[:sensitive].to_i if required_params[:sensitive] - if current_user.data_coordinator? + if current_user.data_coordinator? && current_user.organisation.stock_owners.count.zero? required_params[:owning_organisation_id] = current_user.organisation_id end required_params @@ -291,10 +291,6 @@ private @scheme end - def user_allowed_action? - current_user.support? || current_user.organisation == @scheme&.owning_organisation || current_user.organisation.parent_organisations.exists?(@scheme&.owning_organisation_id) - end - def redirect_if_scheme_confirmed redirect_to @scheme if @scheme.confirmed? end diff --git a/app/helpers/check_answers_helper.rb b/app/helpers/check_answers_helper.rb index ed07f3b56..80f4326f7 100644 --- a/app/helpers/check_answers_helper.rb +++ b/app/helpers/check_answers_helper.rb @@ -14,7 +14,7 @@ module CheckAnswersHelper def can_change_scheme_answer?(attribute_name, scheme) return false unless current_user.support? || current_user.data_coordinator? - editable_attributes = current_user.support? ? ["Name", "Confidential information", "Housing stock owned by"] : ["Name", "Confidential information"] + editable_attributes = ["Name", "Confidential information", "Housing stock owned by"] !scheme.confirmed? || editable_attributes.include?(attribute_name) end diff --git a/app/helpers/schemes_helper.rb b/app/helpers/schemes_helper.rb index f7da6d3f7..d111b402c 100644 --- a/app/helpers/schemes_helper.rb +++ b/app/helpers/schemes_helper.rb @@ -1,6 +1,6 @@ module SchemesHelper - def display_scheme_attributes(scheme, user) - base_attributes = [ + def display_scheme_attributes(scheme) + [ { name: "Scheme code", value: scheme.id_to_display }, { name: "Name", value: scheme.service_name, edit: true }, { name: "Status", value: status_tag_from_resource(scheme) }, @@ -16,16 +16,6 @@ module SchemesHelper { name: "Intended length of stay", value: scheme.intended_stay }, { name: "Availability", value: scheme_availability(scheme) }, ] - - if user.data_coordinator? - base_attributes.delete_if { |item| item[:name] == "Housing stock owned by" } - end - - if scheme.has_other_client_group == "Yes" - base_attributes.append - end - - base_attributes end def scheme_availability(scheme) @@ -44,6 +34,17 @@ module SchemesHelper return govuk_button_link_to "Reactivate this scheme", scheme_new_reactivation_path(scheme) if scheme.deactivated? end + def owning_organisation_options(current_user) + all_orgs = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } + user_org = [OpenStruct.new(id: current_user.organisation_id, name: current_user.organisation.name)] + stock_owners = current_user.organisation.stock_owners.map { |org| OpenStruct.new(id: org.id, name: org.name) } + current_user.support? ? all_orgs : user_org + stock_owners + end + + def null_option + [OpenStruct.new(id: "", name: "Select an option")] + end + private ActivePeriod = Struct.new(:from, :to) diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 55d15dfb2..19a6491fd 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -252,7 +252,7 @@ class Scheme < ApplicationRecord end def validate_owning_organisation - unless owning_organisation.holds_own_stock? + unless owning_organisation&.holds_own_stock? errors.add(:owning_organisation_id, :does_not_own_stock, message: I18n.t("validations.scheme.owning_organisation.does_not_own_stock")) end end diff --git a/app/policies/location_policy.rb b/app/policies/location_policy.rb index f10f96ef5..5d6d6d467 100644 --- a/app/policies/location_policy.rb +++ b/app/policies/location_policy.rb @@ -16,14 +16,14 @@ class LocationPolicy if location == Location user.data_coordinator? else - user.data_coordinator? && user.organisation == scheme&.owning_organisation + user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner end end def update? return true if user.support? - user.data_coordinator? && scheme&.owning_organisation == user.organisation + user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner end %w[ @@ -51,7 +51,7 @@ class LocationPolicy define_method method_name do return true if user.support? - user.data_coordinator? && scheme&.owning_organisation == user.organisation + user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner end end @@ -62,7 +62,7 @@ class LocationPolicy define_method method_name do return true if user.support? - user.organisation.parent_organisations.exists?(scheme&.owning_organisation_id) || scheme&.owning_organisation == user.organisation + scheme_owned_by_user_org_or_stock_owner end end @@ -71,4 +71,8 @@ private def scheme location.scheme end + + def scheme_owned_by_user_org_or_stock_owner + scheme&.owning_organisation == user.organisation || user.organisation.stock_owners.exists?(scheme&.owning_organisation_id) + end end diff --git a/app/policies/scheme_policy.rb b/app/policies/scheme_policy.rb index 39842a160..30aa1543c 100644 --- a/app/policies/scheme_policy.rb +++ b/app/policies/scheme_policy.rb @@ -12,7 +12,7 @@ class SchemePolicy if scheme == Scheme true else - user.organisation.parent_organisations.exists?(scheme&.owning_organisation_id) || scheme&.owning_organisation == user.organisation + scheme_owned_by_user_org_or_stock_owner end end @@ -27,7 +27,7 @@ class SchemePolicy def update? return true if user.support? - user.data_coordinator? && (scheme&.owning_organisation == user.organisation) + user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner end %w[ @@ -37,7 +37,7 @@ class SchemePolicy define_method method_name do return true if user.support? - user.organisation.parent_organisations.exists?(scheme&.owning_organisation_id) || scheme&.owning_organisation == user.organisation + scheme_owned_by_user_org_or_stock_owner end end @@ -57,7 +57,13 @@ class SchemePolicy define_method method_name do return true if user.support? - user.data_coordinator? && scheme&.owning_organisation == user.organisation + user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner end end + +private + + def scheme_owned_by_user_org_or_stock_owner + scheme&.owning_organisation == user.organisation || user.organisation.stock_owners.exists?(scheme&.owning_organisation_id) + end end diff --git a/app/views/schemes/check_answers.html.erb b/app/views/schemes/check_answers.html.erb index 7924768f0..dccefc565 100644 --- a/app/views/schemes/check_answers.html.erb +++ b/app/views/schemes/check_answers.html.erb @@ -9,7 +9,6 @@

Scheme

<% @scheme.check_details_attributes.each do |attr| %> - <% next if current_user.data_coordinator? && attr[:name] == ("owned by") %> <%= render partial: "scheme_summary_list_row", locals: { scheme: @scheme, attribute: attr, change_link: @scheme.confirmed? ? scheme_edit_name_path(@scheme) : scheme_details_path(@scheme, check_answers: true) } %> <% end %> diff --git a/app/views/schemes/details.html.erb b/app/views/schemes/details.html.erb index 0e4f3a7d9..3dc6208b1 100644 --- a/app/views/schemes/details.html.erb +++ b/app/views/schemes/details.html.erb @@ -25,10 +25,6 @@ label: { text: "This scheme contains confidential information" } %> <% end %> - <% if current_user.data_coordinator? %> - <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %> - <% end %> - <% scheme_types_selection = Scheme.scheme_types.keys.excluding("Missing").map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> <%= f.govuk_collection_radio_buttons :scheme_type, @@ -48,11 +44,11 @@ :description, legend: { text: "Is this scheme registered under the Care Standards Act 2000?", size: "m" } %> - <% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %> - - <% if current_user.support? %> + <% if current_user.data_coordinator? && current_user.organisation.stock_owners.count.zero? %> + <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %> + <% else %> <%= f.govuk_collection_select :owning_organisation_id, - organisations, + owning_organisation_options(current_user), :id, :name, label: { text: "Which organisation owns the housing stock for this scheme?", size: "m" }, diff --git a/app/views/schemes/edit_name.html.erb b/app/views/schemes/edit_name.html.erb index 1dc8bc147..0c7c1091e 100644 --- a/app/views/schemes/edit_name.html.erb +++ b/app/views/schemes/edit_name.html.erb @@ -25,11 +25,11 @@ label: { text: "This scheme contains confidential information" } %> <% end %> - <% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %> - - <% if current_user.support? %> + <% if current_user.data_coordinator? && current_user.organisation.stock_owners.count.zero? %> + <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %> + <% else %> <%= f.govuk_collection_select :owning_organisation_id, - organisations, + owning_organisation_options(current_user), :id, :name, label: { text: "Which organisation owns the housing stock for this scheme?", size: "m" }, diff --git a/app/views/schemes/new.html.erb b/app/views/schemes/new.html.erb index 81c5febbf..af5c2089a 100644 --- a/app/views/schemes/new.html.erb +++ b/app/views/schemes/new.html.erb @@ -26,14 +26,6 @@ label: { text: "This scheme contains confidential information" } %> <% end %> - <% null_option = [OpenStruct.new(id: "", name: "Select an option")] %> - <% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %> - <% answer_options = null_option + organisations %> - - <% if current_user.data_coordinator? %> - <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %> - <% end %> - <% scheme_types_selection = Scheme.scheme_types.keys.excluding("Missing").map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> <%= f.govuk_collection_radio_buttons :scheme_type, scheme_types_selection, @@ -52,13 +44,15 @@ :description, legend: { text: "Is this scheme registered under the Care Standards Act 2000?", size: "m" } %> - <% if current_user.support? %> - <%= f.govuk_collection_select :owning_organisation_id, - answer_options, - :id, - :name, - label: { text: "Which organisation owns the housing stock for this scheme?", size: "m" }, - "data-controller": %w[accessible-autocomplete conditional-filter] %> + <% if current_user.data_coordinator? && current_user.organisation.stock_owners.count.zero? %> + <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %> + <% else %> + <%= f.govuk_collection_select :owning_organisation_id, + null_option + owning_organisation_options(current_user), + :id, + :name, + label: { text: "Which organisation owns the housing stock for this scheme?", size: "m" }, + "data-controller": %w[accessible-autocomplete conditional-filter] %> <% end %> <% mantype_selection = Scheme.arrangement_types.keys.excluding("Missing").map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> diff --git a/app/views/schemes/show.html.erb b/app/views/schemes/show.html.erb index 66209fa28..7115dc07e 100644 --- a/app/views/schemes/show.html.erb +++ b/app/views/schemes/show.html.erb @@ -16,7 +16,7 @@

Scheme

<%= govuk_summary_list do |summary_list| %> - <% display_scheme_attributes(@scheme, current_user).each do |attr| %> + <% display_scheme_attributes(@scheme).each do |attr| %> <%= summary_list.row do |row| %> <% row.key { attr[:name] } %> <% row.value do %> diff --git a/spec/features/schemes_spec.rb b/spec/features/schemes_spec.rb index dd683312e..12bd76697 100644 --- a/spec/features/schemes_spec.rb +++ b/spec/features/schemes_spec.rb @@ -1069,7 +1069,7 @@ RSpec.describe "Schemes scheme Features" do it "lets me see amended details on the check answers page" do expect(page).to have_content "FooBar" expect(page).to have_current_path("/schemes/#{scheme.id}/check-answers") - expect(page).to have_link("Change", href: /schemes\/#{scheme.id}\/edit-name/, count: 2) + expect(page).to have_link("Change", href: /schemes\/#{scheme.id}\/edit-name/, count: 3) end end end diff --git a/spec/helpers/schemes_helper_spec.rb b/spec/helpers/schemes_helper_spec.rb index 95d3c78e3..ef4b5fcec 100644 --- a/spec/helpers/schemes_helper_spec.rb +++ b/spec/helpers/schemes_helper_spec.rb @@ -111,7 +111,7 @@ RSpec.describe SchemesHelper do let(:coordinator_user) { FactoryBot.create(:user, :data_coordinator) } context "when scheme has no locations" do - it "returns correct display attributes for a support user" do + it "returns correct display attributes" do attributes = [ { name: "Scheme code", value: "S#{scheme.id}" }, { name: "Name", value: "Test service_name", edit: true }, @@ -128,26 +128,7 @@ RSpec.describe SchemesHelper do { name: "Intended length of stay", value: "Permanent" }, { name: "Availability", value: "Active from 1 April 2021" }, ] - expect(display_scheme_attributes(scheme, support_user)).to eq(attributes) - end - - it "returns correct display attributes for a coordinator user" do - attributes = [ - { name: "Scheme code", value: "S#{scheme.id}" }, - { name: "Name", value: "Test service_name", edit: true }, - { name: "Status", value: status_tag(:incomplete) }, - { name: "Confidential information", value: "No", edit: true }, - { name: "Type of scheme", value: "Housing for older people" }, - { name: "Registered under Care Standards Act 2000", value: "Yes – registered care home providing personal care" }, - { name: "Support services provided by", value: "A registered charity or voluntary organisation" }, - { name: "Primary client group", value: "Rough sleepers" }, - { name: "Has another client group", value: "Yes" }, - { name: "Secondary client group", value: "Refugees (permanent)" }, - { name: "Level of support given", value: "High level" }, - { name: "Intended length of stay", value: "Permanent" }, - { name: "Availability", value: "Active from 1 April 2021" }, - ] - expect(display_scheme_attributes(scheme, coordinator_user)).to eq(attributes) + expect(display_scheme_attributes(scheme)).to eq(attributes) end end @@ -156,7 +137,7 @@ RSpec.describe SchemesHelper do FactoryBot.create(:location, scheme:) end - it "returns correct display attributes for a support user" do + it "returns correct display attributes" do attributes = [ { name: "Scheme code", value: "S#{scheme.id}" }, { name: "Name", value: "Test service_name", edit: true }, @@ -173,31 +154,12 @@ RSpec.describe SchemesHelper do { name: "Intended length of stay", value: "Permanent" }, { name: "Availability", value: "Active from 1 April 2021" }, ] - expect(display_scheme_attributes(scheme, support_user)).to eq(attributes) - end - - it "returns correct display attributes for a coordinator user" do - attributes = [ - { name: "Scheme code", value: "S#{scheme.id}" }, - { name: "Name", value: "Test service_name", edit: true }, - { name: "Status", value: status_tag(:active) }, - { name: "Confidential information", value: "No", edit: true }, - { name: "Type of scheme", value: "Housing for older people" }, - { name: "Registered under Care Standards Act 2000", value: "Yes – registered care home providing personal care" }, - { name: "Support services provided by", value: "A registered charity or voluntary organisation" }, - { name: "Primary client group", value: "Rough sleepers" }, - { name: "Has another client group", value: "Yes" }, - { name: "Secondary client group", value: "Refugees (permanent)" }, - { name: "Level of support given", value: "High level" }, - { name: "Intended length of stay", value: "Permanent" }, - { name: "Availability", value: "Active from 1 April 2021" }, - ] - expect(display_scheme_attributes(scheme, coordinator_user)).to eq(attributes) + expect(display_scheme_attributes(scheme)).to eq(attributes) end context "when the managing organisation is the owning organisation" do it "doesn't show the organisation providing support" do - attributes = display_scheme_attributes(scheme_where_managing_organisation_is_owning_organisation, support_user).find { |x| x[:name] == "Organisation providing support" } + attributes = display_scheme_attributes(scheme_where_managing_organisation_is_owning_organisation).find { |x| x[:name] == "Organisation providing support" } expect(attributes).to be_nil end end @@ -206,7 +168,7 @@ RSpec.describe SchemesHelper do context "with no deactivations" do it "displays current collection start date as availability date if created_at is later than collection start date" do scheme.update!(created_at: Time.zone.local(2022, 4, 16)) - availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value] + availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value] expect(availability_attribute).to eq("Active from 1 April 2021") end @@ -221,7 +183,7 @@ RSpec.describe SchemesHelper do end it "displays the timeline of availability" do - availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value] + availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value] expect(availability_attribute).to eq("Active from 1 April 2021 to 9 August 2022\nDeactivated on 10 August 2022\nActive from 1 September 2022 to 14 September 2022\nDeactivated on 15 September 2022\nActive from 28 September 2022") end @@ -235,7 +197,7 @@ RSpec.describe SchemesHelper do end it "displays the timeline of availability" do - availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value] + availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value] expect(availability_attribute).to eq("Active from 1 April 2021 to 9 August 2022\nDeactivated on 10 August 2022\nActive from 1 September 2022 to 14 September 2022\nDeactivated on 15 September 2022") end @@ -251,7 +213,7 @@ RSpec.describe SchemesHelper do end it "displays the timeline of availability" do - availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value] + availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value] expect(availability_attribute).to eq("Active from 1 April 2021 to 14 June 2022\nDeactivated on 15 June 2022\nActive from 18 June 2022 to 23 September 2022\nDeactivated on 24 September 2022\nActive from 28 September 2022") end @@ -265,7 +227,7 @@ RSpec.describe SchemesHelper do end it "displays the timeline of availability" do - availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value] + availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value] expect(availability_attribute).to eq("Active from 1 April 2021 to 14 June 2022\nDeactivated on 15 June 2022\nActive from 28 September 2022") end @@ -282,7 +244,7 @@ RSpec.describe SchemesHelper do end it "displays the timeline of availability" do - availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value] + availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value] expect(availability_attribute).to eq("Active from 1 April 2021 to 14 June 2022\nDeactivated on 15 June 2022\nActive from 28 September 2022 to 23 October 2022\nDeactivated on 24 October 2022\nActive from 28 October 2022") end @@ -297,7 +259,7 @@ RSpec.describe SchemesHelper do end it "displays the timeline of availability" do - availability_attribute = display_scheme_attributes(scheme, support_user).find { |x| x[:name] == "Availability" }[:value] + availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value] expect(availability_attribute).to eq("Active from 1 April 2021 to 9 October 2022\nDeactivated on 10 October 2022\nActive from 11 December 2022") end diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 7dccbec1b..fd3bd48e5 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -179,7 +179,7 @@ RSpec.describe LocationsController, type: :request do get "/schemes/#{scheme.id}/locations" end - context "when coordinator attempts to see scheme belonging to a different organisation" do + context "when coordinator attempts to see scheme belonging to a different (and not their parent) organisation" do let(:another_scheme) { create(:scheme) } before do @@ -302,8 +302,8 @@ RSpec.describe LocationsController, type: :request do end end - it "does not allow adding new locations" do - expect(page).not_to have_button("Add a location") + it "does allow adding new locations" do + expect(page).to have_button("Add a location") end end end diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 50d83a5b3..9d322ec87 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -457,7 +457,7 @@ RSpec.describe SchemesController, type: :request do expect(page).to have_content(specific_scheme.intended_stay) end - context "when coordinator attempts to see scheme belonging to a different organisation" do + context "when coordinator attempts to see scheme belonging to a different (and not their parent) organisation" do let!(:specific_scheme) { create(:scheme) } it "returns 401" do @@ -474,7 +474,6 @@ RSpec.describe SchemesController, type: :request do end context "when looking at scheme details" do - let(:user) { create(:user, :data_coordinator) } let!(:scheme) { create(:scheme, owning_organisation: user.organisation) } let(:add_deactivations) { scheme.scheme_deactivation_periods << scheme_deactivation_period } @@ -535,21 +534,68 @@ RSpec.describe SchemesController, type: :request do context "when coordinator attempts to see scheme belonging to a parent organisation" do let(:parent_organisation) { create(:organisation) } let!(:specific_scheme) { create(:scheme, owning_organisation: parent_organisation) } + let(:add_deactivations) { specific_scheme.scheme_deactivation_periods << scheme_deactivation_period } before do create(:location, scheme: specific_scheme) create(:organisation_relationship, parent_organisation:, child_organisation: user.organisation) + Timecop.freeze(Time.utc(2022, 10, 10)) + sign_in user + add_deactivations + specific_scheme.save! get "/schemes/#{specific_scheme.id}" end - it "shows the scheme" do - expect(page).to have_content(specific_scheme.id_to_display) + after do + Timecop.unfreeze end - it "does not allow editing the scheme" do - expect(page).not_to have_link("Change") - expect(page).not_to have_content("Reactivate this scheme") - expect(page).not_to have_content("Deactivate this scheme") + context "with active scheme" do + let(:add_deactivations) {} + + it "shows the scheme" do + expect(page).to have_content(specific_scheme.id_to_display) + end + + it "allows editing" do + expect(page).to have_link("Change") + end + + it "renders deactivate this scheme" do + expect(response).to have_http_status(:ok) + expect(page).to have_link("Deactivate this scheme", href: "/schemes/#{specific_scheme.id}/new-deactivation") + end + end + + context "with deactivated scheme" do + let(:scheme_deactivation_period) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9), scheme: specific_scheme) } + + it "renders reactivate this scheme" do + expect(response).to have_http_status(:ok) + expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{specific_scheme.id}/new-reactivation") + end + end + + context "with scheme that's deactivating soon" do + let(:scheme_deactivation_period) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 12), scheme: specific_scheme) } + + it "does not render toggle scheme link" do + expect(response).to have_http_status(:ok) + expect(page).not_to have_link("Reactivate this scheme") + expect(page).not_to have_link("Deactivate this scheme") + end + end + + context "with scheme that's deactivating in more than 6 months" do + let(:scheme_deactivation_period) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 5, 12), scheme: specific_scheme) } + + it "does not render toggle scheme link" do + expect(response).to have_http_status(:ok) + expect(page).not_to have_link("Reactivate this scheme") + expect(page).to have_link("Deactivate this scheme") + expect(response.body).not_to include("Deactivating soon") + expect(response.body).to include("Active") + end end end @@ -678,62 +724,159 @@ RSpec.describe SchemesController, type: :request do context "when signed in as a data coordinator" do let(:user) { create(:user, :data_coordinator) } - let(:params) do - { scheme: { service_name: " testy ", - sensitive: "1", - scheme_type: "Foyer", - registered_under_care_act: "No", - arrangement_type: "D" } } - end before do sign_in user end - it "creates a new scheme for user organisation with valid params and renders correct page" do - expect { post "/schemes", params: }.to change(Scheme, :count).by(1) - follow_redirect! - expect(response).to have_http_status(:ok) - expect(page).to have_content("What client group is this scheme intended for?") - end + context "when making a scheme in the user's organisation" do + let!(:params) do + { scheme: { service_name: " testy ", + sensitive: "1", + scheme_type: "Foyer", + registered_under_care_act: "No", + owning_organisation_id: user.organisation.id, + arrangement_type: "D" } } + end - it "creates a new scheme for user organisation with valid params" do - post "/schemes", params: params + it "creates a new scheme for user organisation with valid params and renders correct page" do + expect { post "/schemes", params: }.to change(Scheme, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content("What client group is this scheme intended for?") + end - expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) - expect(Scheme.last.service_name).to eq("testy") - expect(Scheme.last.scheme_type).to eq("Foyer") - expect(Scheme.last.sensitive).to eq("Yes") - expect(Scheme.last.registered_under_care_act).to eq("No") - expect(Scheme.last.id).not_to eq(nil) - expect(Scheme.last.has_other_client_group).to eq(nil) - expect(Scheme.last.primary_client_group).to eq(nil) - expect(Scheme.last.secondary_client_group).to eq(nil) - expect(Scheme.last.support_type).to eq(nil) - expect(Scheme.last.intended_stay).to eq(nil) - expect(Scheme.last.id_to_display).to match(/S*/) + it "creates a new scheme for user organisation with valid params" do + post "/schemes", params: params + + expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) + expect(Scheme.last.service_name).to eq("testy") + expect(Scheme.last.scheme_type).to eq("Foyer") + expect(Scheme.last.sensitive).to eq("Yes") + expect(Scheme.last.registered_under_care_act).to eq("No") + expect(Scheme.last.id).not_to eq(nil) + expect(Scheme.last.has_other_client_group).to eq(nil) + expect(Scheme.last.primary_client_group).to eq(nil) + expect(Scheme.last.secondary_client_group).to eq(nil) + expect(Scheme.last.support_type).to eq(nil) + expect(Scheme.last.intended_stay).to eq(nil) + expect(Scheme.last.id_to_display).to match(/S*/) + end + + context "when support services provider is selected" do + let(:params) do + { scheme: { service_name: "testy", + sensitive: "1", + scheme_type: "Foyer", + registered_under_care_act: "No", + owning_organisation_id: user.organisation.id, + arrangement_type: "R" } } + end + + it "creates a new scheme for user organisation with valid params and renders correct page" do + expect { post "/schemes", params: }.to change(Scheme, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content(" What client group is this scheme intended for?") + end + + it "creates a new scheme for user organisation with valid params" do + post "/schemes", params: params + + expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) + expect(Scheme.last.service_name).to eq("testy") + expect(Scheme.last.scheme_type).to eq("Foyer") + expect(Scheme.last.sensitive).to eq("Yes") + expect(Scheme.last.registered_under_care_act).to eq("No") + expect(Scheme.last.id).not_to eq(nil) + expect(Scheme.last.has_other_client_group).to eq(nil) + expect(Scheme.last.primary_client_group).to eq(nil) + expect(Scheme.last.secondary_client_group).to eq(nil) + expect(Scheme.last.support_type).to eq(nil) + expect(Scheme.last.intended_stay).to eq(nil) + expect(Scheme.last.id_to_display).to match(/S*/) + end + end + + context "when required params are missing" do + let(:params) do + { scheme: { service_name: "", + scheme_type: "", + registered_under_care_act: "", + arrangement_type: "" } } + end + + it "renders the same page with error message" do + post "/schemes", params: params + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content("Create a new supported housing scheme") + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.scheme_type.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.registered_under_care_act.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.arrangement_type.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.service_name.invalid")) + end + end + + context "when there are no stock owners" do + let(:params) do + { scheme: { service_name: " testy ", + sensitive: "1", + scheme_type: "Foyer", + registered_under_care_act: "No", + arrangement_type: "D" } } + end + + before do + user.organisation.stock_owners.destroy_all + end + + it "infers the user's organisation" do + post "/schemes", params: params + expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) + end + end + + context "when the organisation id param is included" do + let(:organisation) { create(:organisation) } + let(:params) { { scheme: { owning_organisation: organisation } } } + + it "sets the owning organisation correctly" do + post "/schemes", params: params + expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) + end + end end - context "when support services provider is selected" do + context "when making a scheme in a parent organisation of the user's organisation" do + let(:parent_organisation) { create(:organisation) } + let!(:parent_schemes) { create_list(:scheme, 5, owning_organisation: parent_organisation) } let(:params) do - { scheme: { service_name: "testy", + { scheme: { service_name: " testy ", sensitive: "1", scheme_type: "Foyer", registered_under_care_act: "No", - arrangement_type: "R" } } + owning_organisation_id: user.organisation.stock_owners.first.id, + arrangement_type: "D" } } + end + + before do + create(:organisation_relationship, parent_organisation:, child_organisation: user.organisation) + parent_schemes.each do |scheme| + create(:location, scheme:) + end end it "creates a new scheme for user organisation with valid params and renders correct page" do expect { post "/schemes", params: }.to change(Scheme, :count).by(1) follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content(" What client group is this scheme intended for?") + expect(page).to have_content("What client group is this scheme intended for?") end it "creates a new scheme for user organisation with valid params" do post "/schemes", params: params - expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) + expect(Scheme.last.owning_organisation_id).to eq(user.organisation.stock_owners.first.id) expect(Scheme.last.service_name).to eq("testy") expect(Scheme.last.scheme_type).to eq("Foyer") expect(Scheme.last.sensitive).to eq("Yes") @@ -746,34 +889,71 @@ RSpec.describe SchemesController, type: :request do expect(Scheme.last.intended_stay).to eq(nil) expect(Scheme.last.id_to_display).to match(/S*/) end - end - context "when required params are missing" do - let(:params) do - { scheme: { service_name: "", - scheme_type: "", - registered_under_care_act: "", - arrangement_type: "" } } + context "when support services provider is selected" do + let(:params) do + { scheme: { service_name: "testy", + sensitive: "1", + scheme_type: "Foyer", + registered_under_care_act: "No", + owning_organisation_id: user.organisation.stock_owners.first.id, + arrangement_type: "R" } } + end + + it "creates a new scheme for user organisation with valid params and renders correct page" do + expect { post "/schemes", params: }.to change(Scheme, :count).by(1) + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_content(" What client group is this scheme intended for?") + end + + it "creates a new scheme for user organisation with valid params" do + post "/schemes", params: params + + expect(Scheme.last.owning_organisation_id).to eq(user.organisation.stock_owners.first.id) + expect(Scheme.last.service_name).to eq("testy") + expect(Scheme.last.scheme_type).to eq("Foyer") + expect(Scheme.last.sensitive).to eq("Yes") + expect(Scheme.last.registered_under_care_act).to eq("No") + expect(Scheme.last.id).not_to eq(nil) + expect(Scheme.last.has_other_client_group).to eq(nil) + expect(Scheme.last.primary_client_group).to eq(nil) + expect(Scheme.last.secondary_client_group).to eq(nil) + expect(Scheme.last.support_type).to eq(nil) + expect(Scheme.last.intended_stay).to eq(nil) + expect(Scheme.last.id_to_display).to match(/S*/) + end end - it "renders the same page with error message" do - post "/schemes", params: params - expect(response).to have_http_status(:unprocessable_entity) - expect(page).to have_content("Create a new supported housing scheme") - expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.scheme_type.invalid")) - expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.registered_under_care_act.invalid")) - expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.arrangement_type.invalid")) - expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.service_name.invalid")) + context "when required params are missing" do + let(:params) do + { scheme: { service_name: "", + scheme_type: "", + registered_under_care_act: "", + arrangement_type: "", + owning_organisation_id: "" } } + end + + it "renders the same page with error message" do + post "/schemes", params: params + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content("Create a new supported housing scheme") + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.scheme_type.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.registered_under_care_act.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.arrangement_type.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.owning_organisation_id.invalid")) + expect(page).to have_content(I18n.t("activerecord.errors.models.scheme.attributes.service_name.invalid")) + end end - end - context "when the organisation id param is included" do - let(:organisation) { create(:organisation) } - let(:params) { { scheme: { owning_organisation: organisation } } } + context "when the organisation id param is included" do + let(:organisation) { create(:organisation) } + let(:params) { { scheme: { owning_organisation: organisation } } } - it "sets the owning organisation correctly" do - post "/schemes", params: params - expect(Scheme.last.owning_organisation_id).to eq(user.organisation_id) + it "sets the owning organisation correctly" do + post "/schemes", params: params + expect(Scheme.last.owning_organisation_id).to eq(user.organisation.stock_owners.first.id) + end end end end @@ -1813,7 +1993,7 @@ RSpec.describe SchemesController, type: :request do expect(response).to have_http_status(:ok) expect(path).to match("/schemes/#{scheme.id}") expect(page).to have_content(scheme.service_name) - assert_select "a", text: /Change/, count: 2 + assert_select "a", text: /Change/, count: 3 end end end @@ -1958,7 +2138,7 @@ RSpec.describe SchemesController, type: :request do expect(response).to have_http_status(:ok) expect(path).to match("/schemes/#{scheme.id}") expect(page).to have_content(scheme.service_name) - assert_select "a", text: /Change/, count: 2 + assert_select "a", text: /Change/, count: 3 end end end @@ -2016,7 +2196,20 @@ RSpec.describe SchemesController, type: :request do expect(response).to have_http_status(:ok) expect(page).to have_content("Scheme details") expect(page).to have_content("This scheme contains confidential information") - expect(page).not_to have_content("Which organisation owns the housing stock for this scheme?") + end + + context "when there are stock owners" do + let(:parent_organisation) { create(:organisation) } + + before do + create(:organisation_relationship, parent_organisation:, child_organisation: user.organisation) + get "/schemes/#{scheme.id}/edit-name" + end + + it "includes the owning organisation question" do + expect(response).to have_http_status(:ok) + expect(page).to have_content("Which organisation owns the housing stock for this scheme?") + end end context "when attempting to access secondary-client-group scheme page for another organisation" do