From eb7f1f199a8d3563f6608038494d6a9065754484 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Fri, 2 Jun 2023 18:34:46 +0200 Subject: [PATCH] feat(sva): block configuration changes when procedure is published --- .../procedure/sva_svr_form_component.rb | 6 +++ .../sva_svr_form_component.en.yml | 1 + .../sva_svr_form_component.fr.yml | 1 + .../sva_svr_form_component.html.haml | 16 +++----- .../concerns/procedure_sva_svr_concern.rb | 13 ++++++ config/locales/models/procedure/en.yml | 2 + config/locales/models/procedure/fr.yml | 2 + spec/models/procedure_spec.rb | 40 +++++++++++++++++++ 8 files changed, 71 insertions(+), 10 deletions(-) diff --git a/app/components/procedure/sva_svr_form_component.rb b/app/components/procedure/sva_svr_form_component.rb index 8f9d119fb..ee39138fa 100644 --- a/app/components/procedure/sva_svr_form_component.rb +++ b/app/components/procedure/sva_svr_form_component.rb @@ -8,6 +8,12 @@ class Procedure::SVASVRFormComponent < ApplicationComponent @configuration = configuration end + def form_disabled? + return false if procedure.brouillon? + + procedure.sva_svr_enabled? + end + def decision_buttons scope = ".decision_buttons" diff --git a/app/components/procedure/sva_svr_form_component/sva_svr_form_component.en.yml b/app/components/procedure/sva_svr_form_component/sva_svr_form_component.en.yml index 2943a8cf8..8aaf15440 100644 --- a/app/components/procedure/sva_svr_form_component/sva_svr_form_component.en.yml +++ b/app/components/procedure/sva_svr_form_component/sva_svr_form_component.en.yml @@ -24,3 +24,4 @@ en: reset_label: "Reset the delay" reset_hint: "Example: if the file is resubmitted on April 15 and the delay is 2 months, the decision will be automatically made on June 15, unless the instructor pronounces in the meantime or asks for corrections again" notice_new_files_only: "Information: if you activate this rule, only the newly submitted files will be subject to it." + notice_edit_denied: "Warning: SVA/SVR cannot be changed or disabled." diff --git a/app/components/procedure/sva_svr_form_component/sva_svr_form_component.fr.yml b/app/components/procedure/sva_svr_form_component/sva_svr_form_component.fr.yml index 9e72aa009..a66afb45d 100644 --- a/app/components/procedure/sva_svr_form_component/sva_svr_form_component.fr.yml +++ b/app/components/procedure/sva_svr_form_component/sva_svr_form_component.fr.yml @@ -24,3 +24,4 @@ fr: reset_label: "Réinitialiser le délai" reset_hint: "Exemple: si le dossier est re-déposé le 15 avril et que le délai est de 2 mois, la décision sera automatiquement prise le 15 juin, sauf à ce que l’instructeur se prononce d’ici là ou demande à nouveau des corrections" notice_new_files_only: "Information : si vous activez cette règle, seuls les nouveaux dossiers déposés y seront soumis." + notice_edit_denied: "Avertissement : le changement ou la désactivation du SVA/SVR est impossible." diff --git a/app/components/procedure/sva_svr_form_component/sva_svr_form_component.html.haml b/app/components/procedure/sva_svr_form_component/sva_svr_form_component.html.haml index 401b5f960..fbd8287ba 100644 --- a/app/components/procedure/sva_svr_form_component/sva_svr_form_component.html.haml +++ b/app/components/procedure/sva_svr_form_component/sva_svr_form_component.html.haml @@ -1,11 +1,11 @@ = form_for [procedure, configuration], url: admin_procedure_sva_svr_path(procedure), method: :put do |f| - - if procedure.publiee? + - if procedure.publiee? && !procedure.sva_svr_enabled? .fr-alert.fr-alert--info.fr-alert--sm.fr-mb-4w - - if procedure.sva_svr_enabled? - %p Information : si vous désactivez cette règle, seuls les nouveaux dossiers seront impactés. Ceux déjà déjà déposés la conserveront. - - else - %p= t('.notice_new_files_only') + %p= t('.notice_new_files_only') + - if procedure.publiee? && procedure.sva_svr_enabled? + .fr-alert.fr-alert--warning.fr-alert--sm.fr-mb-4w + %p= t('.notice_edit_denied') %fieldset.fr-fieldset %legend.fr-fieldset__legend= t(".rule") @@ -27,11 +27,7 @@ %span.fr-hint-text = t(".resume_intro") - - if procedure.publiee? && procedure.sva_svr_enabled? - .fr-alert.fr-alert--warning.fr-alert--sm - %p Attention : un changement de ce réglage affectera les éventuels dossiers en cours. - = render Dsfr::RadioButtonListComponent.new(form: f, target: :resume, buttons: resume_buttons) - = f.submit t(".submit"), class: "fr-btn" + = f.submit t(".submit"), class: "fr-btn", disabled: form_disabled? = link_to t(".cancel"), admin_procedure_path(procedure.id), class: "fr-btn fr-btn--secondary fr-ml-2w" diff --git a/app/models/concerns/procedure_sva_svr_concern.rb b/app/models/concerns/procedure_sva_svr_concern.rb index 56851c36d..d14eaa5d6 100644 --- a/app/models/concerns/procedure_sva_svr_concern.rb +++ b/app/models/concerns/procedure_sva_svr_concern.rb @@ -2,6 +2,8 @@ module ProcedureSVASVRConcern extend ActiveSupport::Concern included do + validate :sva_svr_immutable_on_published, if: :will_save_change_to_sva_svr? + def sva_svr_enabled? sva? || svr? end @@ -23,5 +25,16 @@ module ProcedureSVASVRConcern def decision sva_svr.fetch("decision", nil)&.to_sym end + + def decision_was + sva_svr_was.fetch("decision", nil)&.to_sym + end + + def sva_svr_immutable_on_published + return if brouillon? + return if [:sva, :svr].exclude?(decision_was) + + errors.add(:sva_svr, :immutable) + end end end diff --git a/config/locales/models/procedure/en.yml b/config/locales/models/procedure/en.yml index 6437d9905..72d0426ec 100644 --- a/config/locales/models/procedure/en.yml +++ b/config/locales/models/procedure/en.yml @@ -57,3 +57,5 @@ en: format: 'Private field %{message}' lien_dpo: invalid_uri_or_email: "Fill in with an email or a link" + sva_svr: + immutable: "SVA/SVR configuration can no longer be modified" diff --git a/config/locales/models/procedure/fr.yml b/config/locales/models/procedure/fr.yml index 0de138530..953cc0fd4 100644 --- a/config/locales/models/procedure/fr.yml +++ b/config/locales/models/procedure/fr.yml @@ -65,3 +65,5 @@ fr: invalid_uri_or_email: "Veuillez saisir un mail ou un lien" auto_archive_on: future: doit être dans le futur + sva_svr: + immutable: "La configuration SVA/SVR ne peut plus être modifiée" diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 1fee39972..5a28c6cd7 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -401,6 +401,46 @@ describe Procedure do end end end + + context 'with sva svr' do + before { + procedure.sva_svr["decision"] = "svr" + } + + context 'when procedure is published with sva' do + let(:procedure) { create(:procedure, :published, :sva) } + + it 'prevents changes to sva_svr' do + expect(procedure).not_to be_valid + expect(procedure.errors[:sva_svr].join).to include('ne peut plus être modifiée') + end + end + + context 'when procedure is published without sva' do + let(:procedure) { create(:procedure, :published) } + + it 'allow activation' do + expect(procedure).to be_valid + end + + it 'allow activation from disabled value' do + procedure.sva_svr["decision"] = "disabled" + procedure.save! + + procedure.sva_svr["decision"] = "svr" + + expect(procedure).to be_valid + end + end + + context 'brouillon procedure' do + let(:procedure) { create(:procedure, :sva) } + + it "can update sva config" do + expect(procedure).to be_valid + end + end + end end describe 'opendata' do