[#2550] Generate attestation on auto-accepted dossier

This commit is contained in:
Frederic Merizen 2018-09-25 15:53:57 +02:00
parent 0b990fd6ec
commit 7631bde16a
2 changed files with 15 additions and 10 deletions

View file

@ -3,22 +3,24 @@ class AutoReceiveDossiersForProcedureJob < ApplicationJob
def perform(procedure_id, state)
procedure = Procedure.find(procedure_id)
attrs = case state
case state
when Dossier.states.fetch(:en_instruction)
{
procedure.dossiers.state_en_construction.update_all(
state: Dossier.states.fetch(:en_instruction),
en_instruction_at: DateTime.now
}
)
when Dossier.states.fetch(:accepte)
{
state: Dossier.states.fetch(:accepte),
en_instruction_at: DateTime.now,
processed_at: DateTime.now
}
procedure.dossiers.state_en_construction.find_each do |dossier|
dossier.update(
state: Dossier.states.fetch(:accepte),
en_instruction_at: DateTime.now,
processed_at: DateTime.now
)
dossier.attestation = dossier.build_attestation
dossier.save
end
else
raise "Receiving Procedure##{procedure_id} in invalid state \"#{state}\""
end
procedure.dossiers.state_en_construction.update_all(attrs)
end
end

View file

@ -7,6 +7,7 @@ RSpec.describe AutoReceiveDossiersForProcedureJob, type: :job do
before do
Timecop.freeze(date)
create(:attestation_template, procedure: nouveau_dossier1.procedure)
AutoReceiveDossiersForProcedureJob.new.perform(procedure_id, state)
end
@ -41,10 +42,12 @@ RSpec.describe AutoReceiveDossiersForProcedureJob, type: :job do
it { expect(nouveau_dossier1.reload.accepte?).to be true }
it { expect(nouveau_dossier1.reload.en_instruction_at).to eq(date) }
it { expect(nouveau_dossier1.reload.processed_at).to eq(date) }
it { expect(nouveau_dossier1.reload.attestation).to be_present }
it { expect(nouveau_dossier2.reload.accepte?).to be true }
it { expect(nouveau_dossier2.reload.en_instruction_at).to eq(date) }
it { expect(nouveau_dossier2.reload.processed_at).to eq(date) }
it { expect(nouveau_dossier2.reload.attestation).to be_present }
it { expect(dossier_recu.reload.en_instruction?).to be true }
it { expect(dossier_recu.reload.en_instruction_at).to eq(instruction_date) }