Skip to content

Commit ae48a05

Browse files
authored
Merge pull request #6285 from NHSDigital/bugfix-missing-patient-id-on-notify-log-entries
Bugfix for consent confirmation comms activity log entries not appearing in some cases
2 parents baa8947 + 826cf79 commit ae48a05

5 files changed

Lines changed: 55 additions & 2 deletions

app/lib/govuk_notify_personalisation.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ def initialize(
2525
@consent = consent
2626
@consent_form = consent_form
2727
@parent = parent || consent&.parent
28-
@patient = patient || consent&.patient || vaccination_record&.patient
28+
@patient =
29+
patient || consent&.patient || vaccination_record&.patient ||
30+
Patient.find_by(id: consent_form&.matched_patient&.id)
2931
@session = session || consent_form&.session || vaccination_record&.session
3032
@team =
3133
team || session&.team || consent_form&.team || consent&.team ||
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
class BackfillPatientIdOnNotifyLogEntries < ActiveRecord::Migration[8.1]
4+
def up
5+
migration = self.class.name
6+
started_at = Time.zone.now
7+
8+
scope = NotifyLogEntry.where(patient_id: nil).where.not(consent_form_id: nil)
9+
10+
Rails.logger.info(event: "data_migration_start", migration:, total_records: scope.count)
11+
12+
records_updated =
13+
scope
14+
.joins(
15+
"INNER JOIN consents ON consents.consent_form_id = notify_log_entries.consent_form_id"
16+
)
17+
.update_all("patient_id = consents.patient_id")
18+
19+
duration_minutes = ((Time.zone.now - started_at) / 60.0).round
20+
21+
Rails.logger.info(
22+
event: "data_migration_finish",
23+
migration:,
24+
duration_minutes:,
25+
records_updated:
26+
)
27+
end
28+
29+
def down
30+
raise ActiveRecord::IrreversibleMigration
31+
end
32+
end

db/data_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DataMigrate::Data.define(version: 2026_03_05_165603)
1+
DataMigrate::Data.define(version: 2026_03_12_182158)

spec/features/parental_consent_inexact_auto_match_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,6 @@ def and_they_see_consent_contact_warning_notifications
121121
expect(page).to have_content("Consent unknown contact details warning sent")
122122
expect(page).to have_content(@parent.email)
123123
expect(page).to have_content(@parent.phone)
124+
expect(page).to have_content("Consent confirmation given sent")
124125
end
125126
end

spec/jobs/email_delivery_job_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,24 @@
206206
expect(notify_log_entry.programmes.map(&:type)).to eq(programme_types)
207207
end
208208

209+
context "when the consent form is matched to a patient" do
210+
let(:matched_patient) { create(:patient) }
211+
212+
before do
213+
create(
214+
:consent,
215+
programme: session.programmes.first,
216+
consent_form:,
217+
patient: matched_patient
218+
)
219+
end
220+
221+
it "sets patient on the log entry from the matched consent form" do
222+
perform_now
223+
expect(NotifyLogEntry.last.patient).to eq(matched_patient)
224+
end
225+
end
226+
209227
context "when the parent doesn't have a phone number" do
210228
let(:consent_form) do
211229
create(:consent_form, session:, parent_email: nil)

0 commit comments

Comments
 (0)