Skip to content

Commit 6e6c6cb

Browse files
committed
Add VaccinationConfirmationsJob
This adds a job which sends the vaccination confirmation emails at a specific time in the evening every day, ensuring that any vaccination records which have been created since the last ones were sent out, get processed. The job is set up such that it can be run at any point and it will send the confirmations for any new vaccination records that have been created since the last time the job ran will get their confirmation emails sent out.
1 parent 90e3d02 commit 6e6c6cb

11 files changed

+196
-10
lines changed

app/controllers/concerns/vaccination_mailer_concern.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def send_vaccination_confirmation(vaccination_record)
1717
text_template_name = :"vaccination_#{mailer_action}"
1818

1919
parents.each do |parent|
20-
params = { parent:, vaccination_record:, sent_by: current_user }
20+
params = { parent:, vaccination_record:, sent_by: try(:current_user) }
2121

2222
if parent.email.present?
2323
VaccinationMailer.with(params).public_send(mailer_action).deliver_later
@@ -32,7 +32,7 @@ def send_vaccination_deletion(vaccination_record)
3232
return if parents.empty?
3333

3434
parents.each do |parent|
35-
params = { parent:, vaccination_record:, sent_by: current_user }
35+
params = { parent:, vaccination_record:, sent_by: try(:current_user) }
3636

3737
if parent.email.present?
3838
VaccinationMailer.with(params).deleted.deliver_later
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
class VaccinationConfirmationsJob < ApplicationJob
4+
include VaccinationMailerConcern
5+
6+
queue_as :notifications
7+
8+
def perform
9+
# Find the oldest record that has had a confirmation sent, and send confirmations for all subsequent records
10+
since =
11+
VaccinationRecord
12+
.kept
13+
.where.not(confirmation_sent_at: nil)
14+
.maximum(:created_at) || 24.hours.ago
15+
academic_year = Date.current.academic_year
16+
17+
VaccinationRecord
18+
.kept
19+
.where("created_at >= ?", since)
20+
.where(confirmation_sent_at: nil)
21+
.select { _1.academic_year == academic_year }
22+
.each do |vaccation_record|
23+
send_vaccination_confirmation(vaccation_record)
24+
vaccation_record.update!(confirmation_sent_at: Time.current)
25+
end
26+
end
27+
end

app/models/vaccination_record.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ def dose_volume_ml
178178
vaccine.dose_volume_ml * 1 if vaccine.present?
179179
end
180180

181+
def academic_year
182+
performed_at.to_date.academic_year
183+
end
184+
181185
private
182186

183187
def requires_location_name?

config/environments/production.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@
158158
cron: "every day at 2am",
159159
class: "TrimActiveRecordSessionsJob",
160160
description: "Remove ActiveRecord sessions older than 30 days"
161+
},
162+
vaccination_confirmations: {
163+
cron: "every day at 7pm",
164+
class: "VaccinationConfirmationsJob",
165+
description: "Send vaccination confirmation emails to parents"
161166
}
162167
}
163168
end

spec/features/hpv_vaccination_administered_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
when_i_go_to_the_patient
5353
then_i_see_that_the_status_is_vaccinated
5454
and_i_see_the_vaccination_details
55-
and_an_email_is_sent_to_the_parent_confirming_the_vaccination
55+
56+
when_vaccination_confirmations_are_sent
57+
then_an_email_is_sent_to_the_parent_confirming_the_vaccination
5658
and_a_text_is_sent_to_the_parent_confirming_the_vaccination
5759
end
5860

@@ -181,7 +183,11 @@ def and_i_see_the_vaccination_details
181183
expect(page).to have_content("Vaccination details")
182184
end
183185

184-
def and_an_email_is_sent_to_the_parent_confirming_the_vaccination
186+
def when_vaccination_confirmations_are_sent
187+
VaccinationConfirmationsJob.perform_now
188+
end
189+
190+
def then_an_email_is_sent_to_the_parent_confirming_the_vaccination
185191
expect_email_to(
186192
@patient.consents.last.parent.email,
187193
:vaccination_confirmation_administered

spec/features/hpv_vaccination_already_had_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
when_i_go_to_the_patient
2424
then_i_see_that_the_status_is_could_not_vaccinate
25-
and_an_email_is_sent_saying_the_vaccination_didnt_happen
25+
26+
when_vaccination_confirmations_are_sent
27+
then_an_email_is_sent_saying_the_vaccination_didnt_happen
2628
and_a_text_is_sent_saying_the_vaccination_didnt_happen
2729
end
2830

@@ -89,7 +91,11 @@ def then_i_see_that_the_status_is_could_not_vaccinate
8991
)
9092
end
9193

92-
def and_an_email_is_sent_saying_the_vaccination_didnt_happen
94+
def when_vaccination_confirmations_are_sent
95+
VaccinationConfirmationsJob.perform_now
96+
end
97+
98+
def then_an_email_is_sent_saying_the_vaccination_didnt_happen
9399
expect_email_to(
94100
@patient.consents.last.parent.email,
95101
:vaccination_confirmation_not_administered

spec/features/hpv_vaccination_clinic_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
when_i_go_to_the_patient
2727
then_i_see_that_the_status_is_vaccinated
28-
and_an_email_is_sent_to_the_parent_confirming_the_vaccination
28+
29+
when_vaccination_confirmations_are_sent
30+
then_an_email_is_sent_to_the_parent_confirming_the_vaccination
2931
and_a_text_is_sent_to_the_parent_confirming_the_vaccination
3032
end
3133

@@ -108,7 +110,11 @@ def then_i_see_that_the_status_is_vaccinated
108110
expect(page).to have_content("Vaccinated")
109111
end
110112

111-
def and_an_email_is_sent_to_the_parent_confirming_the_vaccination
113+
def when_vaccination_confirmations_are_sent
114+
VaccinationConfirmationsJob.perform_now
115+
end
116+
117+
def then_an_email_is_sent_to_the_parent_confirming_the_vaccination
112118
expect_email_to(
113119
@patient.consents.last.parent.email,
114120
:vaccination_confirmation_administered

spec/features/hpv_vaccination_delayed_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
when_i_go_to_the_patient
1919
then_i_see_that_the_status_is_delayed
20-
and_an_email_is_sent_to_the_parent_confirming_the_delay
20+
21+
when_vaccination_confirmations_are_sent
22+
then_an_email_is_sent_to_the_parent_confirming_the_delay
2123
and_a_text_is_sent_to_the_parent_confirming_the_delay
2224
end
2325

@@ -83,7 +85,11 @@ def then_i_see_that_the_status_is_delayed
8385
)
8486
end
8587

86-
def and_an_email_is_sent_to_the_parent_confirming_the_delay
88+
def when_vaccination_confirmations_are_sent
89+
VaccinationConfirmationsJob.perform_now
90+
end
91+
92+
def then_an_email_is_sent_to_the_parent_confirming_the_delay
8793
expect_email_to(
8894
@patient.consents.last.parent.email,
8995
:vaccination_confirmation_not_administered

spec/features/hpv_vaccination_offline_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
end
1010

1111
scenario "Download spreadsheet, record offline at a school session, upload vaccination outcomes back into Mavis" do
12+
stub_pds_get_nhs_number_to_return_a_patient
13+
1214
given_an_hpv_programme_is_underway
1315
when_i_choose_to_record_offline_from_a_school_session_page
1416
then_i_see_an_excel_spreadsheet_for_recording_offline
@@ -17,9 +19,15 @@
1719
and_i_upload_the_modified_csv_file
1820
and_i_navigate_to_the_session_page
1921
then_i_see_the_uploaded_vaccination_outcomes_reflected_in_the_session
22+
23+
when_vaccination_confirmations_are_sent
24+
then_an_email_is_sent_to_the_parent_confirming_the_vaccination
25+
and_a_text_is_sent_to_the_parent_confirming_the_vaccination
2026
end
2127

2228
scenario "Download spreadsheet, record offline at a clinic, upload vaccination outcomes back into Mavis" do
29+
stub_pds_get_nhs_number_to_return_a_patient
30+
2331
given_an_hpv_programme_is_underway(clinic: true)
2432
when_i_choose_to_record_offline_from_a_clinic_page
2533
then_i_see_an_excel_spreadsheet_for_recording_offline
@@ -29,6 +37,10 @@
2937
and_i_navigate_to_the_clinic_page
3038
then_i_see_the_uploaded_vaccination_outcomes_reflected_in_the_session
3139
and_the_clinic_location_is_displayed
40+
41+
when_vaccination_confirmations_are_sent
42+
then_an_email_is_sent_to_the_parent_confirming_the_vaccination
43+
and_a_text_is_sent_to_the_parent_confirming_the_vaccination
3244
end
3345

3446
def given_an_hpv_programme_is_underway(clinic: false)
@@ -267,4 +279,36 @@ def then_i_see_the_uploaded_vaccination_outcomes_reflected_in_the_session
267279
def and_the_clinic_location_is_displayed
268280
expect(page).to have_content("Westfield Shopping Centre")
269281
end
282+
283+
def when_vaccination_confirmations_are_sent
284+
VaccinationConfirmationsJob.perform_now
285+
end
286+
287+
def then_an_email_is_sent_to_the_parent_confirming_the_vaccination
288+
expect_email_to(
289+
@vaccinated_patient.consents.last.parent.email,
290+
:vaccination_confirmation_administered,
291+
:any
292+
)
293+
294+
expect_email_to(
295+
@unvaccinated_patient.consents.last.parent.email,
296+
:vaccination_confirmation_not_administered,
297+
:any
298+
)
299+
end
300+
301+
def and_a_text_is_sent_to_the_parent_confirming_the_vaccination
302+
expect_text_to(
303+
@vaccinated_patient.consents.last.parent.phone,
304+
:vaccination_confirmation_administered,
305+
:any
306+
)
307+
308+
expect_text_to(
309+
@unvaccinated_patient.consents.last.parent.phone,
310+
:vaccination_confirmation_not_administered,
311+
:any
312+
)
313+
end
270314
end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# frozen_string_literal: true
2+
3+
describe VaccinationConfirmationsJob do
4+
let(:job) { described_class.new }
5+
6+
describe "#perform" do
7+
subject(:perform) { job.perform }
8+
9+
let(:programme) { create(:programme) }
10+
11+
let(:existing_vaccination_record_already_sent) do
12+
create(
13+
:vaccination_record,
14+
:confirmation_sent,
15+
programme:,
16+
created_at: 3.days.ago
17+
)
18+
end
19+
20+
let(:old_vaccination_record) do
21+
create(:vaccination_record, created_at: 2.days.ago, programme:)
22+
end
23+
24+
let(:new_vaccination_record) { create(:vaccination_record, programme:) }
25+
26+
let(:discarded_vaccination_record) do
27+
create(:vaccination_record, :discarded, programme:)
28+
end
29+
30+
let(:historical_vaccination_record) do
31+
create(
32+
:vaccination_record,
33+
performed_at: Time.zone.local(2020, 1, 1),
34+
programme:
35+
)
36+
end
37+
38+
before { allow(job).to receive(:send_vaccination_confirmation) }
39+
40+
it "sends vaccination confirmations for approriate records" do
41+
expect(job).not_to receive(:send_vaccination_confirmation).with(
42+
existing_vaccination_record_already_sent
43+
)
44+
expect(job).to receive(:send_vaccination_confirmation).with(
45+
old_vaccination_record
46+
)
47+
expect(job).to receive(:send_vaccination_confirmation).with(
48+
new_vaccination_record
49+
)
50+
expect(job).not_to receive(:send_vaccination_confirmation).with(
51+
discarded_vaccination_record
52+
)
53+
expect(job).not_to receive(:send_vaccination_confirmation).with(
54+
historical_vaccination_record
55+
)
56+
57+
perform
58+
end
59+
60+
it "records when the confirmation was sent" do
61+
freeze_time do
62+
expect { perform }.to change {
63+
new_vaccination_record.reload.confirmation_sent_at
64+
}.from(nil).to(Time.current)
65+
end
66+
end
67+
end
68+
end

0 commit comments

Comments
 (0)