Browse Source

Skip log reassignment question if user has no logs

pull/2596/head
Kat 2 years ago
parent
commit
6ce9ccb78b
  1. 5
      app/controllers/users_controller.rb
  2. 3
      app/models/user.rb
  3. 12
      app/policies/user_policy.rb
  4. 31
      spec/models/user_spec.rb
  5. 17
      spec/requests/users_controller_spec.rb

5
app/controllers/users_controller.rb

@ -155,6 +155,8 @@ class UsersController < ApplicationController
def log_reassignment def log_reassignment
authorize @user authorize @user
assigned_to_logs_count = @user.assigned_to_lettings_logs.count + @user.assigned_to_sales_logs.count
return redirect_to user_organisation_change_confirmation_path(@user, organisation_id: params[:organisation_id]) if assigned_to_logs_count.zero?
if params[:organisation_id].present? && Organisation.where(id: params[:organisation_id]).exists? if params[:organisation_id].present? && Organisation.where(id: params[:organisation_id]).exists?
@new_organisation = Organisation.find(params[:organisation_id]) @new_organisation = Organisation.find(params[:organisation_id])
@ -164,6 +166,7 @@ class UsersController < ApplicationController
end end
def update_log_reassignment def update_log_reassignment
authorize @user
return redirect_to user_path(@user) unless log_reassignment_params[:organisation_id].present? && Organisation.where(id: log_reassignment_params[:organisation_id]).exists? return redirect_to user_path(@user) unless log_reassignment_params[:organisation_id].present? && Organisation.where(id: log_reassignment_params[:organisation_id]).exists?
@new_organisation = Organisation.find(log_reassignment_params[:organisation_id]) @new_organisation = Organisation.find(log_reassignment_params[:organisation_id])
@ -178,6 +181,7 @@ class UsersController < ApplicationController
end end
def organisation_change_confirmation def organisation_change_confirmation
authorize @user
if params[:organisation_id].blank? || params[:log_reassignment].blank? || !Organisation.where(id: params[:organisation_id]).exists? if params[:organisation_id].blank? || params[:log_reassignment].blank? || !Organisation.where(id: params[:organisation_id]).exists?
return redirect_to user_path(@user) return redirect_to user_path(@user)
end end
@ -187,6 +191,7 @@ class UsersController < ApplicationController
end end
def confirm_organisation_change def confirm_organisation_change
authorize @user
if log_reassignment_params[:organisation_id].blank? || log_reassignment_params[:log_reassignment].blank? || !Organisation.where(id: log_reassignment_params[:organisation_id]).exists? if log_reassignment_params[:organisation_id].blank? || log_reassignment_params[:log_reassignment].blank? || !Organisation.where(id: log_reassignment_params[:organisation_id]).exists?
return redirect_to user_path(@user) return redirect_to user_path(@user)
end end

3
app/models/user.rb

@ -295,6 +295,9 @@ class User < ApplicationRecord
sales_logs_to_reassign = assigned_to_sales_logs sales_logs_to_reassign = assigned_to_sales_logs
current_organisation = organisation current_organisation = organisation
logs_count = lettings_logs_to_reassign.count + sales_logs_to_reassign.count
return if logs_count.positive? && (log_reassignment.blank? || !LOG_REASSIGNMENT.key?(log_reassignment.to_sym))
update!(organisation: new_organisation) update!(organisation: new_organisation)
case log_reassignment case log_reassignment

12
app/policies/user_policy.rb

@ -45,12 +45,16 @@ class UserPolicy
!has_any_logs_in_editable_collection_period && !has_signed_data_protection_agreement? !has_any_logs_in_editable_collection_period && !has_signed_data_protection_agreement?
end end
def edit_organisation? %w[
edit_organisation?
log_reassignment?
update_log_reassignment?
organisation_change_confirmation?
confirm_organisation_change?
].each do |method_name|
define_method method_name do
@current_user.support? && @user.active? @current_user.support? && @user.active?
end end
def log_reassignment?
edit_organisation?
end end
private private

31
spec/models/user_spec.rb

@ -707,5 +707,36 @@ RSpec.describe User, type: :model do
end end
end end
end end
context "when log_reassignent is not given" do
context "and user has no logs" do
let(:user_without_logs) { create(:user) }
it "moves the user to the new organisation" do
user_without_logs.reassign_logs_and_update_organisation(new_organisation, nil)
expect(user_without_logs.organisation).to eq(new_organisation)
end
context "and there is an error" do
before do
allow(user_without_logs).to receive(:update!).and_raise(ActiveRecord::RecordInvalid)
end
it "rolls back the changes" do
user_without_logs.reassign_logs_and_update_organisation(new_organisation, nil)
expect(user_without_logs.organisation).not_to eq(new_organisation)
end
end
end
context "and user has logs" do
it "does not move the user to the new organisation" do
user.reassign_logs_and_update_organisation(new_organisation, nil)
expect(user.organisation).not_to eq(new_organisation)
end
end
end
end end
end end

17
spec/requests/users_controller_spec.rb

@ -2411,6 +2411,10 @@ RSpec.describe UsersController, type: :request do
describe "#log_reassignment" do describe "#log_reassignment" do
context "when organisation id is not given" do context "when organisation id is not given" do
before do
create(:lettings_log, assigned_to: other_user)
end
it "redirects to the user page" do it "redirects to the user page" do
get "/users/#{other_user.id}/log-reassignment" get "/users/#{other_user.id}/log-reassignment"
expect(response).to redirect_to("/users/#{other_user.id}") expect(response).to redirect_to("/users/#{other_user.id}")
@ -2418,6 +2422,10 @@ RSpec.describe UsersController, type: :request do
end end
context "when organisation id does not exist" do context "when organisation id does not exist" do
before do
create(:lettings_log, assigned_to: other_user)
end
it "redirects to the user page" do it "redirects to the user page" do
get "/users/#{other_user.id}/log-reassignment?organisation_id=123123" get "/users/#{other_user.id}/log-reassignment?organisation_id=123123"
expect(response).to redirect_to("/users/#{other_user.id}") expect(response).to redirect_to("/users/#{other_user.id}")
@ -2427,6 +2435,7 @@ RSpec.describe UsersController, type: :request do
context "with valid organisation id" do context "with valid organisation id" do
let(:new_organisation) { create(:organisation, name: "new org") } let(:new_organisation) { create(:organisation, name: "new org") }
context "and user has assigned logs" do
before do before do
create(:lettings_log, assigned_to: other_user) create(:lettings_log, assigned_to: other_user)
end end
@ -2440,6 +2449,14 @@ RSpec.describe UsersController, type: :request do
expect(page).to have_link("Cancel", href: "/users/#{other_user.id}/edit") expect(page).to have_link("Cancel", href: "/users/#{other_user.id}/edit")
end end
end end
context "and user has no assigned logs" do
it "redirects to confirm organisation change page" do
get "/users/#{other_user.id}/log-reassignment?organisation_id=#{new_organisation.id}"
expect(response).to redirect_to("/users/#{other_user.id}/organisation-change-confirmation?organisation_id=#{new_organisation.id}")
end
end
end
end end
describe "#confirm_organisation_change" do describe "#confirm_organisation_change" do

Loading…
Cancel
Save