From 616188bfe31f53e6f25a9f896ebfd02c29c4ab77 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 4 May 2023 16:15:41 +0200 Subject: [PATCH] feat(administrateur): when clone or preview procedure should use published revision if not the owned --- .../administrateurs/procedures_controller.rb | 16 +++++++------- app/models/procedure.rb | 22 +++++++++++++------ spec/models/procedure_spec.rb | 12 ++++++++++ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index c31d605ad..962ae59b2 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -60,7 +60,12 @@ module Administrateurs end def apercu - @dossier = procedure_without_control.draft_revision.dossier_for_preview(current_user) + revision = if current_administrateur.owns?(procedure_without_control) + procedure_without_control.draft_revision + else + procedure_without_control.active_revision + end + @dossier = revision.dossier_for_preview(current_user) @tab = apercu_tab end @@ -172,13 +177,8 @@ module Administrateurs flash.notice = 'Démarche clonée, pensez a vérifier la Présentation et choisir le service a laquelle cette procédure est associé.' redirect_to admin_procedure_path(id: new_procedure.id) else - if cloned_from_library? - flash.alert = new_procedure.errors.full_messages - redirect_to new_from_existing_admin_procedures_path - else - flash.alert = new_procedure.errors.full_messages - redirect_to admin_procedures_path - end + flash.alert = new_procedure.errors.full_messages + redirect_back(fallback_location: admin_procedures_path) end rescue ActiveRecord::RecordNotFound diff --git a/app/models/procedure.rb b/app/models/procedure.rb index b40c8080f..62e70af54 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -520,12 +520,16 @@ class Procedure < ApplicationRecord is_different_admin = !admin.owns?(self) populate_champ_stable_ids - include_list = { - attestation_template: [], - draft_revision: { - revision_types_de_champ: [:type_de_champ], - dossier_submitted_message: [] - } + + revision_to_clone = if is_different_admin && published_revision_id.present? + :published_revision + else + :draft_revision + end + include_list = { attestation_template: [] } + include_list[revision_to_clone] = { + revision_types_de_champ: [:type_de_champ], + dossier_submitted_message: [] } include_list[:groupe_instructeurs] = :instructeurs if !is_different_admin procedure = self.deep_clone(include: include_list) do |original, kopy| @@ -544,8 +548,12 @@ class Procedure < ApplicationRecord procedure.max_duree_conservation_dossiers_dans_ds = NEW_MAX_DUREE_CONSERVATION end procedure.estimated_dossiers_count = 0 - procedure.published_revision = nil + if procedure.published_revision.present? + procedure.draft_revision = procedure.published_revision + end procedure.draft_revision.procedure = procedure + procedure.draft_revision.published_at = nil + procedure.published_revision = nil if is_different_admin procedure.administrateurs = [admin] diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index a61564470..decb9a6a5 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -669,6 +669,18 @@ describe Procedure do expect(subject.groupe_instructeurs.first.label).to eq(GroupeInstructeur::DEFAUT_LABEL) expect(subject.groupe_instructeurs.first.instructeurs.size).to eq(0) end + + context 'when published' do + before { procedure.publish! } + + it 'should clone published revision as draft' do + expect(procedure.revisions.size).to eq(2) + expect(procedure.published_revision).not_to be_nil + expect(subject.published_revision).to be_nil + expect(subject.draft_revision.published_at).to be_nil + expect(subject.draft_revision.types_de_champ.first.stable_id).to eq(procedure.published_revision.types_de_champ.first.stable_id) + end + end end it 'should duplicate existing mail_templates' do