Merge pull request #7935 from betagouv/fix_duplicate_service_in_merge

fix(admin_merge): résout un pb lorsque les 2 admins ont un service avec le mm nom
This commit is contained in:
LeSim 2022-10-19 18:08:26 +02:00 committed by GitHub
commit 345e609889
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View file

@ -122,7 +122,18 @@ class Administrateur < ApplicationRecord
p.administrateurs.delete(old_admin)
end
old_admin.services.update_all(administrateur_id: id)
old_services = old_admin.services
new_service_by_nom = services.index_by(&:nom)
old_services.each do |old_service|
corresponding_service = new_service_by_nom[old_service.nom]
if corresponding_service.present?
old_service.procedures.update_all(service_id: corresponding_service.id)
old_service.destroy
else
old_service.update(administrateur_id: id)
end
end
instructeurs_with_new_admin, instructeurs_without_new_admin = old_admin.instructeurs
.partition { |i| i.administrateurs.exists?(id) }

View file

@ -133,6 +133,25 @@ describe Administrateur, type: :model do
end
end
context 'when both admins have a service with the same name' do
let(:service_1) { create(:service, nom: 'S', administrateur: old_admin) }
let(:service_2) { create(:service, nom: 'S', administrateur: new_admin) }
let(:procedure_1) { create(:procedure, service: service_1) }
before do
service_1
service_2
procedure_1
subject
[new_admin, old_admin, service_2].map(&:reload)
end
it 'removes the service from the old one' do
expect(old_admin.services).to be_empty
expect(service_2.procedures).to include(procedure_1)
end
end
context 'when the old admin has an instructeur' do
let(:instructeur) { create(:instructeur) }