Skip to content

Commit

Permalink
Track session notifications
Browse files Browse the repository at this point in the history
This updates the session reminders job to track when the notifications
are sent by creating `SessionNotification` instances.
  • Loading branch information
thomasleese committed Oct 1, 2024
1 parent 04c8acc commit 5623dc7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
16 changes: 12 additions & 4 deletions app/jobs/session_reminders_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class SessionRemindersJob < ApplicationJob
def perform
return unless Flipper.enabled?(:scheduled_emails)

patient_sessions.each do |patient_session|
date = Date.tomorrow

patient_sessions(date).each do |patient_session|
patient_session.consents_to_send_communication.each do |consent|
SessionMailer.with(consent:, patient_session:).reminder.deliver_later
TextDeliveryJob.perform_later(
Expand All @@ -17,16 +19,22 @@ def perform
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
def patient_sessions(date)
PatientSession
.includes(:consents)
.includes(:consents, :patient)
.joins(:session)
.merge(Session.has_date(Date.tomorrow))
.merge(Session.has_date(date))
.reminder_not_sent
end
end
28 changes: 23 additions & 5 deletions spec/jobs/session_reminders_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
subject(:perform_now) { described_class.perform_now }

before { Flipper.enable(:scheduled_emails) }
after { Flipper.disable(:scheduled_emails) }

let(:programme) { create(:programme) }
let(:parents) { create_list(:parent, 2, :recorded) }
Expand Down Expand Up @@ -40,6 +41,19 @@
)
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:),
:count
).by(1)
end

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

Expand All @@ -53,12 +67,16 @@
it "doesn't sent a reminder text" do
expect { perform_now }.not_to have_enqueued_text(:session_reminder)
end
end

it "updates the reminder_sent_at attribute for patient sessions" do
expect { perform_now }.to(
change { patient_session.reload.reminder_sent_at }
)
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
end
end

Expand Down

0 comments on commit 5623dc7

Please sign in to comment.