Skip to content

Commit 2636e07

Browse files
Refactor notice text into helper function
This is an effort to deduplicate the hardcoded values of the important notice text. In particular, this is triggered by the introduction of the notice for patients who don't want their parents notified after self-consent; this requires dynamic content.
1 parent 1472652 commit 2636e07

File tree

7 files changed

+249
-86
lines changed

7 files changed

+249
-86
lines changed

app/components/app_notices_table_component.rb

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,84 +5,38 @@ def initialize(
55
deceased_patients:,
66
invalidated_patients:,
77
restricted_patients:,
8-
gillick_no_notify_patients:
8+
has_vaccination_records_dont_notify_parents_patients:
99
)
1010
super
1111

1212
@deceased_patients = deceased_patients
1313
@invalidated_patients = invalidated_patients
1414
@restricted_patients = restricted_patients
15-
@gillick_no_notify_patients = gillick_no_notify_patients
15+
@has_vaccination_records_dont_notify_parents_patients =
16+
has_vaccination_records_dont_notify_parents_patients
1617
end
1718

1819
def render?
1920
@deceased_patients.present? || @invalidated_patients.present? ||
20-
@restricted_patients.present? || @gillick_no_notify_patients.present?
21+
@restricted_patients.present? ||
22+
@has_vaccination_records_dont_notify_parents_patients.present?
2123
end
2224

2325
private
2426

2527
def notices
26-
(
27-
deceased_notices + invalidated_notices + restricted_notices +
28-
gillick_no_notify_notices
29-
).sort_by { _1[:date_time] }.reverse
30-
end
31-
32-
def deceased_notices
33-
@deceased_patients.map do |patient|
34-
{
35-
patient:,
36-
date_time: patient.date_of_death_recorded_at,
37-
message: "Record updated with child’s date of death"
38-
}
39-
end
40-
end
41-
42-
def invalidated_notices
43-
@invalidated_patients.map do |patient|
44-
{
45-
patient:,
46-
date_time: patient.invalidated_at,
47-
message: "Record flagged as invalid"
48-
}
49-
end
50-
end
51-
52-
def restricted_notices
53-
@restricted_patients.map do |patient|
54-
{
55-
patient:,
56-
date_time: patient.restricted_at,
57-
message: "Record flagged as sensitive"
58-
}
59-
end
60-
end
61-
62-
def gillick_no_notify_notices
63-
64-
@gillick_no_notify_patients.map do |patient|
65-
vaccination_records = patient.vaccination_records.includes(:programme).select { it.notify_parents == false }
66-
67-
{
68-
patient:,
69-
date_time:
70-
patient
71-
.vaccination_records
72-
.reject(&:notify_parents?)
73-
.max_by(&:created_at)
74-
&.created_at || Time.current,
75-
message:
76-
"Child gave consent for #{format_vaccinations(vaccination_records)} under Gillick competence and " \
77-
"does not want their parents to be notified. " \
78-
"These records will not be automatically synced with GP records. " \
79-
"Your team must let the child’s GP know they were vaccinated."
80-
}
81-
end
82-
end
83-
84-
def format_vaccinations(vaccination_records)
85-
"#{vaccination_records.map(&:programme).map(&:name).to_sentence} " \
86-
"#{"vaccination".pluralize(vaccination_records.length)}"
28+
all_patients =
29+
(
30+
@deceased_patients + @invalidated_patients + @restricted_patients +
31+
@has_vaccination_records_dont_notify_parents_patients
32+
).uniq
33+
34+
notices =
35+
all_patients.flat_map do |patient|
36+
helpers
37+
.patient_important_notices(patient)
38+
.map { |notification| notification.merge(patient:) }
39+
end
40+
notices.sort_by { it[:date_time] }.reverse
8741
end
8842
end

app/components/app_patient_card_component.rb

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,9 @@ class AppPatientCardComponent < ViewComponent::Base
44
erb_template <<-ERB
55
<%= render AppCardComponent.new(heading_level:, section: true) do |card| %>
66
<% card.with_heading { "Child’s details" } %>
7-
8-
<% if patient.date_of_death.present? %>
9-
<%= render AppStatusComponent.new(
10-
text: "Record updated with child’s date of death"
11-
) %>
12-
<% end %>
13-
14-
<% if patient.invalidated? %>
15-
<%= render AppStatusComponent.new(
16-
text: "Record flagged as invalid"
17-
) %>
18-
<% end %>
19-
20-
<% if patient.restricted? %>
21-
<%= render AppStatusComponent.new(
22-
text: "Record flagged as sensitive"
23-
) %>
7+
8+
<% helpers.patient_important_notices(patient).each do |notification| %>
9+
<%= render AppStatusComponent.new(text: notification[:message]) %>
2410
<% end %>
2511
2612
<%= render AppChildSummaryComponent.new(

app/controllers/imports/notices_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def index
99
@deceased_patients = policy_scope(Patient).deceased
1010
@invalidated_patients = policy_scope(Patient).invalidated
1111
@restricted_patients = policy_scope(Patient).restricted
12-
@gillick_no_notify_patients = policy_scope(Patient).gillick_no_notify
12+
@has_vaccination_records_dont_notify_parents_patients =
13+
policy_scope(Patient).has_vaccination_records_dont_notify_parents
1314
end
1415
end

app/helpers/patients_helper.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,51 @@ def patient_year_group(patient, academic_year:)
5252
def patient_parents(patient)
5353
format_parents_with_relationships(patient.parent_relationships)
5454
end
55+
56+
def patient_important_notices(patient)
57+
notifications = []
58+
59+
if patient.deceased?
60+
notifications << {
61+
date_time: patient.date_of_death_recorded_at,
62+
message: "Record updated with child’s date of death"
63+
}
64+
end
65+
66+
if patient.invalidated?
67+
notifications << {
68+
date_time: patient.invalidated_at,
69+
message: "Record flagged as invalid"
70+
}
71+
end
72+
73+
if patient.restricted?
74+
notifications << {
75+
date_time: patient.restricted_at,
76+
message: "Record flagged as sensitive"
77+
}
78+
end
79+
80+
no_notify_vaccination_records =
81+
patient.vaccination_records.select { it.notify_parents == false }
82+
if no_notify_vaccination_records.any?
83+
notifications << {
84+
date_time: no_notify_vaccination_records.maximum(:performed_at),
85+
message:
86+
"Child gave consent for #{format_vaccinations(no_notify_vaccination_records)} under Gillick competence and " \
87+
"does not want their parents to be notified. " \
88+
"These records will not be automatically synced with GP records. " \
89+
"Your team must let the child's GP know they were vaccinated."
90+
}
91+
end
92+
93+
notifications.sort_by { |notification| notification[:date_time] }.reverse
94+
end
95+
96+
private
97+
98+
def format_vaccinations(vaccination_records)
99+
"#{vaccination_records.map(&:programme).map(&:name).to_sentence} " \
100+
"#{"vaccination".pluralize(vaccination_records.length)}"
101+
end
55102
end

app/models/patient.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class Patient < ApplicationRecord
122122
scope :not_deceased, -> { where(date_of_death: nil) }
123123
scope :restricted, -> { where.not(restricted_at: nil) }
124124

125-
scope :gillick_no_notify,
125+
scope :has_vaccination_records_dont_notify_parents,
126126
-> do
127127
joins(:vaccination_records).where(
128128
vaccination_records: {
@@ -132,7 +132,12 @@ class Patient < ApplicationRecord
132132
end
133133

134134
scope :with_notice,
135-
-> { (deceased + restricted + invalidated + gillick_no_notify).uniq }
135+
-> do
136+
(
137+
deceased + restricted + invalidated +
138+
has_vaccination_records_dont_notify_parents
139+
).uniq
140+
end
136141

137142
scope :appear_in_programmes,
138143
->(programmes, academic_year:) do

app/views/imports/notices/index.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
<%= render AppImportsNavigationComponent.new(active: :notices) %>
66

7-
<% if @deceased_patients.any? || @invalidated_patients.any? || @restricted_patients.any? || @gillick_no_notify_patients.any? %>
7+
<% if @deceased_patients.any? || @invalidated_patients.any? || @restricted_patients.any? || @has_vaccination_records_dont_notify_parents_patients.any? %>
88
<%= render AppNoticesTableComponent.new(
99
deceased_patients: @deceased_patients,
1010
invalidated_patients: @invalidated_patients,
1111
restricted_patients: @restricted_patients,
12-
gillick_no_notify_patients: @gillick_no_notify_patients,
12+
has_vaccination_records_dont_notify_parents_patients: @has_vaccination_records_dont_notify_parents_patients,
1313
) %>
1414
<% else %>
1515
<p class="nhsuk-body"><%= t(".no_results") %></p>

0 commit comments

Comments
 (0)