feat(sva): transition to en_instruction when (re-)submitting a dossier

This commit is contained in:
Colin Darie 2023-06-02 17:51:02 +02:00
parent c3480f550d
commit afe373c6b4
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
6 changed files with 118 additions and 13 deletions

View file

@ -185,6 +185,7 @@ module Users
if errors.blank?
@dossier.passer_en_construction!
@dossier.process_declarative!
@dossier.process_sva_svr! # TODO no en construction email if SVA/SVR (and declarative?)
NotificationMailer.send_en_construction_notification(@dossier).deliver_later
@dossier.groupe_instructeur.instructeurs.with_instant_email_dossier_notifications.each do |instructeur|
DossierMailer.notify_new_dossier_depose_to_instructeur(@dossier, instructeur.email).deliver_later
@ -233,6 +234,7 @@ module Users
if cast_bool(params.dig(:dossier, :pending_correction_confirm))
editing_fork_origin.resolve_pending_correction!
editing_fork_origin.process_sva_svr!
end
redirect_to dossier_path(editing_fork_origin)

View file

@ -39,6 +39,7 @@ module DossierCorrectableConcern
def resolve_pending_correction!
pending_corrections.update!(resolved_at: Time.current)
pending_corrections.reset
end
end
end

View file

@ -569,7 +569,11 @@ class Dossier < ApplicationRecord
end
def can_passer_automatiquement_en_instruction?
(declarative_triggered_at.nil? && procedure.declarative_en_instruction?) || procedure.auto_archive_on&.then { _1 <= Time.zone.today }
return true if declarative_triggered_at.nil? && procedure.declarative_en_instruction?
return true if procedure.auto_archive_on? && !procedure.auto_archive_on.future?
return true if procedure.sva_svr_enabled? && sva_svr_decision_triggered_at.nil? && !pending_correction?
false
end
def can_repasser_en_instruction?
@ -902,9 +906,12 @@ class Dossier < ApplicationRecord
def after_passer_automatiquement_en_instruction
self.en_construction_close_to_expiration_notice_sent_at = nil
self.conservation_extension = 0.days
self.en_instruction_at = self.declarative_triggered_at = self.traitements
.passer_en_instruction
.processed_at
self.en_instruction_at = traitements.passer_en_instruction.processed_at
if procedure.declarative_en_instruction?
self.declarative_triggered_at = en_instruction_at
end
save!
NotificationMailer.send_en_instruction_notification(self).deliver_later
@ -1048,6 +1055,14 @@ class Dossier < ApplicationRecord
end
end
def process_sva_svr!
return unless procedure.sva_svr_enabled?
return if sva_svr_decision_triggered_at.present?
self.sva_svr_decision_on = SVASVRDateCalculatorService.new(self, procedure).calculate
passer_automatiquement_en_instruction!
end
def remove_titres_identite!
champs_public.filter(&:titre_identite?).map(&:piece_justificative_file).each(&:purge_later)
end

View file

@ -522,6 +522,47 @@ describe Users::DossiersController, type: :controller do
it "resolve correction" do
expect { subject }.to change { correction.reload.resolved_at }.to be_truthy
end
context 'when procedure has sva enabled' do
let(:procedure) { create(:procedure, :sva) }
let!(:dossier) { create(:dossier, :en_construction, procedure:, user:) }
it 'passe automatiquement en instruction' do
expect(dossier.pending_correction?).to be_truthy
subject
dossier.reload
expect(dossier).to be_en_instruction
expect(dossier.pending_correction?).to be_falsey
expect(dossier.en_instruction_at).to within(5.seconds).of(Time.current)
end
end
end
context 'when there is sva without confirming correction' do
let!(:correction) { create(:dossier_correction, dossier: dossier) }
subject { post :submit_en_construction, params: { id: dossier.id } }
it "does not resolve correction" do
expect { subject }.not_to change { correction.reload.resolved_at }
end
context 'when procedure has sva enabled' do
let(:procedure) { create(:procedure, :sva) }
let!(:dossier) { create(:dossier, :en_construction, procedure:, user:) }
it 'does not passe automatiquement en instruction' do
expect(dossier.pending_correction?).to be_truthy
subject
dossier.reload
expect(dossier).to be_en_construction
expect(dossier.pending_correction?).to be_truthy
end
end
end
end

View file

@ -412,6 +412,10 @@ FactoryBot.define do
build(:dossier_submitted_message, revisions: [procedure.active_revision])
end
end
trait :sva do
sva_svr { SVASVRConfiguration.new(decision: :sva).attributes }
end
end
end

View file

@ -1062,20 +1062,49 @@ describe Dossier, type: :model do
end
describe '#passer_automatiquement_en_instruction!' do
let(:dossier) { create(:dossier, :en_construction, :with_declarative_en_instruction, en_construction_close_to_expiration_notice_sent_at: Time.zone.now) }
let(:last_operation) { dossier.dossier_operation_logs.last }
let(:operation_serialized) { last_operation.data }
let(:instructeur) { create(:instructeur) }
before { dossier.passer_automatiquement_en_instruction! }
context "via procedure declarative en instruction" do
let(:dossier) { create(:dossier, :en_construction, :with_declarative_en_instruction, en_construction_close_to_expiration_notice_sent_at: Time.zone.now) }
it { expect(dossier.followers_instructeurs).not_to include(instructeur) }
it { expect(dossier.en_construction_close_to_expiration_notice_sent_at).to be_nil }
it { expect(last_operation.operation).to eq('passer_en_instruction') }
it { expect(last_operation.automatic_operation?).to be_truthy }
it { expect(operation_serialized['operation']).to eq('passer_en_instruction') }
it { expect(operation_serialized['dossier_id']).to eq(dossier.id) }
it { expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601) }
subject do
dossier.process_declarative!
dossier.reload
end
it 'passes dossier en instruction' do
expect(subject.followers_instructeurs).not_to include(instructeur)
expect(subject.en_construction_close_to_expiration_notice_sent_at).to be_nil
expect(subject.declarative_triggered_at).to be_within(1.second).of(Time.current)
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
end
context "via procedure sva" do
let(:procedure) { create(:procedure, :sva, :published) }
let(:dossier) { create(:dossier, :en_construction, procedure:) }
subject do
dossier.process_sva_svr!
dossier.reload
end
it 'passes dossier en instruction' do
expect(subject.followers_instructeurs).not_to include(instructeur)
expect(subject.sva_svr_decision_on).to eq(2.months.from_now.to_date)
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
end
end
describe '#can_passer_automatiquement_en_instruction?' do
@ -1115,6 +1144,19 @@ describe Dossier, type: :model do
it { expect(dossier.can_passer_automatiquement_en_instruction?).to be_truthy }
end
end
context 'when procedure has sva or svr enabled' do
let(:procedure) { create(:procedure, :published, :sva) }
let(:dossier) { create(:dossier, :en_construction, procedure:) }
it { expect(dossier.can_passer_automatiquement_en_instruction?).to be_truthy }
context 'when dossier was already processed by sva' do
let(:dossier) { create(:dossier, :en_construction, procedure:, sva_svr_decision_triggered_at: 1.hour.ago) }
it { expect(dossier.can_passer_automatiquement_en_instruction?).to be_falsey }
end
end
end
describe '#can_accepter_automatiquement?' do