From c772c3454cfb365e1fb517aea44e414c2de489c6 Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Wed, 22 Jul 2020 17:11:33 +0200 Subject: [PATCH] update dossier after saving champ update last_champ_updated_at without hook update last_champ_private_updated_at without hook Co-authored-by: Christophe Robillard --- .../instructeurs/dossiers_controller.rb | 6 +- app/controllers/users/dossiers_controller.rb | 6 +- .../instructeurs/dossiers_controller_spec.rb | 61 ++++++++++++++----- .../users/dossiers_controller_spec.rb | 17 ++++++ 4 files changed, 74 insertions(+), 16 deletions(-) diff --git a/app/controllers/instructeurs/dossiers_controller.rb b/app/controllers/instructeurs/dossiers_controller.rb index 4ad0cbe88..f03f6e1ac 100644 --- a/app/controllers/instructeurs/dossiers_controller.rb +++ b/app/controllers/instructeurs/dossiers_controller.rb @@ -192,7 +192,11 @@ module Instructeurs def update_annotations dossier = current_instructeur.dossiers.includes(champs_private: :type_de_champ).find(params[:dossier_id]) - dossier.update(champs_private_params) + dossier.assign_attributes(champs_private_params) + if dossier.champs_private.any?(&:changed?) + dossier.last_champ_private_updated_at = Time.zone.now + end + dossier.save dossier.modifier_annotations!(current_instructeur) redirect_to annotations_privees_instructeur_dossier_path(procedure, dossier) end diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 0fe661950..4d65e3e05 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -345,7 +345,11 @@ module Users errors = [] if champs_params[:dossier] - if !@dossier.update(champs_params[:dossier]) + @dossier.assign_attributes(champs_params[:dossier]) + if @dossier.champs.any?(&:changed?) + @dossier.last_champ_updated_at = Time.zone.now + end + if !@dossier.save errors += @dossier.errors.full_messages elsif change_groupe_instructeur? groupe_instructeur = @dossier.procedure.groupe_instructeurs.find(params[:dossier][:groupe_instructeur_id]) diff --git a/spec/controllers/instructeurs/dossiers_controller_spec.rb b/spec/controllers/instructeurs/dossiers_controller_spec.rb index 5550518b1..2213f501b 100644 --- a/spec/controllers/instructeurs/dossiers_controller_spec.rb +++ b/spec/controllers/instructeurs/dossiers_controller_spec.rb @@ -584,9 +584,26 @@ describe Instructeurs::DossiersController, type: :controller do create(:dossier, :en_construction, procedure: procedure, champs_private: [champ_multiple_drop_down_list, champ_linked_drop_down_list, champ_datetime, champ_repetition]) end + let(:now) { Time.zone.parse('01/01/2100') } + before do - patch :update_annotations, params: { - procedure_id: procedure.id, + Timecop.freeze(now) + patch :update_annotations, params: params + + champ_multiple_drop_down_list.reload + champ_linked_drop_down_list.reload + champ_datetime.reload + champ_repetition.reload + end + + after do + Timecop.return + end + + context "with new values for champs_private" do + let(:params) do + { + procedure_id: procedure.id, dossier_id: dossier.id, dossier: { champs_private_attributes: { @@ -616,20 +633,36 @@ describe Instructeurs::DossiersController, type: :controller do } } } - } - - champ_multiple_drop_down_list.reload - champ_linked_drop_down_list.reload - champ_datetime.reload - champ_repetition.reload + } + end + it { expect(champ_multiple_drop_down_list.value).to eq('["un", "deux"]') } + it { expect(champ_linked_drop_down_list.primary_value).to eq('primary') } + it { expect(champ_linked_drop_down_list.secondary_value).to eq('secondary') } + it { expect(champ_datetime.value).to eq('21/12/2019 13:17') } + it { expect(champ_repetition.champs.first.value).to eq('text') } + it { expect(dossier.reload.last_champ_private_updated_at).to eq(now) } + it { expect(response).to redirect_to(annotations_privees_instructeur_dossier_path(dossier.procedure, dossier)) } end - it { expect(champ_multiple_drop_down_list.value).to eq('["un", "deux"]') } - it { expect(champ_linked_drop_down_list.primary_value).to eq('primary') } - it { expect(champ_linked_drop_down_list.secondary_value).to eq('secondary') } - it { expect(champ_datetime.value).to eq('21/12/2019 13:17') } - it { expect(champ_repetition.champs.first.value).to eq('text') } - it { expect(response).to redirect_to(annotations_privees_instructeur_dossier_path(dossier.procedure, dossier)) } + context "without new values for champs_private" do + let(:params) do + { + procedure_id: procedure.id, + dossier_id: dossier.id, + dossier: { + champs_private_attributes: {}, + champs_attributes: { + '0': { + id: champ_multiple_drop_down_list.id, + value: ['', 'un', 'deux'] + } + } + } + } + end + it { expect(dossier.reload.last_champ_private_updated_at).to eq(nil) } + it { expect(response).to redirect_to(annotations_privees_instructeur_dossier_path(dossier.procedure, dossier)) } + end end describe "#telecharger_pjs" do diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index b7a949643..29d8fbeb7 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -378,6 +378,22 @@ describe Users::DossiersController, type: :controller do expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_construction)) end + context 'without new values for champs' do + let(:submit_payload) do + { + id: dossier.id, + dossier: { + champs_attributes: {} + } + } + end + + it "doesn't set last_champ_updated_at" do + subject + expect(dossier.reload.last_champ_updated_at).to eq(nil) + end + end + context 'with instructeurs ok to be notified instantly' do let!(:instructeur_with_instant_email_dossier) { create(:instructeur) } let!(:instructeur_without_instant_email_dossier) { create(:instructeur) } @@ -576,6 +592,7 @@ describe Users::DossiersController, type: :controller do it 'updates the champs' do subject expect(first_champ.reload.value).to eq('beautiful value') + expect(first_champ.dossier.reload.last_champ_updated_at).to eq(now) expect(piece_justificative_champ.reload.piece_justificative_file).to be_attached end