diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index cb03b7c39..48b38840c 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -69,31 +69,7 @@ class Administrateur < ApplicationRecord end def can_be_deleted? - procedures.all? { |p| p.administrateurs.count > 1 || p.dossiers.empty? } - end - - def delete_and_transfer_services - if !can_be_deleted? - fail "Impossible de supprimer cet administrateur car il a des démarches où il est le seul administrateur" - end - - procedures.with_discarded.each do |procedure| - next if procedure.service.nil? - - next_administrateur = procedure.administrateurs.where.not(id: self.id).first - procedure.service.update(administrateur: next_administrateur) - - if (procedure.administrateurs.count == 1 && procedure.dossiers.empty?) - procedure.destroy - end - end - - services.each do |service| - # 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 - AdministrateursProcedure.where(administrateur_id: self.id).delete_all - destroy + procedures.with_discarded.all? { |p| p.administrateurs.count > 1 || p.dossiers.empty? } end def merge(old_admin) diff --git a/app/services/administrateur_deletion_service.rb b/app/services/administrateur_deletion_service.rb index 0b4809384..75d2ee458 100644 --- a/app/services/administrateur_deletion_service.rb +++ b/app/services/administrateur_deletion_service.rb @@ -47,7 +47,7 @@ class AdministrateurDeletionService def delete_procedures_without_dossier procedures_without_dossier = owned_procedures.filter { _1.dossiers.empty? } - procedures_without_dossier.each { _1.discard_and_keep_track!(super_admin) } + procedures_without_dossier.each { |p| p.discard_and_keep_track!(super_admin) unless p.discarded? } procedures_without_dossier.each(&:purge_discarded) end diff --git a/spec/models/administrateur_spec.rb b/spec/models/administrateur_spec.rb index 7f6dc04b3..247d8526a 100644 --- a/spec/models/administrateur_spec.rb +++ b/spec/models/administrateur_spec.rb @@ -49,47 +49,6 @@ describe Administrateur, type: :model do end end - describe '#delete_and_transfer_services' do - let!(:administrateur) { create(:administrateur) } - let!(:autre_administrateur) { create(:administrateur) } - let!(:procedure) { create(:procedure, :with_service, administrateurs: [administrateur, autre_administrateur]) } - let(:service) { procedure.service } - - it "delete and transfer services to other admin" do - service.update(administrateur: administrateur) - administrateur.delete_and_transfer_services - - expect(Administrateur.find_by(id: administrateur.id)).to be_nil - expect(service.reload.administrateur).to eq(autre_administrateur) - end - - it "delete service if not associated to procedures" do - service_without_procedure = create(:service, administrateur: administrateur) - administrateur.delete_and_transfer_services - - 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 - - context "proedure without service" do - let!(:procedure) { create(:procedure, :draft, administrateurs: [administrateur, autre_administrateur]) } - - it "delete procedure without service" do - administrateur.delete_and_transfer_services - expect(Administrateur.find_by(id: administrateur.id)).to be_nil - end - end - end - describe '#merge' do let(:new_admin) { create(:administrateur) } let(:old_admin) { create(:administrateur) } diff --git a/spec/services/administrateur_deletion_service_spec.rb b/spec/services/administrateur_deletion_service_spec.rb index 2a64392cb..c899b1b55 100644 --- a/spec/services/administrateur_deletion_service_spec.rb +++ b/spec/services/administrateur_deletion_service_spec.rb @@ -48,6 +48,17 @@ describe AdministrateurDeletionService do end end + context 'when admin has one discarded procedure without dossiers and only one admin' do + let(:owned_procedure_without_dossier) { create(:procedure, service: owned_procedure_service, administrateurs: [admin]) } + + before { owned_procedure_without_dossier.discard! } + + it "deletes admin" do + subject + expect(Administrateur.find_by(id: admin.id)).to be_nil + end + end + context "when there is a failure" do it 'rollbacks' do allow_any_instance_of(Service).to receive(:update).and_return(false)