From 904d8d208bc4f0b64e3671e1deed2649036288a0 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Thu, 20 Dec 2018 11:13:58 +0100 Subject: [PATCH] [Fix #3192] Fix data --- ...ne_service_for_transferred_procedures.rake | 32 +++++++++++++++++++ ...ce_for_transferred_procedures.rake_spec.rb | 32 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 lib/tasks/deployment/20181219164523_clone_service_for_transferred_procedures.rake create mode 100644 spec/lib/tasks/deployment/20181219164523_clone_service_for_transferred_procedures.rake_spec.rb diff --git a/lib/tasks/deployment/20181219164523_clone_service_for_transferred_procedures.rake b/lib/tasks/deployment/20181219164523_clone_service_for_transferred_procedures.rake new file mode 100644 index 000000000..99c7452b8 --- /dev/null +++ b/lib/tasks/deployment/20181219164523_clone_service_for_transferred_procedures.rake @@ -0,0 +1,32 @@ +namespace :after_party do + desc 'Deployment task: clone_service_for_transferred_procedures' + task clone_service_for_transferred_procedures: :environment do + puts "Running deploy task 'clone_service_for_transferred_procedures'" + + procedures = Procedure.includes(:service).where.not(service_id: nil) + procedures_to_fix_in_array = procedures.select do |p| + p.administrateur_id != p.service.administrateur_id + end + procedures_to_fix = Procedure.where(id: procedures_to_fix_in_array.map(&:id)) + + service_and_admin_list = procedures_to_fix.group(:service_id, :administrateur_id).count.keys + progress = ProgressReport.new(service_and_admin_list.count) + + service_and_admin_list.each do |service_id, administrateur_id| + cloned_service = Service.find(service_id).clone_and_assign_to_administrateur(Administrateur.find(administrateur_id)) + cloned_service.save! + + procedures_to_fix + .where(service_id: service_id, administrateur_id: administrateur_id) + .update_all(service_id: cloned_service.id) + + progress.inc + end + + progress.finish + + # Update task as completed. If you remove the line below, the task will + # run with every deploy (or every time you call after_party:run). + AfterParty::TaskRecord.create version: '20181219164523' + end # task :clone_service_for_transferred_procedures +end # namespace :after_party diff --git a/spec/lib/tasks/deployment/20181219164523_clone_service_for_transferred_procedures.rake_spec.rb b/spec/lib/tasks/deployment/20181219164523_clone_service_for_transferred_procedures.rake_spec.rb new file mode 100644 index 000000000..a3995ddc9 --- /dev/null +++ b/spec/lib/tasks/deployment/20181219164523_clone_service_for_transferred_procedures.rake_spec.rb @@ -0,0 +1,32 @@ +describe '20181219164523_clone_service_for_transferred_procedures.rake' do + let(:rake_task) { Rake::Task['after_party:clone_service_for_transferred_procedures'] } + + subject do + rake_task.invoke + end + + after do + rake_task.reenable + end + + context 'procedures from different admins share the same service' do + let(:admin1) { create(:administrateur) } + let(:admin2) { create(:administrateur) } + let(:service) { create(:service, administrateur: admin1) } + let!(:procedure1) { create(:procedure, service: service, administrateur: admin1) } + let!(:procedure2) { create(:procedure, service: service, administrateur: admin2) } + let!(:procedure3) { create(:procedure, service: service, administrateur: admin2) } + + it 'clones service for procedure2 & procedure3' do + subject + expect(procedure1.reload.service).not_to eq(procedure2.reload.service) + expect(procedure1.reload.service).not_to eq(procedure3.reload.service) + expect(procedure2.reload.service).to eq(procedure3.reload.service) + end + + it 'does nothing for procedure1' do + subject + expect(procedure1.reload.service).to eq(service) + end + end +end