feat(Commentaire.notify_user): wait 5 minutes before sending email to user when instructor write a commentaire. leaving some space for instructor to destroy his message

This commit is contained in:
Martin 2021-11-15 15:38:57 +01:00
parent f8783c9f00
commit c68fa2d7f3
4 changed files with 43 additions and 5 deletions

View file

@ -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

View file

@ -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?

View file

@ -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

View file

@ -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