Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[File d'attente] Envoi de l'alerte #382

Merged
18 commits merged into from
Feb 6, 2020
5 changes: 2 additions & 3 deletions app/jobs/file_attente_job.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class FileAttenteJob < ApplicationJob
def perform(user, rdv)
TwilioSenderJob.perform_later(:file_attente, rdv, user) if user.formated_phone
FileAttenteMailer.send_notification(rdv, user) if user.email
def perform
FileAttente.send_notifications
end
end
3 changes: 2 additions & 1 deletion app/models/file_attente.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def self.send_notifications
next unless !creneaux.empty? && fa.notifications_sent < 10 && creneaux.first.starts_at < end_time

fa.rdv.users.map(&:user_to_notify).uniq.each do |user|
FileAttenteJob.perform_later(user, fa.rdv)
TwilioSenderJob.perform_later(:file_attente, fa.rdv, user) if user.formated_phone
FileAttenteMailer.send_notification(fa.rdv, user).deliver_later if user.email
fa.update!(last_creneau_sent_starts_at: creneaux.first.starts_at)
fa.increment!(:notifications_sent)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/tasks/file_attente.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
task send_file_attente: :environment do
# Every 10 minutes, from 9am to 6pm, from monday to friday
FileAttenteJob.set(cron: "0,10,20,30,40,50 9,10,11,12,13,14,15,16,17,18 ? * MON,TUE,WED,THU,FRI *").perform_later
jjf21 marked this conversation as resolved.
Show resolved Hide resolved
end
7 changes: 3 additions & 4 deletions spec/jobs/file_attente_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
freeze_time
end

after { FileAttenteJob.perform_now(rdv.users.first, rdv) }
after { FileAttenteJob.perform_now }

it 'should send an sms + rdv to rdv' do
expect(FileAttenteMailer).to receive(:send_notification).with(rdv, rdv.users.first).and_return(double(deliver_later: nil))
expect(TwilioTextMessenger).to receive(:new).with(:file_attente, rdv, rdv.users.first).and_call_original
it 'should call send_notifications' do
expect(FileAttente).to receive(:send_notifications)
end
end
end
10 changes: 7 additions & 3 deletions spec/models/file_attente_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,21 @@
file_attente.reload
end.to change(file_attente, :last_creneau_sent_starts_at).from(nil)
end
it 'should increment notifications_sent' do
it 'should send an sms' do
ActiveJob::Base.queue_adapter = :test
expect{subject}.to have_enqueued_job(FileAttenteJob)
expect{subject}.to have_enqueued_job(TwilioSenderJob)
end

it 'should send an email' do
expect(FileAttenteMailer).to receive(:send_notification).with(rdv, rdv.users.first).and_return(double(deliver_later: nil))
subject
end
end

context "without availabilities before rdv" do
let!(:plage_ouverture_2) { create(:plage_ouverture, first_day: now - 1.day) }

it 'should not send notification' do
# debugger
ActiveJob::Base.queue_adapter = :test
expect{subject}.not_to have_enqueued_job(FileAttenteJob)
end
Expand Down