Use state change methods on dossier in dossier auto recieve job

This commit is contained in:
Paul Chavard 2018-11-26 21:25:50 +01:00
parent a079eaea5c
commit b5b6832be8
3 changed files with 52 additions and 43 deletions

View file

@ -1,24 +1,18 @@
class AutoReceiveDossiersForProcedureJob < ApplicationJob
queue_as :cron
def perform(procedure_id, state)
def perform(procedure_id, state, gestionnaire_id = nil)
procedure = Procedure.find(procedure_id)
gestionnaire = procedure.gestionnaire_for_cron_job
case state
when Dossier.states.fetch(:en_instruction)
procedure.dossiers.state_en_construction.update_all(
state: Dossier.states.fetch(:en_instruction),
en_instruction_at: Time.zone.now
)
procedure.dossiers.state_en_construction.find_each do |dossier|
dossier.passer_en_instruction!(gestionnaire)
end
when Dossier.states.fetch(:accepte)
procedure.dossiers.state_en_construction.find_each do |dossier|
dossier.update(
state: Dossier.states.fetch(:accepte),
en_instruction_at: Time.zone.now,
processed_at: Time.zone.now
)
dossier.attestation = dossier.build_attestation
dossier.save
NotificationMailer.send_closed_notification(dossier).deliver_later
dossier.accepter!('')
end
else
raise "Receiving Procedure##{procedure_id} in invalid state \"#{state}\""

View file

@ -46,6 +46,12 @@ FactoryBot.define do
end
end
trait :with_gestionnaire do
after(:build) do |procedure, _evaluator|
procedure.gestionnaires << create(:gestionnaire)
end
end
trait :with_api_carto do
after(:build) do |procedure, _evaluator|
procedure.module_api_carto.use_api_carto = true

View file

@ -5,57 +5,66 @@ RSpec.describe AutoReceiveDossiersForProcedureJob, type: :job do
let(:date) { Time.utc(2017, 9, 1, 10, 5, 0) }
let(:instruction_date) { date + 120 }
let(:procedure) { create(:procedure, :with_gestionnaire) }
let(:nouveau_dossier1) { create(:dossier, :en_construction, procedure: procedure) }
let(:nouveau_dossier2) { create(:dossier, :en_construction, procedure: procedure) }
let(:dossier_recu) { create(:dossier, :en_instruction, procedure: procedure) }
let(:dossier_brouillon) { create(:dossier, procedure: procedure) }
before do
Timecop.freeze(date)
create(:attestation_template, procedure: nouveau_dossier1.procedure)
AutoReceiveDossiersForProcedureJob.new.perform(procedure_id, state)
nouveau_dossier1
nouveau_dossier2
dossier_recu
dossier_brouillon
create(:attestation_template, procedure: procedure)
AutoReceiveDossiersForProcedureJob.new.perform(procedure.id, state)
end
after { Timecop.return }
context "with some dossiers" do
let(:nouveau_dossier1) { create(:dossier, :en_construction) }
let(:nouveau_dossier2) { create(:dossier, :en_construction, procedure: nouveau_dossier1.procedure) }
let(:dossier_recu) { create(:dossier, :en_instruction, procedure: nouveau_dossier2.procedure) }
let(:dossier_brouillon) { create(:dossier, procedure: dossier_recu.procedure) }
let(:procedure_id) { dossier_brouillon.procedure_id }
context "en_construction" do
let(:state) { Dossier.states.fetch(:en_instruction) }
it { expect(nouveau_dossier1.reload.en_instruction?).to be true }
it { expect(nouveau_dossier1.reload.en_instruction_at).to eq(date) }
it {
expect(nouveau_dossier1.reload.en_instruction?).to be true
expect(nouveau_dossier1.reload.en_instruction_at).to eq(date)
it { expect(nouveau_dossier2.reload.en_instruction?).to be true }
it { expect(nouveau_dossier2.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)
it { expect(dossier_recu.reload.en_instruction?).to be true }
it { expect(dossier_recu.reload.en_instruction_at).to eq(instruction_date) }
expect(dossier_recu.reload.en_instruction?).to be true
expect(dossier_recu.reload.en_instruction_at).to eq(instruction_date)
it { expect(dossier_brouillon.reload.brouillon?).to be true }
it { expect(dossier_brouillon.reload.en_instruction_at).to eq(nil) }
expect(dossier_brouillon.reload.brouillon?).to be true
expect(dossier_brouillon.reload.en_instruction_at).to eq(nil)
}
end
context "accepte" do
let(:state) { Dossier.states.fetch(:accepte) }
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_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_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 }
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(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) }
it { expect(dossier_recu.reload.processed_at).to eq(nil) }
expect(dossier_recu.reload.en_instruction?).to be true
expect(dossier_recu.reload.en_instruction_at).to eq(instruction_date)
expect(dossier_recu.reload.processed_at).to eq(nil)
it { expect(dossier_brouillon.reload.brouillon?).to be true }
it { expect(dossier_brouillon.reload.en_instruction_at).to eq(nil) }
it { expect(dossier_brouillon.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