Skip to content

Commit 4c8c83d

Browse files
Merge pull request #5969 from nhsuk/rework-already-had-notification-sender
Rework already had notification sender
2 parents 7b1aa59 + 747bf84 commit 4c8c83d

7 files changed

Lines changed: 305 additions & 329 deletions

app/jobs/search_vaccination_records_in_nhs_job.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ def perform(patient_id)
4545
end
4646

4747
# Remaining incoming_vaccination_records are new
48-
incoming_vaccination_records.each do |vaccination_record|
49-
vaccination_record.save!
50-
AlreadyHadNotificationSender.call(vaccination_record:)
51-
end
48+
incoming_vaccination_records.each(&:save!)
5249

5350
update_vaccination_search_timestamps if patient.nhs_number.present?
5451

5552
PatientStatusUpdater.call(patient:)
53+
54+
incoming_vaccination_records.each do |vaccination_record|
55+
AlreadyHadNotificationSender.call(vaccination_record:)
56+
end
5657
end
5758
end
5859

app/lib/already_had_notification_sender.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ def initialize(vaccination_record:)
77

88
def call
99
return if vaccination_record.sourced_from_service?
10-
return if would_still_be_vaccinated?
10+
return if was_already_vaccinated?
11+
return if is_still_eligible_for_vaccination?
1112

1213
consents = patient.consents.includes(:parent)
1314

@@ -58,15 +59,25 @@ def self.call(...) = new(...).call
5859

5960
attr_reader :vaccination_record
6061

61-
delegate :patient, :programme_type, to: :vaccination_record
62+
delegate :patient, :programme_type, :programme, to: :vaccination_record
6263

6364
def academic_year = AcademicYear.current
6465

65-
def other_vaccination_records
66-
patient.vaccination_records.where.not(id: vaccination_record.id)
66+
def previous_vaccination_records
67+
# We need to ensure there is a deterministic order of vaccination records on
68+
# a child so that the :already_vaccinated_notification is sent once and only
69+
# once per child. Using created_at alone is not sufficient as batching
70+
# allows for multiple vaccination records to be created with the same
71+
# created_at timestamp. We chose the ID as a tie-breaker since IDs are
72+
# unique and sequential.
73+
patient.vaccination_records.where(
74+
"(created_at < :created_at) OR (created_at = :created_at AND id < :id)",
75+
created_at: vaccination_record.created_at,
76+
id: vaccination_record.id
77+
)
6778
end
6879

69-
def would_still_be_vaccinated?
80+
def was_already_vaccinated?
7081
# We're not using the existing `Patient::ProgrammeStatus` instance here
7182
# because we want to know if the patient would still be vaccinated if we
7283
# took away the vaccination record in question, to know whether to send
@@ -79,11 +90,15 @@ def would_still_be_vaccinated?
7990
programme_type:,
8091
academic_year:,
8192
patient:,
82-
vaccination_records: other_vaccination_records,
93+
vaccination_records: previous_vaccination_records,
8394
patient_locations: [],
8495
consents: [],
8596
triages: [],
8697
attendance_record: nil
8798
).status == :vaccinated
8899
end
100+
101+
def is_still_eligible_for_vaccination?
102+
!patient.programme_status(programme, academic_year:).vaccinated?
103+
end
89104
end

app/lib/notification_parent_selector.rb

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,14 @@
33
class NotificationParentSelector
44
def initialize(vaccination_record:, consents: nil)
55
@vaccination_record = vaccination_record
6-
76
@consents =
8-
if consents.present?
9-
consents
7+
if @vaccination_record.patient.send_notifications?(
8+
team: @vaccination_record.team,
9+
send_to_archived: true
10+
) && @vaccination_record.notify_parents
11+
consents || @vaccination_record.patient.consents
1012
else
11-
patient = @vaccination_record.patient
12-
13-
if patient.send_notifications?(
14-
team: @vaccination_record.team,
15-
send_to_archived: true
16-
) && @vaccination_record.notify_parents
17-
patient.consents
18-
else
19-
[]
20-
end
13+
[]
2114
end
2215
end
2316

@@ -47,6 +40,10 @@ def parents = parents_with_consent.map(&:first)
4740

4841
def latest_consents
4942
@latest_consents ||=
50-
ConsentGrouper.call(consents, programme_type:, academic_year:)
43+
ConsentGrouper.call(
44+
consents,
45+
programme_type:,
46+
academic_year: AcademicYear.current
47+
)
5148
end
5249
end

app/models/immunisation_import.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,6 @@ def bulk_import(rows: 100)
129129
on_duplicate_key_update: :all
130130
)
131131

132-
vaccination_records.each do |vaccination_record|
133-
AlreadyHadNotificationSender.call(vaccination_record:)
134-
end
135-
136132
PatientLocation.import(patient_locations, on_duplicate_key_ignore: :all)
137133

138134
ArchiveReason.import(archive_reasons, on_duplicate_key_ignore: :all)
@@ -175,6 +171,12 @@ def postprocess_rows!
175171

176172
PatientTeamUpdater.call(patient_scope: patients)
177173
PatientStatusUpdater.call(patient_scope: patients)
174+
175+
vaccination_records
176+
.includes(:patient, :team)
177+
.find_each do |vaccination_record|
178+
AlreadyHadNotificationSender.call(vaccination_record:)
179+
end
178180
end
179181

180182
def post_commit!

0 commit comments

Comments
 (0)