Skip to content

Commit

Permalink
feat(Users::DossiersController#update): sync invalid_transition_rules…
Browse files Browse the repository at this point in the history
… modal when all champs used by transitions_rules are available and changed in a way the dossier is no longer submissible
  • Loading branch information
mfo committed May 13, 2024
1 parent 583e940 commit 3f7dc49
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/controllers/users/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,12 @@ def submit_en_construction
def update
@dossier = dossier.en_construction? ? dossier.find_editing_fork(dossier.user) : dossier
@dossier = dossier_with_champs(pj_template: false)
@transition_rules_was_computable = dossier.revision&.transitions_rules&.computable?(dossier.champs)
@can_passer_en_construction_was = @dossier.can_passer_en_construction?
@errors = update_dossier_and_compute_errors

@transition_rules_is_computable = dossier.revision&.transitions_rules&.computable?(dossier.champs)
@can_passer_en_construction_is = @dossier.can_passer_en_construction?
@transition_rules_computable_changed = !@transition_rules_was_computable && @transition_rules_is_computable
respond_to do |format|
format.turbo_stream do
@to_show, @to_hide, @to_update = champs_to_turbo_update(champs_public_attributes_params, dossier.champs.filter(&:public?))
Expand Down
6 changes: 6 additions & 0 deletions app/views/users/dossiers/update.turbo_stream.haml
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
= render partial: 'shared/dossiers/update_champs', locals: { to_show: @to_show, to_hide: @to_hide, to_update: @to_update, dossier: @dossier }

- if @transition_rules_is_computable
= turbo_stream.remove(dom_id(@dossier, :transition_rules_broken))

- if (@transition_rules_computable_changed && !@can_passer_en_construction_is) || (@can_passer_en_construction_was && !@can_passer_en_construction_is)
= turbo_stream.append('contenu', render(Dossiers::InvalidTransitionsRulesComponent.new(dossier: @dossier)))
61 changes: 60 additions & 1 deletion spec/controllers/users/dossiers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,8 @@
describe '#update brouillon' do
before { sign_in(user) }

let(:procedure) { create(:procedure, :published, types_de_champ_public: [{}, { type: :piece_justificative }]) }
let(:procedure) { create(:procedure, :published, types_de_champ_public:) }
let(:types_de_champ_public) { [{}, { type: :piece_justificative }] }
let(:dossier) { create(:dossier, user:, procedure:) }
let(:first_champ) { dossier.champs_public.first }
let(:piece_justificative_champ) { dossier.champs_public.last }
Expand Down Expand Up @@ -770,6 +771,64 @@
end
end
end

context 'having transitions_rules setup' do
include Logic
render_views

let(:types_de_champ_public) { [{ type: :text }, { type: :integer_number }] }
let(:text_champ) { dossier.champs_public.first }
let(:number_champ) { dossier.champs_public.last }
let(:submit_payload) do
{
id: dossier.id,
dossier: {
groupe_instructeur_id: dossier.groupe_instructeur_id,
champs_public_attributes: {
text_champ.public_id => {
with_public_id: true,
value: "hello world"
},
number_champ.public_id => {
with_public_id: true,
value:
}
}
}
}
end
let(:must_be_greater_than) { 10 }

before do
procedure.published_revision.transitions_rules = greater_than(champ_value(number_champ.stable_id), constant(must_be_greater_than))
procedure.published_revision.save!
end
render_views

context 'when it pass from undefined to true' do
let(:value) { must_be_greater_than / 2 }

it 'raises popup' do
subject
dossier.reload
expect(dossier.can_passer_en_construction?).to be_falsey
expect(assigns(:transition_rules_was_computable)).to eq(false)
expect(assigns(:transition_rules_is_computable)).to eq(true)
expect(response.body).to match(ActionView::RecordIdentifier.dom_id(dossier, :transition_rules_broken))
end
end
context 'when it pass from undefined to false' do
let(:value) { must_be_greater_than * 2 }
it 'does nothing' do
subject
dossier.reload
expect(dossier.can_passer_en_construction?).to be_truthy
expect(assigns(:transition_rules_was_computable)).to eq(false)
expect(assigns(:transition_rules_is_computable)).to eq(true)
expect(response.body).not_to have_selector("##{ActionView::RecordIdentifier.dom_id(dossier, :transition_rules_broken)}")
end
end
end
end

describe '#update en_construction' do
Expand Down

0 comments on commit 3f7dc49

Please sign in to comment.