From f3a675c3bcbfbe447277bb0356cccaea4231e00c Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Wed, 22 Jul 2020 16:34:33 +0200 Subject: [PATCH] update dossier after saving commentaire update last_commentaire_updated_at without hook Co-authored-by: Christophe Robillard --- app/controllers/instructeurs/avis_controller.rb | 1 + app/controllers/instructeurs/dossiers_controller.rb | 1 + app/controllers/users/dossiers_controller.rb | 1 + spec/controllers/instructeurs/avis_controller_spec.rb | 5 +++++ spec/controllers/instructeurs/dossiers_controller_spec.rb | 5 +++++ spec/controllers/users/dossiers_controller_spec.rb | 5 +++++ 6 files changed, 18 insertions(+) diff --git a/app/controllers/instructeurs/avis_controller.rb b/app/controllers/instructeurs/avis_controller.rb index 933aa64a7..ba8997448 100644 --- a/app/controllers/instructeurs/avis_controller.rb +++ b/app/controllers/instructeurs/avis_controller.rb @@ -60,6 +60,7 @@ module Instructeurs @commentaire = CommentaireService.build(current_instructeur, avis.dossier, commentaire_params) if @commentaire.save + @commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now) flash.notice = "Message envoyé" redirect_to messagerie_instructeur_avis_path(avis.procedure, avis) else diff --git a/app/controllers/instructeurs/dossiers_controller.rb b/app/controllers/instructeurs/dossiers_controller.rb index 85b21678d..4ad0cbe88 100644 --- a/app/controllers/instructeurs/dossiers_controller.rb +++ b/app/controllers/instructeurs/dossiers_controller.rb @@ -169,6 +169,7 @@ module Instructeurs @commentaire = CommentaireService.build(current_instructeur, dossier, commentaire_params) if @commentaire.save + @commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now) current_instructeur.follow(dossier) flash.notice = "Message envoyé" redirect_to messagerie_instructeur_dossier_path(procedure, dossier) diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 93a62ad67..0fe661950 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -198,6 +198,7 @@ module Users @commentaire = CommentaireService.build(current_user, dossier, commentaire_params) if @commentaire.save + @commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now) dossier.followers_instructeurs .with_instant_email_message_notifications .each do |instructeur| diff --git a/spec/controllers/instructeurs/avis_controller_spec.rb b/spec/controllers/instructeurs/avis_controller_spec.rb index 1d805df7d..b220573cc 100644 --- a/spec/controllers/instructeurs/avis_controller_spec.rb +++ b/spec/controllers/instructeurs/avis_controller_spec.rb @@ -117,18 +117,23 @@ describe Instructeurs::AvisController, type: :controller do describe '#create_commentaire' do let(:file) { nil } let(:scan_result) { true } + let(:now) { Time.zone.parse("14/07/1789") } subject { post :create_commentaire, params: { id: avis_without_answer.id, procedure_id: procedure.id, commentaire: { body: 'commentaire body', piece_jointe: file } } } before do allow(ClamavService).to receive(:safe_file?).and_return(scan_result) + Timecop.freeze(now) end + after { Timecop.return } + it do subject expect(response).to redirect_to(messagerie_instructeur_avis_path(avis_without_answer.procedure, avis_without_answer)) expect(dossier.commentaires.map(&:body)).to match(['commentaire body']) + expect(dossier.reload.last_commentaire_updated_at).to eq(now) end context "with a file" do diff --git a/spec/controllers/instructeurs/dossiers_controller_spec.rb b/spec/controllers/instructeurs/dossiers_controller_spec.rb index ec2d68386..28e8e4798 100644 --- a/spec/controllers/instructeurs/dossiers_controller_spec.rb +++ b/spec/controllers/instructeurs/dossiers_controller_spec.rb @@ -390,6 +390,7 @@ describe Instructeurs::DossiersController, type: :controller do let(:body) { "avant\napres" } let(:file) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') } let(:scan_result) { true } + let(:now) { Timecop.freeze("09/11/1989") } subject { post :create_commentaire, params: { @@ -404,14 +405,18 @@ describe Instructeurs::DossiersController, type: :controller do before do allow(ClamavService).to receive(:safe_file?).and_return(scan_result) + Timecop.freeze(now) end + after { Timecop.return } + it "creates a commentaire" do expect { subject }.to change(Commentaire, :count).by(1) expect(instructeur.followed_dossiers).to include(dossier) expect(response).to redirect_to(messagerie_instructeur_dossier_path(dossier.procedure, dossier)) expect(flash.notice).to be_present + expect(dossier.reload.last_commentaire_updated_at).to eq(now) end context "when the commentaire created with virus file" do diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 9dd0cdfd5..b7a949643 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -802,6 +802,7 @@ describe Users::DossiersController, type: :controller do let(:body) { "avant\napres" } let(:file) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') } let(:scan_result) { true } + let(:now) { Time.zone.parse("18/09/1981") } subject { post :create_commentaire, params: { @@ -814,6 +815,7 @@ describe Users::DossiersController, type: :controller do } before do + Timecop.freeze(now) sign_in(user) allow(ClamavService).to receive(:safe_file?).and_return(scan_result) allow(DossierMailer).to receive(:notify_new_commentaire_to_instructeur).and_return(double(deliver_later: nil)) @@ -823,6 +825,8 @@ describe Users::DossiersController, type: :controller do create(:assign_to, instructeur: instructeur_without_instant_message, procedure: procedure, instant_email_message_notifications_enabled: false, groupe_instructeur: procedure.defaut_groupe_instructeur) end + after { Timecop.return } + it "creates a commentaire" do expect { subject }.to change(Commentaire, :count).by(1) @@ -830,6 +834,7 @@ describe Users::DossiersController, type: :controller do expect(DossierMailer).to have_received(:notify_new_commentaire_to_instructeur).with(dossier, instructeur_with_instant_message.email) expect(DossierMailer).not_to have_received(:notify_new_commentaire_to_instructeur).with(dossier, instructeur_without_instant_message.email) expect(flash.notice).to be_present + expect(dossier.reload.last_commentaire_updated_at).to eq(now) end end