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:
parent
f8783c9f00
commit
c68fa2d7f3
4 changed files with 43 additions and 5 deletions
10
app/jobs/notify_new_answer_with_delay_job.rb
Normal file
10
app/jobs/notify_new_answer_with_delay_job.rb
Normal 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
|
|
@ -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?
|
||||
|
|
22
spec/jobs/notify_new_answer_with_delay_job_spec.rb
Normal file
22
spec/jobs/notify_new_answer_with_delay_job_spec.rb
Normal 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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue