Skip to content

Commit

Permalink
Track consent notifications
Browse files Browse the repository at this point in the history
This updates the consent reminders and requests to track when the
notifications are sent by creating ConsentNotification instances.
  • Loading branch information
thomasleese committed Oct 1, 2024
1 parent aeeb0fe commit 1735e2a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 28 deletions.
4 changes: 4 additions & 0 deletions app/jobs/consent_reminders_session_batch_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def perform(session)
end

patient.update!(consent_reminder_sent_at: Time.zone.now)

session.programmes.each do |programme|
ConsentNotification.create!(programme:, patient:, reminder: true)
end
end
end
end
6 changes: 5 additions & 1 deletion app/jobs/consent_requests_session_batch_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ConsentRequestsSessionBatchJob < ApplicationJob
queue_as :default

def perform(session)
session.patients.consent_request_not_sent.each do |patient|
session.patients.needing_consent_request.each do |patient|
patient.parents.each do |parent|
ConsentMailer.with(parent:, patient:, session:).request.deliver_now
TextDeliveryJob.perform_later(
Expand All @@ -26,6 +26,10 @@ def perform(session)
end

patient.update!(consent_request_sent_at: Time.zone.now)

session.programmes.each do |programme|
ConsentNotification.create!(programme:, patient:, reminder: false)
end
end
end
end
3 changes: 3 additions & 0 deletions app/models/patient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class Patient < ApplicationRecord

scope :without_consent,
-> { includes(:consents).where(consents: { id: nil }) }

scope :needing_consent_request,
-> { without_consent.consent_request_not_sent }
scope :needing_consent_reminder,
-> { consent_request_sent.without_consent.consent_reminder_not_sent }

Expand Down
22 changes: 22 additions & 0 deletions spec/factories/patients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,28 @@

trait :consent_request_sent do
consent_request_sent_at { 1.week.ago }

after(:create) do |patient, context|
create(
:consent_notification,
:request,
patient:,
programme: context.programme
)
end
end

trait :consent_reminder_sent do
consent_reminder_sent_at { 1.week.ago }

after(:create) do |patient, context|
create(
:consent_notification,
:reminder,
patient:,
programme: context.programme
)
end
end

trait :consent_given_triage_not_needed do
Expand Down
27 changes: 18 additions & 9 deletions spec/jobs/consent_reminders_session_batch_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@
let(:programme) { create(:programme) }
let(:parents) { create_list(:parent, 2) }

let(:patient_with_reminder_sent) do
create(
:patient,
:consent_request_sent,
consent_reminder_sent_at: Date.current
)
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:)
let!(:patient_not_sent_reminder) do
create(:patient, :consent_request_sent, parents:, programme:)
end
let(:patient_with_consent) do
let!(:patient_with_consent) do
create(:patient, :consent_given_triage_not_needed, programme:)
end

Expand Down Expand Up @@ -49,4 +45,17 @@
patient_not_sent_reminder.reload.consent_reminder_sent_at
}.to(today)
end

it "creates a consent notification record" do
expect { perform_now }.to change(ConsentNotification, :count).by(1)

consent_notification =
ConsentNotification.find_by(
programme:,
patient: patient_not_sent_reminder,
reminder: true
)
expect(consent_notification).not_to be_nil
expect(consent_notification.sent_at).to be_today
end
end
64 changes: 46 additions & 18 deletions spec/jobs/consent_requests_session_batch_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
# frozen_string_literal: true

describe ConsentRequestsSessionBatchJob do
let(:parents_of_patient_without_consent_sent) { create_list(:parent, 2) }
subject(:perform_now) do
travel_to(today) { described_class.perform_now(session) }
end

it "only sends emails to patient's parents to whom they have not been sent yet" do
patient_with_consent_sent =
create(:patient, consent_request_sent_at: Time.zone.today)
patient_not_sent_consent =
create(:patient, parents: parents_of_patient_without_consent_sent)
session =
create(
:session,
patients: [patient_with_consent_sent, patient_not_sent_consent]
)
let(:today) { Date.new(2024, 1, 1) }

expect { described_class.perform_now(session) }.to send_email(
to: parents_of_patient_without_consent_sent.first.email
).and send_email(to: parents_of_patient_without_consent_sent.second.email)
let(:programme) { create(:programme) }
let(:parents) { create_list(:parent, 2) }

let!(:patient_with_consent_sent) do
create(:patient, :consent_request_sent, programme:)
end
let!(:patient_not_sent_consent) { create(:patient, parents:) }
let!(:patient_with_consent) do
create(:patient, :consent_given_triage_not_needed, programme:)
end

let(:session) do
create(
:session,
patients: [
patient_with_consent_sent,
patient_not_sent_consent,
patient_with_consent
],
programmes: [programme]
)
end

it "only sends emails to patient's parents to whom they have not been sent yet" do
expect { perform_now }.to send_email(
to: parents.first.email
).and send_email(to: parents.second.email)

expect(ActionMailer::Base.deliveries.count).to eq(2)
end

it "updates the consent_request_sent_at attribute for patients" do
patient = create(:patient)
session = create(:session, patients: [patient])
expect { perform_now }.to change {
patient_not_sent_consent.reload.consent_request_sent_at
}.to(today)
end

it "creates a consent notification record" do
expect { perform_now }.to change(ConsentNotification, :count).by(1)

described_class.perform_now(session)
expect(patient.reload.consent_request_sent_at).to be_today
consent_notification =
ConsentNotification.find_by(
programme:,
patient: patient_not_sent_consent,
reminder: false
)
expect(consent_notification).not_to be_nil
expect(consent_notification.sent_at).to be_today
end
end

0 comments on commit 1735e2a

Please sign in to comment.