Create a Job that receive automatically each new dossier from a specific procedure

This commit is contained in:
Mathieu Magnin 2017-10-05 15:22:56 +02:00
parent 5eff915b9d
commit c5487657db
3 changed files with 53 additions and 0 deletions

View file

@ -0,0 +1,10 @@
class AutoReceiveDossiersForProcedureJob < ApplicationJob
queue_as :cron
def perform(procedure_id)
procedure = Procedure.find_by(id: procedure_id)
if procedure
procedure.dossiers.state_nouveaux.update_all(state: "received", received_at: Time.now)
end
end
end

View file

@ -79,5 +79,14 @@ FactoryGirl.define do
dossier.save!
end
end
trait :received do
after(:create) do |dossier, _evaluator|
dossier.state = 'received'
dossier.initiated_at = dossier.created_at + 1.minute
dossier.created_at = dossier.created_at + 2.minute
dossier.save!
end
end
end
end

View file

@ -0,0 +1,34 @@
require 'rails_helper'
RSpec.describe AutoReceiveDossiersForProcedureJob, type: :job do
describe "perform" do
let(:date) { Time.utc(2017, 9, 1, 10, 5, 0) }
before { Timecop.freeze(date) }
subject { AutoReceiveDossiersForProcedureJob.new.perform(procedure_id) }
context "with some dossiers" do
let(:nouveau_dossier1) { create(:dossier, :initiated) }
let(:nouveau_dossier2) { create(:dossier, :initiated, procedure: nouveau_dossier1.procedure) }
let(:dossier_recu) { create(:dossier, :received, procedure: nouveau_dossier2.procedure) }
let(:dossier_draft) { create(:dossier, procedure: dossier_recu.procedure) }
let(:procedure_id) { dossier_draft.procedure_id }
it do
subject
expect(nouveau_dossier1.reload.received?).to be true
expect(nouveau_dossier1.reload.received_at).to eq(date)
expect(nouveau_dossier2.reload.received?).to be true
expect(nouveau_dossier2.reload.received_at).to eq(date)
expect(dossier_recu.reload.received?).to be true
expect(dossier_recu.reload.received_at).to eq(date)
expect(dossier_draft.reload.draft?).to be true
expect(dossier_draft.reload.received_at).to eq(nil)
end
end
end
end