Browse Source

Add log reassignment page

pull/2596/head
Kat 2 years ago
parent
commit
d26b20ada6
  1. 10
      app/controllers/users_controller.rb
  2. 7
      app/helpers/user_helper.rb
  3. 17
      app/models/user.rb
  4. 4
      app/policies/user_policy.rb
  5. 34
      app/views/users/log_reassignment.html.erb
  6. 43
      spec/helpers/user_helper_spec.rb
  7. 54
      spec/requests/users_controller_spec.rb

10
app/controllers/users_controller.rb

@ -153,6 +153,16 @@ class UsersController < ApplicationController
redirect_to users_organisation_path(@user.organisation), notice: I18n.t("notification.user_deleted", name: @user.name)
end
def log_reassignment
authorize @user
if params[:organisation_id].present? && Organisation.where(id: params[:organisation_id]).exists?
@new_organisation = Organisation.find(params[:organisation_id])
else
redirect_to user_path(@user)
end
end
private
def validate_attributes

7
app/helpers/user_helper.rb

@ -14,4 +14,11 @@ module UserHelper
def delete_user_link(user)
govuk_button_link_to "Delete this user", delete_confirmation_user_path(user), warning: true
end
def organisation_change_warning(user, new_organisation)
logs_count = user.assigned_to_lettings_logs.count + user.assigned_to_sales_logs.count
logs_count_text = logs_count == 1 ? "is #{logs_count} log" : "are #{logs_count} logs"
"You’re moving #{user.name} from #{user.organisation.name} to #{new_organisation.name}. There #{logs_count_text} assigned to them."
end
end

17
app/models/user.rb

@ -49,6 +49,13 @@ class User < ApplicationRecord
support: 99,
}.freeze
LOG_REASSIGNMENT = {
reassign_all: "Yes, change the stock owner and the managing agent",
reassign_stock_owner: "Yes, change the stock owner but keep the managing agent the same",
reassign_managing_agent: "Yes, change the managing agent but keep the stock owner the same",
unassign: "No, unassign the logs",
}.freeze
enum role: ROLES
scope :search_by_name, ->(name) { where("users.name ILIKE ?", "%#{name}%") }
@ -80,6 +87,8 @@ class User < ApplicationRecord
scope :visible, -> { where(discarded_at: nil) }
scope :own_and_managing_org_users, ->(organisation) { where(organisation: organisation.child_organisations + [organisation]) }
attr_accessor :log_reassignment
def lettings_logs
if support?
LettingsLog.all
@ -270,6 +279,14 @@ class User < ApplicationRecord
"#{phone}, Ext. #{phone_extension}"
end
def assigned_to_lettings_logs
lettings_logs.where(assigned_to: self)
end
def assigned_to_sales_logs
sales_logs.where(assigned_to: self)
end
protected
# Checks whether a password is needed or not. For validations only.

4
app/policies/user_policy.rb

@ -49,6 +49,10 @@ class UserPolicy
@current_user.support? && @user.active?
end
def log_reassignment?
edit_organisation?
end
private
def has_any_logs_in_editable_collection_period

34
app/views/users/log_reassignment.html.erb

@ -0,0 +1,34 @@
<% content_for :title, "Should this user’s logs move to their new organisation?" %>
<% content_for :before_content do %>
<%= govuk_back_link(href: aliased_user_edit(@user, current_user)) %>
<% end %>
<%= form_for(@user, html: { method: :patch }) do |f| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= f.govuk_error_summary %>
<h1 class="govuk-heading-l">
<%= content_for(:title) %>
</h1>
<%= govuk_warning_text do %>
<%= organisation_change_warning(@user, @new_organisation) %>
<% end %>
<% log_reassignment = User::LOG_REASSIGNMENT.map { |key, value| OpenStruct.new(id: key, name: value) } %>
<%= f.govuk_collection_radio_buttons :log_reassignment,
log_reassignment,
:id,
:name,
legend: { text: "Log reassignment", hidden: true } %>
<div class="govuk-button-group">
<%= f.govuk_submit "Continue" %>
<%= govuk_button_link_to "Cancel", aliased_user_edit(@user, current_user), secondary: true %>
</div>
</div>
</div>
<% end %>

43
spec/helpers/user_helper_spec.rb

@ -61,4 +61,47 @@ RSpec.describe UserHelper do
end
end
end
describe "organisation_change_warning" do
context "when user doesn't own any logs" do
it "returns a message with the number of logs" do
expected_text = "You’re moving #{user.name} from #{user.organisation.name} to #{current_user.organisation.name}. There are 0 logs assigned to them."
expect(organisation_change_warning(user, current_user.organisation)).to eq(expected_text)
end
end
context "when user owns 1 lettings log" do
before do
create(:lettings_log, assigned_to: user)
end
it "returns a message with the number of logs" do
expected_text = "You’re moving #{user.name} from #{user.organisation.name} to #{current_user.organisation.name}. There is 1 log assigned to them."
expect(organisation_change_warning(user, current_user.organisation)).to eq(expected_text)
end
end
context "when user owns 1 sales log" do
before do
create(:sales_log, assigned_to: user)
end
it "returns a message with the number of logs" do
expected_text = "You’re moving #{user.name} from #{user.organisation.name} to #{current_user.organisation.name}. There is 1 log assigned to them."
expect(organisation_change_warning(user, current_user.organisation)).to eq(expected_text)
end
end
context "when user owns multiple log" do
before do
create(:lettings_log, assigned_to: user)
create(:sales_log, assigned_to: user)
end
it "returns a message with the number of logs" do
expected_text = "You’re moving #{user.name} from #{user.organisation.name} to #{current_user.organisation.name}. There are 2 logs assigned to them."
expect(organisation_change_warning(user, current_user.organisation)).to eq(expected_text)
end
end
end
end

54
spec/requests/users_controller_spec.rb

@ -124,6 +124,13 @@ RSpec.describe UsersController, type: :request do
expect(response).to redirect_to("/account/sign-in")
end
end
describe "#log_reassignment" do
it "redirects to the sign in page" do
get "/users/#{user.id}/log-reassignment"
expect(response).to redirect_to("/account/sign-in")
end
end
end
context "when user is signed in as a data provider" do
@ -433,6 +440,13 @@ RSpec.describe UsersController, type: :request do
expect(result.keys).to match_array([org_user.id.to_s, managing_user.id.to_s])
end
end
describe "#log_reassignment" do
it "returns unauthorized status" do
get "/users/#{user.id}/log-reassignment"
expect(response).to have_http_status(:unauthorized)
end
end
end
context "when user is signed in as a data coordinator" do
@ -1234,6 +1248,13 @@ RSpec.describe UsersController, type: :request do
expect(result.keys).to match_array([org_user.id.to_s, managing_user.id.to_s])
end
end
describe "#log_reassignment" do
it "returns unauthorised status" do
get "/users/#{user.id}/log-reassignment"
expect(response).to have_http_status(:unauthorized)
end
end
end
context "when user is signed in as a support user" do
@ -2272,6 +2293,39 @@ RSpec.describe UsersController, type: :request do
expect(result.keys).to match_array([org_user.id.to_s, managing_user.id.to_s, owner_user.id.to_s, other_user.id.to_s])
end
end
describe "#log_reassignment" do
context "when organisation id is not given" do
it "redirects to the user page" do
get "/users/#{other_user.id}/log-reassignment"
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}/log-reassignment?organisation_id=123123"
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 "allows reassigning logs" do
get "/users/#{other_user.id}/log-reassignment?organisation_id=#{new_organisation.id}"
expect(page).to have_content("Should this user’s logs move to their new organisation?")
expect(page).to have_content("You’re moving #{other_user.name} from #{other_user.organisation_name} to new org. There is 1 log assigned to them.")
expect(page).to have_button("Continue")
expect(page).to have_link("Back", href: "/users/#{other_user.id}/edit")
expect(page).to have_link("Cancel", href: "/users/#{other_user.id}/edit")
end
end
end
end
describe "title link" do

Loading…
Cancel
Save