diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index 2388508db..96d6847fb 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -44,7 +44,7 @@ module Administrateurs end def apercu - @dossier = procedure_without_control.draft_revision.new_dossier + @dossier = procedure_without_control.draft_revision.dossier_for_preview(current_user) @tab = apercu_tab end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 520190c88..34c36b010 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -544,7 +544,7 @@ class Dossier < ApplicationRecord end def can_transition_to_en_construction? - brouillon? && procedure.dossier_can_transition_to_en_construction? + brouillon? && procedure.dossier_can_transition_to_en_construction? && !for_procedure_preview? end def can_repasser_en_instruction? @@ -1246,7 +1246,7 @@ class Dossier < ApplicationRecord end def send_draft_notification_email - if brouillon? && !procedure.declarative? + if brouillon? && !procedure.declarative? && !for_procedure_preview? DossierMailer.notify_new_draft(self).deliver_later end end diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 09d425847..942ee2ea7 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -132,6 +132,19 @@ class ProcedureRevision < ApplicationRecord ) end + def dossier_for_preview(user) + dossier = Dossier + .create_with(groupe_instructeur: procedure.defaut_groupe_instructeur_for_new_dossier) + .find_or_initialize_by(revision: self, user: user, for_procedure_preview: true, state: Dossier.states.fetch(:brouillon)) + + if dossier.new_record? + dossier.build_default_individual + dossier.save! + end + + dossier + end + private def compare_attestation_template(from_at, to_at) diff --git a/spec/controllers/administrateurs/procedures_controller_spec.rb b/spec/controllers/administrateurs/procedures_controller_spec.rb index a53d204fc..b34663ef1 100644 --- a/spec/controllers/administrateurs/procedures_controller_spec.rb +++ b/spec/controllers/administrateurs/procedures_controller_spec.rb @@ -23,7 +23,11 @@ describe Administrateurs::ProceduresController, type: :controller do get :apercu, params: { id: procedure.id } end - it { expect(response).to have_http_status(:ok) } + it do + expect(response).to have_http_status(:ok) + expect(procedure.dossiers.visible_by_user).to be_empty + expect(procedure.dossiers.for_procedure_preview).not_to be_empty + end end let(:procedure_params) {