feat(administrateur): when clone or preview procedure should use published revision if not the owned

This commit is contained in:
Paul Chavard 2023-05-04 16:15:41 +02:00
parent e0071cf1bb
commit 616188bfe3
3 changed files with 35 additions and 15 deletions

View file

@ -60,7 +60,12 @@ module Administrateurs
end end
def apercu 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 @tab = apercu_tab
end 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é.' 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) redirect_to admin_procedure_path(id: new_procedure.id)
else else
if cloned_from_library?
flash.alert = new_procedure.errors.full_messages flash.alert = new_procedure.errors.full_messages
redirect_to new_from_existing_admin_procedures_path redirect_back(fallback_location: admin_procedures_path)
else
flash.alert = new_procedure.errors.full_messages
redirect_to admin_procedures_path
end
end end
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound

View file

@ -520,13 +520,17 @@ class Procedure < ApplicationRecord
is_different_admin = !admin.owns?(self) is_different_admin = !admin.owns?(self)
populate_champ_stable_ids populate_champ_stable_ids
include_list = {
attestation_template: [], revision_to_clone = if is_different_admin && published_revision_id.present?
draft_revision: { :published_revision
else
:draft_revision
end
include_list = { attestation_template: [] }
include_list[revision_to_clone] = {
revision_types_de_champ: [:type_de_champ], revision_types_de_champ: [:type_de_champ],
dossier_submitted_message: [] dossier_submitted_message: []
} }
}
include_list[:groupe_instructeurs] = :instructeurs if !is_different_admin include_list[:groupe_instructeurs] = :instructeurs if !is_different_admin
procedure = self.deep_clone(include: include_list) do |original, kopy| procedure = self.deep_clone(include: include_list) do |original, kopy|
PiecesJustificativesService.clone_attachments(original, kopy) PiecesJustificativesService.clone_attachments(original, kopy)
@ -544,8 +548,12 @@ class Procedure < ApplicationRecord
procedure.max_duree_conservation_dossiers_dans_ds = NEW_MAX_DUREE_CONSERVATION procedure.max_duree_conservation_dossiers_dans_ds = NEW_MAX_DUREE_CONSERVATION
end end
procedure.estimated_dossiers_count = 0 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.procedure = procedure
procedure.draft_revision.published_at = nil
procedure.published_revision = nil
if is_different_admin if is_different_admin
procedure.administrateurs = [admin] procedure.administrateurs = [admin]

View file

@ -669,6 +669,18 @@ describe Procedure do
expect(subject.groupe_instructeurs.first.label).to eq(GroupeInstructeur::DEFAUT_LABEL) expect(subject.groupe_instructeurs.first.label).to eq(GroupeInstructeur::DEFAUT_LABEL)
expect(subject.groupe_instructeurs.first.instructeurs.size).to eq(0) expect(subject.groupe_instructeurs.first.instructeurs.size).to eq(0)
end 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 end
it 'should duplicate existing mail_templates' do it 'should duplicate existing mail_templates' do