Browse Source

Add organisation change confirmation page

pull/2596/head
Kat 2 years ago
parent
commit
1b6f8f507e
  1. 11
      app/controllers/users_controller.rb
  2. 20
      app/helpers/user_helper.rb
  3. 24
      app/views/users/organisation_change_confirmation.html.erb
  4. 2
      config/routes.rb
  5. 45
      spec/helpers/user_helper_spec.rb
  6. 43
      spec/requests/users_controller_spec.rb

11
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

20
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

24
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 %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop">
<h1 class="govuk-heading-xl">
<%= content_for(:title) %>
</h1>
<%= 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 %>
<div class="govuk-button-group">
<%= 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 %>
</div>
<% end %>
</div>
</div>

2
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

45
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

43
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

Loading…
Cancel
Save