Merge pull request #1455 from tchak/set-processed_at-on-auto-received-dossiers
Set `processed_at` on auto recieved dossiers
This commit is contained in:
commit
adffe1cd05
3 changed files with 62 additions and 14 deletions
|
@ -2,9 +2,23 @@ class AutoReceiveDossiersForProcedureJob < ApplicationJob
|
||||||
queue_as :cron
|
queue_as :cron
|
||||||
|
|
||||||
def perform(procedure_id, state)
|
def perform(procedure_id, state)
|
||||||
procedure = Procedure.find_by(id: procedure_id)
|
procedure = Procedure.find(procedure_id)
|
||||||
if procedure
|
attrs = case state
|
||||||
procedure.dossiers.state_en_construction.update_all(state: state, en_instruction_at: Time.now)
|
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
|
end
|
||||||
|
|
||||||
|
procedure.dossiers.state_en_construction.update_all(attrs)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
7
lib/tasks/2018_02_20_set_processed_at.rake
Normal file
7
lib/tasks/2018_02_20_set_processed_at.rake
Normal file
|
@ -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
|
|
@ -7,7 +7,7 @@ RSpec.describe AutoReceiveDossiersForProcedureJob, type: :job do
|
||||||
before { Timecop.freeze(date) }
|
before { Timecop.freeze(date) }
|
||||||
after { Timecop.return }
|
after { Timecop.return }
|
||||||
|
|
||||||
subject { AutoReceiveDossiersForProcedureJob.new.perform(procedure_id, 'en_instruction') }
|
subject { AutoReceiveDossiersForProcedureJob.new.perform(procedure_id, state) }
|
||||||
|
|
||||||
context "with some dossiers" do
|
context "with some dossiers" do
|
||||||
let(:nouveau_dossier1) { create(:dossier, :en_construction) }
|
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(:dossier_brouillon) { create(:dossier, procedure: dossier_recu.procedure) }
|
||||||
let(:procedure_id) { dossier_brouillon.procedure_id }
|
let(:procedure_id) { dossier_brouillon.procedure_id }
|
||||||
|
|
||||||
it do
|
context "en_construction" do
|
||||||
subject
|
let(:state) { :en_instruction }
|
||||||
expect(nouveau_dossier1.reload.en_instruction?).to be true
|
|
||||||
expect(nouveau_dossier1.reload.en_instruction_at).to eq(date)
|
|
||||||
|
|
||||||
expect(nouveau_dossier2.reload.en_instruction?).to be true
|
it do
|
||||||
expect(nouveau_dossier2.reload.en_instruction_at).to eq(date)
|
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(nouveau_dossier2.reload.en_instruction?).to be true
|
||||||
expect(dossier_recu.reload.en_instruction_at).to eq(date)
|
expect(nouveau_dossier2.reload.en_instruction_at).to eq(date)
|
||||||
|
|
||||||
expect(dossier_brouillon.reload.brouillon?).to be true
|
expect(dossier_recu.reload.en_instruction?).to be true
|
||||||
expect(dossier_brouillon.reload.en_instruction_at).to eq(nil)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue