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:
gregoirenovel 2018-02-21 17:01:41 +01:00 committed by GitHub
commit adffe1cd05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 14 deletions

View file

@ -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

View 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

View file

@ -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