diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7543f7984..2f7bb2bd6 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -33,7 +33,7 @@ class UsersController < ApplicationController end def search - user_options = current_user.support? ? User.all : User.affiliated_users(current_user.organisation) + user_options = current_user.support? ? User.all : User.own_and_managing_org_users(current_user.organisation) users = user_options.search_by(params["query"]).limit(20) user_data = users.each_with_object({}) do |user, hash| diff --git a/app/helpers/filters_helper.rb b/app/helpers/filters_helper.rb index f941f2e65..5f8488bc9 100644 --- a/app/helpers/filters_helper.rb +++ b/app/helpers/filters_helper.rb @@ -115,7 +115,7 @@ module FiltersHelper selected_user = if current_user.support? User.where(id: user_id)&.first else - User.affiliated_users(current_user.organisation).where(id: user_id)&.first + User.own_and_managing_org_users(current_user.organisation).where(id: user_id)&.first end return [OpenStruct.new(id: selected_user.id, name: selected_user.name, hint: selected_user.email)] if selected_user.present? @@ -309,8 +309,8 @@ private return "All" if session_filters["assigned_to"].include?("all") return "You" if session_filters["assigned_to"].include?("you") - User.affiliated_users(current_user.organisation).find(session_filters["user"].to_i).name - selected_user_option = User.affiliated_users(current_user.organisation).find(session_filters["user"].to_i) + User.own_and_managing_org_users(current_user.organisation).find(session_filters["user"].to_i).name + selected_user_option = User.own_and_managing_org_users(current_user.organisation).find(session_filters["user"].to_i) return unless selected_user_option "#{selected_user_option.name} (#{selected_user_option.email})" diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 04d6b9ff6..10ab612cd 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -132,7 +132,7 @@ class LettingsLog < Log illness_type_10: false) } - scope :filter_by_user_text_search, ->(param, user) { where(assigned_to: user.support? ? User.search_by(param) : User.affiliated_users(user.organisation).search_by(param)) } + scope :filter_by_user_text_search, ->(param, user) { where(assigned_to: user.support? ? User.search_by(param) : User.own_and_managing_org_users(user.organisation).search_by(param)) } scope :filter_by_owning_organisation_text_search, ->(param, _user) { where(owning_organisation: Organisation.search_by(param)) } scope :filter_by_managing_organisation_text_search, ->(param, _user) { where(managing_organisation: Organisation.search_by(param)) } diff --git a/app/models/log.rb b/app/models/log.rb index 73f739e83..3a6c1e982 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -53,7 +53,7 @@ class Log < ApplicationRecord scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) } scope :filter_by_managing_organisation, ->(managing_organisation, _user = nil) { where(managing_organisation:) } - scope :filter_by_user_text_search, ->(param, user) { where(assigned_to: user.support? ? User.search_by(param) : User.affiliated_users(user.organisation).search_by(param)) } + scope :filter_by_user_text_search, ->(param, user) { where(assigned_to: user.support? ? User.search_by(param) : User.own_and_managing_org_users(user.organisation).search_by(param)) } scope :filter_by_owning_organisation_text_search, ->(param, _user) { where(owning_organisation: Organisation.search_by(param)) } scope :filter_by_managing_organisation_text_search, ->(param, _user) { where(managing_organisation: Organisation.search_by(param)) } diff --git a/app/models/user.rb b/app/models/user.rb index 6da8b8ba8..c79ceb0d9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -77,7 +77,7 @@ class User < ApplicationRecord scope :deactivated, -> { where(active: false) } scope :active_status, -> { where(active: true).where.not(last_sign_in_at: nil) } scope :visible, -> { where(discarded_at: nil) } - scope :affiliated_users, ->(organisation) { where(organisation: organisation.child_organisations + [organisation] + organisation.parent_organisations) } + scope :own_and_managing_org_users, ->(organisation) { where(organisation: organisation.child_organisations + [organisation]) } def lettings_logs if support? diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index 28798e51b..8e87f7f28 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -416,18 +416,18 @@ RSpec.describe UsersController, type: :request do let(:parent_relationship) { create(:organisation_relationship, parent_organisation: user.organisation) } let(:child_relationship) { create(:organisation_relationship, child_organisation: user.organisation) } let!(:org_user) { create(:user, organisation: user.organisation, name: "test_name") } - let!(:managing_user) { create(:user, organisation: child_relationship.parent_organisation, name: "stock_owner_test_name") } - let!(:owner_user) { create(:user, organisation: parent_relationship.child_organisation, name: "managing_agent_test_name") } + let!(:managing_user) { create(:user, organisation: parent_relationship.child_organisation, name: "managing_agent_test_name") } before do + create(:user, organisation: child_relationship.parent_organisation, name: "stock_owner_test_name") create(:user, name: "other_organisation_test_name") end - it "only searches within the current user's organisation, managing agents and stock owners" do + it "only searches within the current user's organisation and managing agents" do get "/users/search", headers:, params: { query: "test_name" } result = JSON.parse(response.body) - expect(result.count).to eq(3) - expect(result.keys).to match_array([org_user.id.to_s, managing_user.id.to_s, owner_user.id.to_s]) + expect(result.count).to eq(2) + expect(result.keys).to match_array([org_user.id.to_s, managing_user.id.to_s]) end end end @@ -1205,18 +1205,18 @@ RSpec.describe UsersController, type: :request do let(:parent_relationship) { create(:organisation_relationship, parent_organisation: user.organisation) } let(:child_relationship) { create(:organisation_relationship, child_organisation: user.organisation) } let!(:org_user) { create(:user, organisation: user.organisation, email: "test_name@example.com") } - let!(:managing_user) { create(:user, organisation: child_relationship.parent_organisation, email: "stock_owner_test_name@example.com") } - let!(:owner_user) { create(:user, organisation: parent_relationship.child_organisation, email: "managing_agent_test_name@example.com") } + let!(:managing_user) { create(:user, organisation: parent_relationship.child_organisation, email: "managing_agent_test_name@example.com") } before do create(:user, email: "other_organisation_test_name@example.com") + create(:user, organisation: child_relationship.parent_organisation, email: "stock_owner_test_name@example.com") end - it "only searches within the current user's organisation, managing agents and stock owners" do + it "only searches within the current user's organisation and managing agents" do get "/users/search", headers:, params: { query: "test_name" } result = JSON.parse(response.body) - expect(result.count).to eq(3) - expect(result.keys).to match_array([org_user.id.to_s, managing_user.id.to_s, owner_user.id.to_s]) + expect(result.count).to eq(2) + expect(result.keys).to match_array([org_user.id.to_s, managing_user.id.to_s]) end end end