feat(demarche): add possibility to reset draft revision

This commit is contained in:
Paul Chavard 2022-07-06 12:37:08 +02:00
parent 34a075571e
commit 9b45dcea3f
5 changed files with 71 additions and 18 deletions

View file

@ -1,7 +1,7 @@
module Administrateurs
class ProceduresController < AdministrateurController
before_action :retrieve_procedure, only: [:champs, :annotations, :modifications, :edit, :monavis, :update_monavis, :jeton, :update_jeton, :publication, :publish, :transfert, :close, :allow_expert_review, :experts_require_administrateur_invitation]
before_action :procedure_revisable?, only: [:champs, :annotations, :modifications]
before_action :retrieve_procedure, only: [:champs, :annotations, :modifications, :edit, :monavis, :update_monavis, :jeton, :update_jeton, :publication, :publish, :transfert, :close, :allow_expert_review, :experts_require_administrateur_invitation, :reset_draft]
before_action :procedure_revisable?, only: [:champs, :annotations, :modifications, :reset_draft]
ITEMS_PER_PAGE = 25
@ -233,13 +233,18 @@ module Administrateurs
@procedure.assign_attributes(publish_params)
if @procedure.draft_changed? && !@procedure.close?
@procedure.publish_revision!
flash.notice = "Nouvelle version de la démarche publiée"
elsif @procedure.draft_changed? && @procedure.close?
@procedure.publish_or_reopen!(current_administrateur)
@procedure.publish_revision!
flash.notice = "Démarche publiée"
if @procedure.draft_changed?
if @procedure.close?
if @procedure.publish_or_reopen!(current_administrateur)
@procedure.publish_revision!
flash.notice = "Démarche publiée"
else
flash.alert = @procedure.errors.full_messages
end
else
@procedure.publish_revision!
flash.notice = "Nouvelle version de la démarche publiée"
end
elsif @procedure.publish_or_reopen!(current_administrateur)
flash.notice = "Démarche publiée"
else
@ -248,6 +253,11 @@ module Administrateurs
redirect_to admin_procedure_path(@procedure)
end
def reset_draft
@procedure.reset_draft_revision!
redirect_to admin_procedure_path(@procedure)
end
def transfert
end

View file

@ -734,19 +734,20 @@ class Procedure < ApplicationRecord
APIEntrepriseToken.new(api_entreprise_token).expired?
end
def create_new_revision
def create_new_revision(revision = nil)
transaction do
new_draft = draft_revision
new_revision = (revision || draft_revision)
.deep_clone(include: [:revision_types_de_champ])
.tap { |revision| revision.published_at = nil }
.tap(&:save!)
move_new_children_to_new_parent_coordinate(new_draft)
move_new_children_to_new_parent_coordinate(new_revision)
# they are not aware of the new tdcs
new_draft.types_de_champ_public.reset
new_draft.types_de_champ_private.reset
new_revision.types_de_champ_public.reset
new_revision.types_de_champ_private.reset
new_draft
new_revision
end
end
@ -772,6 +773,16 @@ class Procedure < ApplicationRecord
.find_each { |dossier| DossierRebaseJob.perform_later(dossier) }
end
def reset_draft_revision!
if published_revision.present? && draft_changed?
transaction do
reset!
draft_revision.destroy
update!(draft_revision: create_new_revision(published_revision))
end
end
end
def cnaf_enabled?
api_particulier_sources['cnaf'].present?
end

View file

@ -38,15 +38,15 @@
%span.icon.archive
Clore
- if @procedure.draft_changed?
= link_to 'Publier les modifications', admin_procedure_publication_path(@procedure), class: 'button primary', id: 'publish-procedure-link', data: { disable_with: "Publication..." }
- if @procedure.draft_changed?
.container
.card.featured
.card-title
= t(:has_changes, scope: [:administrateurs, :revision_changes])
= render partial: 'revision_changes', locals: { changes: @procedure.revision_changes }
.flex.mt-2.justify-end
= button_to "Réinitialiser les modifications", admin_procedure_reset_draft_path(@procedure), class: 'button mr-1', method: :put
= link_to 'Publier les modifications', admin_procedure_publication_path(@procedure), class: 'button primary', id: 'publish-procedure-link', data: { disable_with: "Publication..." }
- if !@procedure.procedure_expires_when_termine_enabled?
= render partial: 'administrateurs/procedures/suggest_expires_when_termine', locals: { procedure: @procedure }

View file

@ -438,6 +438,7 @@ Rails.application.routes.draw do
put 'archive'
get 'publication' => 'procedures#publication', as: :publication
put 'publish' => 'procedures#publish', as: :publish
put 'reset_draft' => 'procedures#reset_draft', as: :reset_draft
get 'transfert' => 'procedures#transfert', as: :transfert
get 'close' => 'procedures#close', as: :close
post 'transfer' => 'procedures#transfer', as: :transfer

View file

@ -890,6 +890,37 @@ describe Procedure do
end
end
describe "#reset_draft_revision!" do
let(:procedure) { create(:procedure) }
let(:tdc_attributes) { { type_champ: :number, libelle: 'libelle 1' } }
let(:publication_date) { Time.zone.local(2021, 1, 1, 12, 00, 00) }
context "brouillon procedure" do
it "should not reset draft revision" do
procedure.draft_revision.add_type_de_champ(tdc_attributes)
previous_draft_revision = procedure.draft_revision
procedure.reset_draft_revision!
expect(procedure.draft_revision).to eq(previous_draft_revision)
end
end
context "published procedure" do
let(:procedure) { create(:procedure, :published) }
it "should reset draft revision" do
procedure.draft_revision.add_type_de_champ(tdc_attributes)
previous_draft_revision = procedure.draft_revision
expect(procedure.draft_changed?).to be_truthy
procedure.reset_draft_revision!
expect(procedure.draft_changed?).to be_falsey
expect(procedure.draft_revision).not_to eq(previous_draft_revision)
expect { previous_draft_revision.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
describe "#unpublish!" do
let(:procedure) { create(:procedure, :published) }
let(:now) { Time.zone.now.beginning_of_minute }