diff --git a/app/models/procedure.rb b/app/models/procedure.rb index e1d9b0176..ad992d2c6 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -226,7 +226,7 @@ class Procedure < ApplicationRecord enum closing_reason: { internal_procedure: 'internal_procedure', other: 'other' - } + }, _prefix: true scope :for_api_v2, -> { includes(:draft_revision, :published_revision, administrateurs: :user) @@ -265,8 +265,7 @@ class Procedure < ApplicationRecord validate :check_juridique, on: [:create, :publication] - # TO DO add validation after data backfill - # validates :replaced_by_id, presence: true, if: -> { closing_reason == self.closing_reasons.fetch(:internal_procedure) } + validates :replaced_by_procedure_id, presence: true, if: :closing_reason_internal_procedure? validates :path, presence: true, format: { with: /\A[a-z0-9_\-]{3,200}\z/ }, uniqueness: { scope: [:path, :closed_at, :hidden_at, :unpublished_at], case_sensitive: false } validates :duree_conservation_dossiers_dans_ds, allow_nil: false, diff --git a/app/tasks/maintenance/update_closing_reason_if_no_replaced_by_id_task.rb b/app/tasks/maintenance/update_closing_reason_if_no_replaced_by_id_task.rb new file mode 100644 index 000000000..daf013ca0 --- /dev/null +++ b/app/tasks/maintenance/update_closing_reason_if_no_replaced_by_id_task.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Maintenance + class UpdateClosingReasonIfNoReplacedByIdTask < MaintenanceTasks::Task + def collection + Procedure + .with_discarded + .closes + .where(closing_reason: Procedure.closing_reasons.fetch(:internal_procedure)) + .where(replaced_by_procedure_id: nil) + end + + def process(procedure) + procedure.closing_reason_other! + end + end +end diff --git a/app/views/administrateurs/procedures/closing_notification.html.haml b/app/views/administrateurs/procedures/closing_notification.html.haml index 36c9f0e77..acd58b10e 100644 --- a/app/views/administrateurs/procedures/closing_notification.html.haml +++ b/app/views/administrateurs/procedures/closing_notification.html.haml @@ -7,7 +7,7 @@ .fr-grid-row .fr-col-12.fr-col-offset-md-2.fr-col-md-8 %h1= t('administrateurs.procedures.closing_notification.page_title') - - if @procedure.closing_reason == Procedure.closing_reasons.fetch(:other) + - if @procedure.closing_reason_other? %h2.fr-h5= I18n.t('administrateurs.procedures.closing_notification.page_subtitle', closing_path: closing_details_path(@procedure.path)).html_safe - else %h2.fr-h5= I18n.t('administrateurs.procedures.closing_notification.page_subtitle_with_redirection', redirection_path: commencer_path(@procedure.replaced_by_procedure.path)).html_safe diff --git a/app/views/administrateurs/procedures/show.html.haml b/app/views/administrateurs/procedures/show.html.haml index 4856331ab..446f37d1c 100644 --- a/app/views/administrateurs/procedures/show.html.haml +++ b/app/views/administrateurs/procedures/show.html.haml @@ -58,7 +58,7 @@ - new_procedure = Procedure.find_by(id: @procedure.replaced_by_procedure_id) %p = "Cette démarche est remplacée par une autre démarche dans Démarches simplifiées :" - = link_to(new_procedure&.libelle, admin_procedure_path(new_procedure)) + = link_to(new_procedure.libelle, admin_procedure_path(new_procedure)) - if @procedure.closing_reason == 'other' %p = "Plus d'informations dans la #{link_to('page de fermeture', closing_details_path(@procedure.path))}, visible par les usagers." diff --git a/app/views/users/dossiers/_dossiers_list.html.haml b/app/views/users/dossiers/_dossiers_list.html.haml index ea6e62933..432dc5239 100644 --- a/app/views/users/dossiers/_dossiers_list.html.haml +++ b/app/views/users/dossiers/_dossiers_list.html.haml @@ -54,13 +54,13 @@ = render Dsfr::AlertComponent.new(state: :info, size: :sm, extra_class_names: "fr-mb-2w") do |c| - c.with_body do %p - - if dossier.brouillon? && dossier.procedure.closing_reason == Procedure.closing_reasons.fetch(:internal_procedure) + - if dossier.brouillon? && dossier.procedure.closing_reason_internal_procedure? = I18n.t('views.users.dossiers.dossiers_list.procedure_closed.brouillon.internal_procedure', link: commencer_path(dossier.procedure.replaced_by_procedure.path)).html_safe - - elsif dossier.brouillon? && dossier.procedure.closing_reason == Procedure.closing_reasons.fetch(:other) + - elsif dossier.brouillon? && dossier.procedure.closing_reason_other? = I18n.t('views.users.dossiers.dossiers_list.procedure_closed.brouillon.other', link: closing_details_path(dossier.procedure.path)).html_safe - - elsif (dossier.en_construction? || dossier.en_instruction?) && dossier.procedure.closing_reason == Procedure.closing_reasons.fetch(:internal_procedure) + - elsif (dossier.en_construction? || dossier.en_instruction?) && dossier.procedure.closing_reason_internal_procedure? = I18n.t('views.users.dossiers.dossiers_list.procedure_closed.en_cours.internal_procedure') - - elsif (dossier.en_construction? || dossier.en_instruction?) && dossier.procedure.closing_reason == Procedure.closing_reasons.fetch(:other) + - elsif (dossier.en_construction? || dossier.en_instruction?) && dossier.procedure.closing_reason_other? = I18n.t('views.users.dossiers.dossiers_list.procedure_closed.en_cours.other', link: closing_details_path(dossier.procedure.path)).html_safe - if dossier.pending_correction? diff --git a/spec/controllers/administrateurs/procedures_controller_spec.rb b/spec/controllers/administrateurs/procedures_controller_spec.rb index 8c3f651e3..72d2484e0 100644 --- a/spec/controllers/administrateurs/procedures_controller_spec.rb +++ b/spec/controllers/administrateurs/procedures_controller_spec.rb @@ -803,9 +803,10 @@ describe Administrateurs::ProceduresController, type: :controller do procedure.reload end - it 'closes the procedure without redirection to the new procedure in DS' do - expect(response).to redirect_to admin_procedure_path(procedure.id) - expect(flash[:notice]).to have_content 'Démarche close' + it 'does not close the procedure' do + expect(response).to redirect_to admin_procedure_close_path + expect(flash[:alert]).to have_content 'Le champ « Nouvelle démarche » doit être rempli' + expect(procedure.close?).to be_falsey expect(procedure.replaced_by_procedure).to eq(nil) end end diff --git a/spec/tasks/maintenance/update_closing_reason_if_no_replaced_by_id_task_spec.rb b/spec/tasks/maintenance/update_closing_reason_if_no_replaced_by_id_task_spec.rb new file mode 100644 index 000000000..a535fdecd --- /dev/null +++ b/spec/tasks/maintenance/update_closing_reason_if_no_replaced_by_id_task_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Maintenance + RSpec.describe UpdateClosingReasonIfNoReplacedByIdTask do + describe "#process" do + subject(:process) { described_class.process(procedure) } + + let(:procedure) { create(:procedure, :closed) } + + before do + procedure.update_column(:closing_reason, Procedure.closing_reasons.fetch(:internal_procedure)) + procedure.update_column(:replaced_by_procedure_id, nil) + end + + it 'updates closing_reason to other' do + subject + expect(procedure.closing_reason).to eq(Procedure.closing_reasons.fetch(:other)) + end + end + end +end