diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 80e5629e7..8f9c31861 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -405,7 +405,7 @@ class Procedure < ApplicationRecord times = dossiers .state_termine .pluck(start_attribute, end_attribute) - .map { |times| times[1] - times[0] } + .map { |(start_date, end_date)| end_date - start_date } if times.present? times.sum.fdiv(times.size).ceil diff --git a/lib/tasks/2018_09_27_fill_missing_en_instruction_at.rake b/lib/tasks/2018_09_27_fill_missing_en_instruction_at.rake new file mode 100644 index 000000000..e100e11ef --- /dev/null +++ b/lib/tasks/2018_09_27_fill_missing_en_instruction_at.rake @@ -0,0 +1,9 @@ +namespace :'2018_09_27_fill_missing_en_instruction_at' do + task run: :environment do + dossiers_with_missing_instruction_at = Dossier + .where.not(processed_at: nil) + .where(en_instruction_at: nil) + + dossiers_with_missing_instruction_at.each { |d| d.update(en_instruction_at: d.processed_at) } + end +end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index d41f2413a..9642789c0 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -773,4 +773,23 @@ describe Procedure do expect(p.juridique_required).to be_truthy end end + + describe '#mean_instruction_time' do + let(:procedure) { create(:procedure) } + + context 'when there is only one dossier' do + let(:dossier) { create(:dossier, procedure: procedure) } + + context 'which is termine' do + before do + dossier.accepte! + processed_date = DateTime.parse('12/12/2012') + instruction_date = processed_date - 1.day + dossier.update(en_instruction_at: instruction_date, processed_at: processed_date) + end + + it { expect(procedure.mean_instruction_time).to eq(1.day.to_i) } + end + end + end end