Merge pull request #4889 from betagouv/4888-fix-suppression-administrateur

remove orphan services when destroying administrateur
This commit is contained in:
krichtof 2020-03-16 22:18:57 +01:00 committed by GitHub
commit b4058e85eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View file

@ -85,6 +85,11 @@ class Administrateur < ApplicationRecord
next_administrateur = procedure.administrateurs.where.not(id: self.id).first next_administrateur = procedure.administrateurs.where.not(id: self.id).first
procedure.service.update(administrateur: next_administrateur) procedure.service.update(administrateur: next_administrateur)
end end
services.each do |service|
service.destroy unless service.procedures.any?
end
destroy destroy
end end
end end

View file

@ -59,6 +59,14 @@ describe Administrateur, type: :model do
expect(Administrateur.find_by(id: administrateur.id)).to be_nil expect(Administrateur.find_by(id: administrateur.id)).to be_nil
expect(service.reload.administrateur).to eq(autre_administrateur) expect(service.reload.administrateur).to eq(autre_administrateur)
end 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
end end
# describe '#password_complexity' do # describe '#password_complexity' do