diff --git a/app/jobs/notify_new_answer_with_delay_job.rb b/app/jobs/notify_new_answer_with_delay_job.rb new file mode 100644 index 000000000..fc070043b --- /dev/null +++ b/app/jobs/notify_new_answer_with_delay_job.rb @@ -0,0 +1,10 @@ +class NotifyNewAnswerWithDelayJob < ApplicationJob + queue_as :exports # for now? + + discard_on ActiveRecord::RecordNotFound + + def perform(dossier, body, commentaire) + return if commentaire.soft_deleted? + DossierMailer.notify_new_answer(dossier, body) + end +end diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 3fa35fa85..7fdefbd45 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -101,13 +101,19 @@ class Commentaire < ApplicationRecord # - If a user or an invited user posted a commentaire, do nothing, # the notification system will properly # - Otherwise, a instructeur posted a commentaire, we need to notify the user - if sent_by_instructeur? || sent_by_expert? + if sent_by_instructeur? + notify_user_with_delay + elsif sent_by_expert? notify_user end end - def notify_user - DossierMailer.notify_new_answer(dossier, body).deliver_later(wait: 10.minutes) + def notify_user_with_delay + NotifyNewAnswerWithDelayJob.set(wait: 5.minutes).perform_later(dossier, body, self) + end + + def notify_user_with() + DossierMailer.notify_with_delay_new_commentaire_to_user(dossier, body, self).deliver_later(job_options) end def messagerie_available? diff --git a/spec/jobs/notify_new_answer_with_delay_job_spec.rb b/spec/jobs/notify_new_answer_with_delay_job_spec.rb new file mode 100644 index 000000000..4a7061d59 --- /dev/null +++ b/spec/jobs/notify_new_answer_with_delay_job_spec.rb @@ -0,0 +1,22 @@ +describe NotifyNewAnswerWithDelayJob, type: :job do + let(:dossier) { double } + let(:body) { "bim un body" } + + context 'when commentaire not soft_deleted?' do + let(:commentaire) { create(:commentaire) } + + it 'call DossierMailer.notify_new_answer' do + expect(DossierMailer).to receive(:notify_new_answer).with(dossier, body) + NotifyNewAnswerWithDelayJob.perform_now(dossier, body, commentaire) + end + end + + context 'when commentaire is soft_deleted?' do + let(:commentaire) { create(:commentaire, deleted_at: 2.hours.ago) } + + it 'skips DossierMailer.notify_new_anser' do + expect(DossierMailer).not_to receive(:notify_new_answer).with(dossier, body) + NotifyNewAnswerWithDelayJob.perform_now(dossier, body, commentaire) + end + end +end diff --git a/spec/models/commentaire_spec.rb b/spec/models/commentaire_spec.rb index 978f5ec30..0b4be7fcf 100644 --- a/spec/models/commentaire_spec.rb +++ b/spec/models/commentaire_spec.rb @@ -86,8 +86,8 @@ describe Commentaire do context "with a commentaire created by a instructeur" do let(:commentaire) { CommentaireService.build(instructeur, dossier, body: "Mon commentaire") } - it "calls notify_user" do - expect(commentaire).to receive(:notify_user) + it "calls notify_user with delay so instructeur can destroy his comment in case of failure" do + expect(commentaire).to receive(:notify_user_with_delay) commentaire.save end end