Skip to content

Commit

Permalink
Send reminders per session date
Browse files Browse the repository at this point in the history
This updates the session reminders job to send a reminder email per
session date that a patient may get vaccinated.
  • Loading branch information
thomasleese committed Oct 1, 2024
1 parent 5b278b1 commit e86dc20
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
38 changes: 19 additions & 19 deletions app/jobs/session_reminders_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@ def perform

date = Date.tomorrow

patient_sessions(date).each do |patient_session|
patient_sessions =
PatientSession
.includes(:consents, :patient)
.joins(:session)
.merge(Session.has_date(date))
.reminder_not_sent(date)

patient_sessions.each do |patient_session|
# We create a record in the database first to avoid sending duplicate emails/texts.
# If a problem occurs while the emails/texts are sent, they will be in the job
# queue and restarted at a later date.

SessionNotification.create!(
patient: patient_session.patient,
session: patient_session.session,
session_date: date
)

patient_session.consents_to_send_communication.each do |consent|
SessionMailer.with(consent:, patient_session:).reminder.deliver_later

TextDeliveryJob.perform_later(
:session_reminder,
consent:,
patient_session:
)
end

patient_session.update!(reminder_sent_at: Time.zone.now)

SessionNotification.create!(
patient: patient_session.patient,
session: patient_session.session,
session_date: date
)
end
end

private

def patient_sessions(date)
PatientSession
.includes(:consents, :patient)
.joins(:session)
.merge(Session.has_date(date))
.reminder_not_sent
end
end
16 changes: 15 additions & 1 deletion app/models/patient_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,21 @@ class PatientSession < ApplicationRecord

has_and_belongs_to_many :immunisation_imports

scope :reminder_not_sent, -> { where(reminder_sent_at: nil) }
scope :reminder_not_sent,
->(session_date) do
where.not(
SessionNotification
.where(
"session_notifications.session_id = patient_sessions.session_id"
)
.where(
"session_notifications.patient_id = patient_sessions.patient_id"
)
.where(session_date:)
.arel
.exists
)
end

def vaccination_record
# HACK: in future, it will be possible to have multiple vaccination records for a patient session
Expand Down
14 changes: 1 addition & 13 deletions spec/jobs/session_reminders_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@
)
end

it "updates the reminder_sent_at attribute for patient sessions" do
expect { perform_now }.to(
change { patient_session.reload.reminder_sent_at }
)
end

it "records a notification" do
expect { perform_now }.to change(
SessionNotification.where(patient:, session:),
Expand All @@ -55,7 +49,7 @@
end

context "when already sent" do
before { patient_session.update!(reminder_sent_at: Time.zone.now) }
before { create(:session_notification, session:, patient:) }

it "doesn't send a reminder email" do
expect { perform_now }.not_to have_enqueued_mail(
Expand All @@ -68,12 +62,6 @@
expect { perform_now }.not_to have_enqueued_text(:session_reminder)
end

it "doesn't change reminder_sent_at" do
expect { perform_now }.not_to(
change { patient_session.reload.reminder_sent_at }
)
end

it "doesn't record a notification" do
expect { perform_now }.not_to change(SessionNotification, :count)
end
Expand Down

0 comments on commit e86dc20

Please sign in to comment.