fix(sva): don't process dossiers submitted before sva was enabled

This commit is contained in:
Colin Darie 2023-06-28 12:18:26 +02:00
parent 7225f1b023
commit 73a28d2eca
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
4 changed files with 33 additions and 7 deletions

View file

@ -1084,7 +1084,12 @@ class Dossier < ApplicationRecord
return unless procedure.sva_svr_enabled?
return if sva_svr_decision_triggered_at.present?
# set or recompute sva date, except for dossiers submitted before sva was enabled
if depose_at.today? || sva_svr_decision_on.present?
self.sva_svr_decision_on = SVASVRDecisionDateCalculatorService.new(self, procedure).decision_date
end
return if sva_svr_decision_on.nil?
if en_construction? && may_passer_automatiquement_en_instruction?
passer_automatiquement_en_instruction!

View file

@ -1,6 +1,7 @@
FactoryBot.define do
factory :commentaire do
association :dossier, :en_construction
email { generate(:user_email) }
body { 'plop' }

View file

@ -3,7 +3,7 @@ RSpec.describe ProcedureSVASVRProcessDossierJob, type: :job do
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) }
let!(:dossier) { create(:dossier, :en_instruction, :with_individual, procedure:, depose_at: 2.months.ago - 1.day, sva_svr_decision_on: Date.current) }
subject do
described_class.perform_now(dossier)
@ -18,7 +18,7 @@ RSpec.describe ProcedureSVASVRProcessDossierJob, type: :job do
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) }
let!(:dossier) { create(:dossier, :en_instruction, :with_individual, procedure:, depose_at: 1.day.ago, sva_svr_decision_on: 2.months.from_now.to_date) }
it 'should not accept dossier' do
expect { subject }.not_to change { dossier.reload.updated_at }
@ -28,7 +28,7 @@ RSpec.describe ProcedureSVASVRProcessDossierJob, type: :job do
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
travel_to 2.days.ago do # create correction in past so it will be 3 days of delay
dossier.flag_as_pending_correction!(build(:commentaire, dossier: dossier))
end
end
@ -39,8 +39,18 @@ RSpec.describe ProcedureSVASVRProcessDossierJob, type: :job do
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)
expect { subject }.to change { dossier.reload.sva_svr_decision_on }.from(Date.current).to(Date.current + 3.days)
end
end
end
context 'when dossier was submitted before sva was enabled' do
let!(:dossier) { create(:dossier, :en_instruction, :with_individual, procedure:, depose_at: 2.months.ago) }
it 'should be noop' do
expect(subject.sva_svr_decision_on).to be_nil
expect(subject).to be_en_instruction
expect(subject.processed_at).to be_nil
end
end
end

View file

@ -1116,7 +1116,7 @@ describe Dossier, type: :model do
context "via procedure sva" do
let(:procedure) { create(:procedure, :sva, :published, :for_individual) }
let(:dossier) { create(:dossier, :en_construction, :with_individual, procedure:) }
let(:dossier) { create(:dossier, :en_construction, :with_individual, procedure:, sva_svr_decision_on: 10.days.from_now) }
subject do
dossier.process_sva_svr!
@ -1124,14 +1124,24 @@ describe Dossier, type: :model do
end
it 'passes dossier en instruction' do
expect(subject.state).to eq('en_instruction')
expect(subject.followers_instructeurs).not_to include(instructeur)
expect(subject.sva_svr_decision_on).to eq(2.months.from_now.to_date + 1.day)
expect(subject.sva_svr_decision_on).to eq(2.months.from_now.to_date + 1.day) # date is updated
expect(last_operation.operation).to eq('passer_en_instruction')
expect(last_operation.automatic_operation?).to be_truthy
expect(operation_serialized['operation']).to eq('passer_en_instruction')
expect(operation_serialized['dossier_id']).to eq(dossier.id)
expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601)
end
context 'when dossier was submitted with sva not yet enabled' do
let(:dossier) { create(:dossier, :en_construction, :with_individual, procedure:, depose_at: 10.days.ago) }
it 'leaves dossier en construction' do
expect(subject.sva_svr_decision_on).to be_nil
expect(subject.state).to eq('en_construction')
end
end
end
end