From 24d7b0b014756b2295e8151f428f410a663c6499 Mon Sep 17 00:00:00 2001 From: mfo Date: Thu, 20 Jun 2024 15:16:53 +0200 Subject: [PATCH] fix(ineligibilite_rules): prevent admin to enable ineligibilite rules without rules --- .../ineligibilite_rules_component.html.haml | 2 +- .../ineligibilite_rules_controller.rb | 4 ++- app/models/dossier.rb | 2 +- app/models/procedure_revision.rb | 4 +++ .../locales/models/procedure_revision/fr.yml | 1 + .../ineligibilite_rules_controller_spec.rb | 29 +++++++++++++++---- 6 files changed, 33 insertions(+), 9 deletions(-) diff --git a/app/components/conditions/ineligibilite_rules_component/ineligibilite_rules_component.html.haml b/app/components/conditions/ineligibilite_rules_component/ineligibilite_rules_component.html.haml index 313618167..7cbbca2fa 100644 --- a/app/components/conditions/ineligibilite_rules_component/ineligibilite_rules_component.html.haml +++ b/app/components/conditions/ineligibilite_rules_component/ineligibilite_rules_component.html.haml @@ -2,7 +2,7 @@ = render Procedure::PendingRepublishComponent.new(procedure: @draft_revision.procedure, render_if: pending_changes?) = render Conditions::ConditionsErrorsComponent.new(conditions: condition_per_row, source_tdcs: @source_tdcs) .fr-fieldset - = form_for(@draft_revision, url: change_admin_procedure_ineligibilite_rules_path(@draft_revision.procedure_id), html: { id: 'ineligibilite_form', class: 'width-100' }) do |f| + = form_for(@draft_revision, url: change_admin_procedure_ineligibilite_rules_path(@draft_revision.procedure_id), html: { id: 'ineligibilite_form', class: 'width-100', novalidate: true }) do |f| .fr-fieldset__element .fr-toggle.fr-toggle--label-left = f.check_box :ineligibilite_enabled, class: 'fr-toggle__input', data: @opt diff --git a/app/controllers/administrateurs/ineligibilite_rules_controller.rb b/app/controllers/administrateurs/ineligibilite_rules_controller.rb index 41d6865f9..f7519785f 100644 --- a/app/controllers/administrateurs/ineligibilite_rules_controller.rb +++ b/app/controllers/administrateurs/ineligibilite_rules_controller.rb @@ -6,7 +6,9 @@ module Administrateurs end def change - if draft_revision.update(procedure_revision_params) + draft_revision.assign_attributes(procedure_revision_params) + + if draft_revision.validate(:ineligibilite_rules_editor) && draft_revision.save redirect_to edit_admin_procedure_ineligibilite_rules_path(@procedure) else flash[:alert] = draft_revision.errors.full_messages diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 343baf06d..7be94d766 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -563,7 +563,7 @@ class Dossier < ApplicationRecord end def can_passer_en_construction? - return true if !revision.ineligibilite_enabled + return true if !revision.ineligibilite_enabled || !revision.ineligibilite_rules !revision.ineligibilite_rules.compute(champs_for_revision(scope: :public)) end diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 0a27fec2c..709ae8472 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -28,6 +28,10 @@ class ProcedureRevision < ApplicationRecord presence: true, if: -> { ineligibilite_enabled? }, on: [:ineligibilite_rules_editor, :publication] + validates :ineligibilite_rules, + presence: true, + if: -> { ineligibilite_enabled? }, + on: [:ineligibilite_rules_editor, :publication] serialize :ineligibilite_rules, LogicSerializer diff --git a/config/locales/models/procedure_revision/fr.yml b/config/locales/models/procedure_revision/fr.yml index 1665415aa..bbeae2422 100644 --- a/config/locales/models/procedure_revision/fr.yml +++ b/config/locales/models/procedure_revision/fr.yml @@ -3,5 +3,6 @@ fr: attributes: procedure_revision: ineligibilite_message: Message d’inéligibilité + ineligibilite_rules: Les conditions d’inéligibilité hints: ineligibilite_message: "Ce message sera affiché à l’usager si son dossier est bloqué et lui expliquera la raison de son inéligibilité." diff --git a/spec/controllers/administrateurs/ineligibilite_rules_controller_spec.rb b/spec/controllers/administrateurs/ineligibilite_rules_controller_spec.rb index 2a76a054a..2ab289772 100644 --- a/spec/controllers/administrateurs/ineligibilite_rules_controller_spec.rb +++ b/spec/controllers/administrateurs/ineligibilite_rules_controller_spec.rb @@ -220,12 +220,29 @@ describe Administrateurs::IneligibiliteRulesController, type: :controller do } end before { sign_in(admin.user) } - it 'works' do - patch :change, params: params - draft_revision = procedure.reload.draft_revision - expect(draft_revision.ineligibilite_message).to eq('panpan') - expect(draft_revision.ineligibilite_enabled).to eq(true) - expect(response).to redirect_to(edit_admin_procedure_ineligibilite_rules_path(procedure)) + + context 'when ineligibilite rules is empty' do + it 'fails gracefull without ineligibilite rules' do + patch :change, params: params + draft_revision = procedure.reload.draft_revision + expect(draft_revision.ineligibilite_enabled).to eq(false) + expect(flash[:alert]).to include("Le champ « Les conditions d’inéligibilité » doit être rempli") + end + end + + context 'when ineligibilite rules is present' do + let(:types_de_champ_public) { [{ type: :drop_down_list, stable_id: 1, options: ['opt'] }] } + before do + procedure.draft_revision.update(ineligibilite_rules: ds_eq(champ_value(1), constant('opt'))) + end + + it 'works' do + patch :change, params: params + draft_revision = procedure.reload.draft_revision + expect(draft_revision.ineligibilite_message).to eq('panpan') + expect(draft_revision.ineligibilite_enabled).to eq(true) + expect(response).to redirect_to(edit_admin_procedure_ineligibilite_rules_path(procedure)) + end end end end