diff --git a/app/jobs/auto_receive_dossiers_for_procedure_job.rb b/app/jobs/auto_receive_dossiers_for_procedure_job.rb index 6fa5968ac..ee04c9cbb 100644 --- a/app/jobs/auto_receive_dossiers_for_procedure_job.rb +++ b/app/jobs/auto_receive_dossiers_for_procedure_job.rb @@ -2,9 +2,23 @@ class AutoReceiveDossiersForProcedureJob < ApplicationJob queue_as :cron def perform(procedure_id, state) - procedure = Procedure.find_by(id: procedure_id) - if procedure - procedure.dossiers.state_en_construction.update_all(state: state, en_instruction_at: Time.now) + procedure = Procedure.find(procedure_id) + attrs = case state + when :en_instruction + { + state: :en_instruction, + en_instruction_at: DateTime.now + } + when :accepte + { + state: :accepte, + en_instruction_at: DateTime.now, + processed_at: DateTime.now + } + else + raise "Receiving Procedure##{procedure_id} in invalid state \"#{state}\"" end + + procedure.dossiers.state_en_construction.update_all(attrs) end end diff --git a/lib/tasks/2018_02_20_set_processed_at.rake b/lib/tasks/2018_02_20_set_processed_at.rake new file mode 100644 index 000000000..2282e7f94 --- /dev/null +++ b/lib/tasks/2018_02_20_set_processed_at.rake @@ -0,0 +1,7 @@ +namespace :'2018_02_20_set_processed_at' do + task set: :environment do + Dossier.where(state: :accepte, processed_at: nil).find_each do |dossier| + dossier.update_column(:processed_at, dossier.en_instruction_at) + end + end +end diff --git a/spec/jobs/auto_receive_dossiers_for_procedure_job_spec.rb b/spec/jobs/auto_receive_dossiers_for_procedure_job_spec.rb index 0de95f720..a7a546b43 100644 --- a/spec/jobs/auto_receive_dossiers_for_procedure_job_spec.rb +++ b/spec/jobs/auto_receive_dossiers_for_procedure_job_spec.rb @@ -7,7 +7,7 @@ RSpec.describe AutoReceiveDossiersForProcedureJob, type: :job do before { Timecop.freeze(date) } after { Timecop.return } - subject { AutoReceiveDossiersForProcedureJob.new.perform(procedure_id, 'en_instruction') } + subject { AutoReceiveDossiersForProcedureJob.new.perform(procedure_id, state) } context "with some dossiers" do let(:nouveau_dossier1) { create(:dossier, :en_construction) } @@ -16,19 +16,46 @@ RSpec.describe AutoReceiveDossiersForProcedureJob, type: :job do let(:dossier_brouillon) { create(:dossier, procedure: dossier_recu.procedure) } let(:procedure_id) { dossier_brouillon.procedure_id } - it do - subject - expect(nouveau_dossier1.reload.en_instruction?).to be true - expect(nouveau_dossier1.reload.en_instruction_at).to eq(date) + context "en_construction" do + let(:state) { :en_instruction } - expect(nouveau_dossier2.reload.en_instruction?).to be true - expect(nouveau_dossier2.reload.en_instruction_at).to eq(date) + it do + subject + expect(nouveau_dossier1.reload.en_instruction?).to be true + expect(nouveau_dossier1.reload.en_instruction_at).to eq(date) - expect(dossier_recu.reload.en_instruction?).to be true - expect(dossier_recu.reload.en_instruction_at).to eq(date) + expect(nouveau_dossier2.reload.en_instruction?).to be true + expect(nouveau_dossier2.reload.en_instruction_at).to eq(date) - expect(dossier_brouillon.reload.brouillon?).to be true - expect(dossier_brouillon.reload.en_instruction_at).to eq(nil) + expect(dossier_recu.reload.en_instruction?).to be true + expect(dossier_recu.reload.en_instruction_at).to eq(date) + + expect(dossier_brouillon.reload.brouillon?).to be true + expect(dossier_brouillon.reload.en_instruction_at).to eq(nil) + end + end + + context "accepte" do + let(:state) { :accepte } + + it do + subject + expect(nouveau_dossier1.reload.accepte?).to be true + expect(nouveau_dossier1.reload.en_instruction_at).to eq(date) + expect(nouveau_dossier1.reload.processed_at).to eq(date) + + expect(nouveau_dossier2.reload.accepte?).to be true + expect(nouveau_dossier2.reload.en_instruction_at).to eq(date) + expect(nouveau_dossier2.reload.processed_at).to eq(date) + + expect(dossier_recu.reload.en_instruction?).to be true + expect(dossier_recu.reload.en_instruction_at).to eq(date) + expect(dossier_recu.reload.processed_at).to eq(nil) + + expect(dossier_brouillon.reload.brouillon?).to be true + expect(dossier_brouillon.reload.en_instruction_at).to eq(nil) + expect(dossier_brouillon.reload.processed_at).to eq(nil) + end end end end