From 1b6f8f507ec3336abeace11c0bcdf631588ef700 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 20 Aug 2024 10:04:26 +0100 Subject: [PATCH] Add organisation change confirmation page --- app/controllers/users_controller.rb | 11 ++++- app/helpers/user_helper.rb | 20 +++++++++ .../organisation_change_confirmation.html.erb | 24 ++++++++++ config/routes.rb | 2 +- spec/helpers/user_helper_spec.rb | 45 ++++++++++++++++++- spec/requests/users_controller_spec.rb | 43 +++++++++++++++++- 6 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 app/views/users/organisation_change_confirmation.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7610e7efb..e5952a80f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -171,12 +171,21 @@ class UsersController < ApplicationController validate_log_reassignment if @user.errors.empty? - redirect_to user_confirm_organisation_change_path(@user, log_reassignment_params) + redirect_to user_organisation_change_confirmation_path(@user, log_reassignment_params) else render :log_reassignment, status: :unprocessable_entity end end + def organisation_change_confirmation + if params[:organisation_id].blank? || params[:log_reassignment].blank? || !Organisation.where(id: params[:organisation_id]).exists? + return redirect_to user_path(@user) + end + + @new_organisation = Organisation.find(params[:organisation_id]) + @log_reassignment = params[:log_reassignment] + end + private def validate_attributes diff --git a/app/helpers/user_helper.rb b/app/helpers/user_helper.rb index c80264203..6b11fc4d4 100644 --- a/app/helpers/user_helper.rb +++ b/app/helpers/user_helper.rb @@ -21,4 +21,24 @@ module UserHelper "You’re moving #{user.name} from #{user.organisation.name} to #{new_organisation.name}. There #{logs_count_text} assigned to them." end + + def organisation_change_confirmation_warning(user, new_organisation, log_reassignment) + log_reassignment_text = "There are no logs assigned to them." + + logs_count = user.assigned_to_lettings_logs.count + user.assigned_to_sales_logs.count + if logs_count.positive? + case log_reassignment + when "reassign_all" + log_reassignment_text = "The stock owner and managing agent on their logs will change to #{new_organisation.name}." + when "reassign_stock_owner" + log_reassignment_text = "The stock owner on their logs will change to #{new_organisation.name}." + when "reassign_managing_agent" + log_reassignment_text = "The managing agent on their logs will change to #{new_organisation.name}." + when "unassign" + log_reassignment_text = "Their logs will be unassigned." + end + end + + "You’re moving #{user.name} from #{user.organisation.name} to #{new_organisation.name}. #{log_reassignment_text}" + end end diff --git a/app/views/users/organisation_change_confirmation.html.erb b/app/views/users/organisation_change_confirmation.html.erb new file mode 100644 index 000000000..8dc56e17c --- /dev/null +++ b/app/views/users/organisation_change_confirmation.html.erb @@ -0,0 +1,24 @@ +<% content_for :before_content do %> + <% content_for :title, "Are you sure you want to move this user?" %> + <%= govuk_back_link(href: :back) %> +<% end %> + +
+
+

+ <%= content_for(:title) %> +

+ + <%= govuk_warning_text(text: organisation_change_confirmation_warning(@user, @new_organisation, @log_reassignment)) %> + + <%= form_with model: @user, url: user_organisation_change_confirmation_path(@user), method: :patch do |f| %> + <%= f.hidden_field :organisation_id, value: @new_organisation.id %> + <%= f.hidden_field :log_reassignment, value: @log_reassignment %> + +
+ <%= f.govuk_submit "Move this user" %> + <%= govuk_button_link_to "Cancel", user_log_reassignment_path(@user, organisation_id: @new_organisation.id, log_reassignment: @log_reassignment), secondary: true %> +
+ <% end %> +
+
diff --git a/config/routes.rb b/config/routes.rb index d30d1aa88..e99c3b9bb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -126,7 +126,7 @@ Rails.application.routes.draw do get "edit-key-contact", to: "users#key_contact" get "log-reassignment", to: "users#log_reassignment" patch "log-reassignment", to: "users#update_log_reassignment" - patch "confirm-organisation-change", to: "users#confirm-organisation-change" + get "organisation-change-confirmation", to: "users#organisation_change_confirmation" collection do get :search diff --git a/spec/helpers/user_helper_spec.rb b/spec/helpers/user_helper_spec.rb index d5b51abae..91a02f2f6 100644 --- a/spec/helpers/user_helper_spec.rb +++ b/spec/helpers/user_helper_spec.rb @@ -92,7 +92,7 @@ RSpec.describe UserHelper do end end - context "when user owns multiple log" do + context "when user owns multiple logs" do before do create(:lettings_log, assigned_to: user) create(:sales_log, assigned_to: user) @@ -104,4 +104,47 @@ RSpec.describe UserHelper do end end end + + describe "organisation_change_confirmation_warning" do + context "when user owns logs" do + before do + create(:lettings_log, assigned_to: user) + end + + context "with reassign all choice" do + it "returns the correct message" do + expected_text = "You’re moving #{user.name} from #{user.organisation.name} to #{current_user.organisation.name}. The stock owner and managing agent on their logs will change to #{current_user.organisation.name}." + expect(organisation_change_confirmation_warning(user, current_user.organisation, "reassign_all")).to eq(expected_text) + end + end + + context "with reassign stock owners choice" do + it "returns the correct message" do + expected_text = "You’re moving #{user.name} from #{user.organisation.name} to #{current_user.organisation.name}. The stock owner on their logs will change to #{current_user.organisation.name}." + expect(organisation_change_confirmation_warning(user, current_user.organisation, "reassign_stock_owner")).to eq(expected_text) + end + end + + context "with reassign all choice" do + it "returns the correct message" do + expected_text = "You’re moving #{user.name} from #{user.organisation.name} to #{current_user.organisation.name}. The managing agent on their logs will change to #{current_user.organisation.name}." + expect(organisation_change_confirmation_warning(user, current_user.organisation, "reassign_managing_agent")).to eq(expected_text) + end + end + + context "with reassign all choice" do + it "returns the correct message" do + expected_text = "You’re moving #{user.name} from #{user.organisation.name} to #{current_user.organisation.name}. Their logs will be unassigned." + expect(organisation_change_confirmation_warning(user, current_user.organisation, "unassign")).to eq(expected_text) + end + end + end + + context "when user doesn't own logs" do + it "returns the correct message" do + expected_text = "You’re moving #{user.name} from #{user.organisation.name} to #{current_user.organisation.name}. There are no logs assigned to them." + expect(organisation_change_confirmation_warning(user, current_user.organisation, "reassign_all")).to eq(expected_text) + end + end + end end diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index 57796f9b0..1d5b68387 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -2108,7 +2108,7 @@ RSpec.describe UsersController, type: :request do end it "redirects to confirmation page" do - expect(response).to redirect_to("/users/#{other_user.id}/confirm-organisation-change?log_reassignment=reassign_all&organisation_id=#{new_organisation.id}") + expect(response).to redirect_to("/users/#{other_user.id}/organisation-change-confirmation?log_reassignment=reassign_all&organisation_id=#{new_organisation.id}") end end @@ -2124,7 +2124,7 @@ RSpec.describe UsersController, type: :request do end it "redirects to confirmation page" do - expect(response).to redirect_to("/users/#{other_user.id}/confirm-organisation-change?log_reassignment=unassign&organisation_id=#{new_organisation.id}") + expect(response).to redirect_to("/users/#{other_user.id}/organisation-change-confirmation?log_reassignment=unassign&organisation_id=#{new_organisation.id}") end end @@ -2441,6 +2441,45 @@ RSpec.describe UsersController, type: :request do end end end + + describe "#confirm_organisation_change" do + context "when organisation id is not given" do + it "redirects to the user page" do + get "/users/#{other_user.id}/organisation-change-confirmation?log_reassignment=reassign_all" + expect(response).to redirect_to("/users/#{other_user.id}") + end + end + + context "when reassignment option is not given" do + let(:new_organisation) { create(:organisation, name: "new org") } + + it "redirects to the user page" do + get "/users/#{other_user.id}/organisation-change-confirmation?organisation_id=#{new_organisation.id}" + expect(response).to redirect_to("/users/#{other_user.id}") + end + end + + context "when organisation id does not exist" do + it "redirects to the user page" do + get "/users/#{other_user.id}/organisation-change-confirmation?organisation_id=123123&log_reassignment=reassign_all" + expect(response).to redirect_to("/users/#{other_user.id}") + end + end + + context "with valid organisation id" do + let(:new_organisation) { create(:organisation, name: "new org") } + + before do + create(:lettings_log, assigned_to: other_user) + end + + it "displays confirm organisation change page" do + get "/users/#{other_user.id}/organisation-change-confirmation?organisation_id=#{new_organisation.id}&log_reassignment=reassign_all" + expect(page).to have_content("Are you sure you want to move this user?") + expect(page).to have_content("You’re moving #{other_user.name} from #{other_user.organisation_name} to #{new_organisation.name}. The stock owner and managing agent on their logs will change to #{new_organisation.name}.") + end + end + end end describe "title link" do