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

Refactor sending consent notifications #1891

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions app/jobs/consent_reminders_job.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# frozen_string_literal: true

# This job triggers a job to send a batch of consent reminders for each sessions
# that needs them sent today.

class ConsentRemindersJob < ApplicationJob
queue_as :default

def perform(*_args)
def perform
return unless Flipper.enabled?(:scheduled_emails)

Session.send_consent_reminders_today.each do |session|
ConsentRemindersSessionBatchJob.perform_later(session)
session.patients.needing_consent_reminder.each do |patient|
session.programmes.each do |programme|
ConsentNotification.create_and_send!(
patient:,
programme:,
session:,
reminder: true
)
end
end
end
end
end
35 changes: 0 additions & 35 deletions app/jobs/consent_reminders_session_batch_job.rb

This file was deleted.

16 changes: 11 additions & 5 deletions app/jobs/consent_requests_job.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# frozen_string_literal: true

# This job triggers a job to send a batch of consent requests for each sessions
# that needs them sent today.

class ConsentRequestsJob < ApplicationJob
queue_as :default

def perform(*_args)
def perform
return unless Flipper.enabled?(:scheduled_emails)

Session.send_consent_requests_today.each do |session|
ConsentRequestsSessionBatchJob.perform_later(session)
session.patients.needing_consent_request.each do |patient|
session.programmes.each do |programme|
ConsentNotification.create_and_send!(
patient:,
programme:,
session:,
reminder: false
)
end
end
end
end
end
35 changes: 0 additions & 35 deletions app/jobs/consent_requests_session_batch_job.rb

This file was deleted.

2 changes: 2 additions & 0 deletions app/jobs/text_delivery_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def perform(
parent: nil,
patient: nil,
patient_session: nil,
programme: nil,
session: nil,
vaccination_record: nil
)
Expand All @@ -33,6 +34,7 @@ def perform(
parent:,
patient:,
patient_session:,
programme:,
vaccination_record:
)

Expand Down
29 changes: 29 additions & 0 deletions app/models/consent_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,33 @@
class ConsentNotification < ApplicationRecord
belongs_to :patient
belongs_to :programme

def self.create_and_send!(patient:, programme:, session:, reminder:)
# 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.

ConsentNotification.create!(programme:, patient:, reminder:)

patient.parents.each do |parent|
ConsentMailer
.with(parent:, patient:, programme:, session:)
.send(reminder ? :reminder : :request)
.deliver_later

TextDeliveryJob.perform_later(
reminder ? :consent_reminder : :consent_request,
parent:,
patient:,
programme:,
session:
)
end

if reminder
patient.update!(consent_reminder_sent_at: Time.zone.now)
else
patient.update!(consent_request_sent_at: Time.zone.now)
end
end
end
87 changes: 62 additions & 25 deletions spec/jobs/consent_reminders_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,76 @@
# frozen_string_literal: true

describe ConsentRemindersJob, type: :job do
before do
Flipper.enable(:scheduled_emails)
ActiveJob::Base.queue_adapter.enqueued_jobs.clear
end
describe ConsentRemindersJob do
subject(:perform_now) { described_class.perform_now }

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

let(:programme) { create(:programme) }

context "with draft and active sessions" do
it "enqueues ConsentRemindersSessionBatchJob for each active sessions" do
active_session =
create(:session, programme:, send_consent_reminders_at: Time.zone.today)
_unscheduled_session = create(:session, :unscheduled, programme:)
let(:parents) { create_list(:parent, 2) }

let(:patient_with_reminder_sent) do
create(:patient, :consent_request_sent, :consent_reminder_sent, programme:)
end
let(:patient_not_sent_reminder) do
create(:patient, :consent_request_sent, parents:, programme:)
end
let(:patient_with_consent) do
create(:patient, :consent_given_triage_not_needed, programme:)
end

let!(:patients) do
[
patient_with_reminder_sent,
patient_not_sent_reminder,
patient_with_consent
]
end

context "when session is unscheduled" do
let(:session) { create(:session, :unscheduled, patients:, programme:) }

described_class.perform_now
expect(ConsentRemindersSessionBatchJob).to have_been_enqueued.once
expect(ConsentRemindersSessionBatchJob).to have_been_enqueued.with(
active_session
it "doesn't send any notifications" do
expect(ConsentNotification).not_to receive(:create_and_send!)
perform_now
end
end

context "when session is in the future" do
let(:session) do
create(
:session,
patients:,
programme:,
send_consent_reminders_at: 2.days.from_now
)
end

it "doesn't send any notifications" do
expect(ConsentNotification).not_to receive(:create_and_send!)
perform_now
end
end

context "with sessions set to send consent today and in the future" do
it "enqueues ConsentRemindersSessionBatchJob for the session set to send consent today" do
active_session =
create(:session, programme:, send_consent_reminders_at: Time.zone.today)
_later_session =
create(:session, programme:, send_consent_reminders_at: 2.days.from_now)

described_class.perform_now
expect(ConsentRemindersSessionBatchJob).to have_been_enqueued.once
expect(ConsentRemindersSessionBatchJob).to have_been_enqueued.with(
active_session
context "when the session is today" do
let(:session) do
create(
:session,
patients:,
programme:,
send_consent_reminders_at: Date.current
)
end

it "sends notifications to one patient" do
expect(ConsentNotification).to receive(:create_and_send!).once.with(
patient: patient_not_sent_reminder,
programme:,
session:,
reminder: true
)
perform_now
end
end
end
61 changes: 0 additions & 61 deletions spec/jobs/consent_reminders_session_batch_job_spec.rb

This file was deleted.

Loading
Loading