Merge pull request #5036 from betagouv/fix-deletion-admin

Manager: Suppression des administrateurs avec des procédures archivées
This commit is contained in:
Keirua 2020-04-14 09:57:30 +02:00 committed by GitHub
commit f6aef9fb66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View file

@ -81,13 +81,14 @@ class Administrateur < ApplicationRecord
fail "Impossible de supprimer cet administrateur car il a des démarches où il est le seul administrateur"
end
procedures.each do |procedure|
procedures.with_discarded.each do |procedure|
next_administrateur = procedure.administrateurs.where.not(id: self.id).first
procedure.service.update(administrateur: next_administrateur)
end
services.each do |service|
service.destroy unless service.procedures.any?
# We can't destroy a service if it has procedures, even if those procedures are archived
service.destroy unless service.procedures.with_discarded.any?
end
destroy

View file

@ -65,6 +65,15 @@ describe Administrateur, type: :model do
expect(Service.find_by(id: service_without_procedure.id)).to be_nil
expect(Administrateur.find_by(id: administrateur.id)).to be_nil
end
it "does not delete service if associated to an archived procedure" do
service.update(administrateur: administrateur)
procedure.discard!
administrateur.delete_and_transfer_services
expect(Service.find_by(id: procedure.service.id)).not_to be_nil
expect(Administrateur.find_by(id: administrateur.id)).to be_nil
end
end
# describe '#password_complexity' do