delete a user

This commit is contained in:
Christophe Robillard 2020-01-08 10:50:16 +01:00 committed by clemkeirua
parent 58ef36ff57
commit 9a62d3fe0c
6 changed files with 73 additions and 6 deletions

View file

@ -20,5 +20,18 @@ module Manager
head :ok
end
def delete
user = User.find(params[:id])
if !user.can_be_deleted?
fail "Impossible de supprimer cet utilisateur car il a des dossiers en instruction"
end
user.delete_and_keep_track_dossiers(current_administration)
logger.info("L'utilisateur #{user.id} est supprimé par #{current_administration.id}")
flash[:notice] = "L'utilisateur #{user.id} est supprimé"
redirect_to manager_users_path
end
end
end

View file

@ -100,6 +100,15 @@ class User < ApplicationRecord
dossiers.state_instruction_commencee.empty?
end
def delete_and_keep_track_dossiers(administration)
if can_be_deleted?
dossiers.each do |dossier|
dossier.delete_and_keep_track(administration)
end
destroy
end
end
private
def link_invites!

View file

@ -25,13 +25,9 @@ as well as a link to its edit page.
</h1>
<div>
<%= link_to(
t("administrate.actions.edit_resource", name: page.page_title),
[:edit, namespace, page.resource],
class: "button",
) if valid_action?(:edit) && show_action?(:edit, page.resource) %>
</div>
<%= button_to "supprimer", delete_manager_user_path(page.resource), method: :delete, disabled: !page.resource.can_be_deleted?, class: "button", data: { confirm: "Confirmez-vous la suppression de l'utilisateur ?" }, title: page.resource.can_be_deleted? ? "Supprimer" : "Cet utilisateur a des dossiers dont l'instruction a commencé et ne peut être supprimé" %>
</div>
<div>
<% if !user.confirmed? %>
<%= link_to('Renvoyer lemail de confirmation', [:resend_confirmation_instructions, namespace, page.resource], method: :post, class: 'button') %>

View file

@ -25,6 +25,7 @@ Rails.application.routes.draw do
end
resources :users, only: [:index, :show] do
delete 'delete', on: :member
post 'resend_confirmation_instructions', on: :member
put 'enable_feature', on: :member
end

View file

@ -0,0 +1,17 @@
describe Manager::UsersController, type: :controller do
let(:administration) { create(:administration) }
describe '#delete' do
let!(:user) { create(:user) }
before { sign_in administration }
subject { delete :delete, params: { id: user.id } }
it 'deletes the user' do
subject
expect(User.find_by(id: user.id)).to be_nil
end
end
end

View file

@ -225,4 +225,35 @@ describe User, type: :model do
it { is_expected.to be true }
end
end
describe '#delete_and_keep_track_dossiers' do
let(:administration) { create(:administration) }
let(:user) { create(:user) }
context 'avec un dossier en instruction' do
let!(:dossier_en_instruction) { create(:dossier, :en_instruction, user: user) }
it 'ne supprime rien si dossier en instruction' do
user.delete_and_keep_track_dossiers(administration)
expect(Dossier.find_by(id: dossier_en_instruction.id)).to be_present
expect(User.find_by(id: user.id)).to be_present
end
end
context 'sans dossier en instruction' do
let!(:dossier_en_construction) { create(:dossier, :en_construction, user: user) }
let!(:dossier_brouillon) { create(:dossier, user: user) }
it "garde une trace des dossiers et supprime l'utilisateur" do
user.delete_and_keep_track_dossiers(administration)
expect(DeletedDossier.find_by(dossier_id: dossier_en_construction)).to be_present
expect(DeletedDossier.find_by(dossier_id: dossier_brouillon)).to be_present
expect(User.find_by(id: user.id)).to be_nil
end
end
end
end