feat(sva): cron accepts dossiers for each sva procedure

This commit is contained in:
Colin Darie 2023-06-05 18:22:33 +02:00
parent 3612eddf79
commit 5db80ee6de
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
6 changed files with 100 additions and 0 deletions

View file

@ -0,0 +1,11 @@
class Cron::ProcedureProcessSVASVRJob < Cron::CronJob
self.schedule_expression = "every day at 1:00"
def perform
Procedure.sva_svr.find_each do |procedure|
procedure.dossiers.state_en_construction_ou_instruction.find_each do |dossier|
ProcedureSVASVRProcessDossierJob.perform_later(dossier)
end
end
end
end

View file

@ -0,0 +1,7 @@
class ProcedureSVASVRProcessDossierJob < ApplicationJob
queue_as :sva
def perform(dossier)
dossier.process_sva_svr!
end
end

View file

@ -2,6 +2,7 @@ module ProcedureSVASVRConcern
extend ActiveSupport::Concern
included do
scope :sva_svr, -> { where("sva_svr ->> 'decision' IN (?)", ['sva', 'svr']) }
validate :sva_svr_immutable_on_published, if: :will_save_change_to_sva_svr?
def sva_svr_enabled?

View file

@ -1076,6 +1076,8 @@ class Dossier < ApplicationRecord
passer_automatiquement_en_instruction!
elsif en_instruction? && procedure.sva? && may_accepter_automatiquement?
accepter_automatiquement!
elsif will_save_change_to_sva_svr_decision_on?
save! # we always want the most up to date decision when there is a pending correction
end
end

View file

@ -0,0 +1,33 @@
RSpec.describe Cron::ProcedureProcessSVASVRJob, type: :job do
let!(:dossier) { create(:dossier, :en_instruction, :with_individual, procedure:, depose_at: 2.months.ago, sva_svr_decision_on: Date.current) }
let!(:dossier_in_future) { create(:dossier, :en_instruction, :with_individual, procedure:, depose_at: 1.day.ago, sva_svr_decision_on: Date.yesterday + 2.months) }
let!(:dossier_en_construction) { create(:dossier, :en_construction, :with_individual, procedure:, depose_at: 2.months.ago, sva_svr_decision_on: Date.current) }
let!(:dossier_en_brouillon) { create(:dossier, :brouillon, :with_individual, procedure:) }
subject(:perform_job) { described_class.perform_now }
before { perform_job }
context 'when procedure is published' do
let(:procedure) { create(:procedure, :published, :sva, :for_individual) }
it 'queues ProcedureSVASVRProcessDossierJob for published sva procedure' do
expect(ProcedureSVASVRProcessDossierJob).to have_been_enqueued.with(dossier)
expect(ProcedureSVASVRProcessDossierJob).to have_been_enqueued.with(dossier_en_construction)
expect(ProcedureSVASVRProcessDossierJob).to have_been_enqueued.with(dossier_in_future)
expect(ProcedureSVASVRProcessDossierJob).not_to have_been_enqueued.with(dossier_en_brouillon)
expect(ProcedureSVASVRProcessDossierJob).to have_been_enqueued.exactly(3).times
end
end
context 'when procedure is closed' do
let(:procedure) { create(:procedure, :closed, :sva, :for_individual) }
it { expect(ProcedureSVASVRProcessDossierJob).to have_been_enqueued.with(dossier) }
end
context 'when procedure is not sva' do
let(:procedure) { create(:procedure, :published, :for_individual) }
it { expect(ProcedureSVASVRProcessDossierJob).not_to have_been_enqueued }
end
end

View file

@ -0,0 +1,46 @@
RSpec.describe ProcedureSVASVRProcessDossierJob, type: :job do
include ActiveJob::TestHelper
include ActiveSupport::Testing::TimeHelpers
let(:procedure) { create(:procedure, :published, :sva, :for_individual) }
let!(:dossier) { create(:dossier, :en_instruction, :with_individual, procedure:, depose_at: 2.months.ago, sva_svr_decision_on: Date.current) }
subject do
described_class.perform_now(dossier)
dossier.reload
end
context 'when procedure is SVA' do
it 'should accept dossier' do
expect(subject.sva_svr_decision_on).to eq(Date.current)
expect(subject).to be_accepte
expect(subject.processed_at).to within(1.second).of(Time.current)
end
context 'when decision is scheduled in the future' do
let!(:dossier) { create(:dossier, :en_instruction, :with_individual, procedure:, depose_at: 1.day.ago, sva_svr_decision_on: Date.yesterday + 2.months) }
it 'should not accept dossier' do
expect { subject }.not_to change { dossier.reload.updated_at }
expect(subject).to be_en_instruction
end
end
context 'when dossier has pending correction / is en_construction' do
before do
travel_to 2.days.ago do # create correction in past so it will be 2 days of delay
dossier.flag_as_pending_correction!(build(:commentaire, dossier: dossier))
end
end
it 'should not accept dossier' do
subject
expect(dossier).to be_en_construction
end
it 'should update sva_svr_decision_on with corrections delay' do
expect { subject }.to change { dossier.reload.sva_svr_decision_on }.from(Date.current).to(Date.current + 2.days)
end
end
end
end