Skip to content

Commit 5e378a3

Browse files
committed
Revert "Migrate old notice generator function to new model"
This reverts commit 9efa49d.
1 parent 8d55faf commit 5e378a3

10 files changed

Lines changed: 317 additions & 61 deletions

app/components/app_imports_navigation_component.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def call
2020
selected: active == :issues
2121
)
2222

23-
if policy(ImportantNotice).index?
23+
if policy(:notices).index?
2424
nav.with_item(
2525
href: imports_notices_path,
2626
text: notices_text,
@@ -42,7 +42,7 @@ def issues_text
4242
end
4343

4444
def notices_text
45-
count = policy_scope(ImportantNotice).count
45+
count = ImportantNotices.call(patient_scope: policy_scope(Patient)).length
4646

4747
text_with_count("Important notices", count)
4848
end

app/components/app_notices_table_component.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
<% body.with_row do |row| %>
2020
<% row.with_cell do %>
2121
<span class="nhsuk-table-responsive__heading">Date</span>
22-
<%= notice.recorded_at.to_date.to_fs(:long) %>
22+
<%= notice[:date_time].to_date.to_fs(:long) %>
2323
<% end %>
2424
<% row.with_cell do %>
2525
<span class="nhsuk-table-responsive__heading">Child</span>
26-
<%= link_to notice.patient.full_name, patient_path(notice.patient) %>
26+
<%= link_to notice[:patient].full_name, patient_path(notice[:patient]) %>
2727
<% end %>
2828
<% row.with_cell do %>
2929
<span class="nhsuk-table-responsive__heading">Notice</span>
30-
<%= notice.message %>
30+
<%= notice[:message] %>
3131
<% end %>
3232
<% end %>
3333
<% end %>

app/components/app_patient_card_component.rb

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class AppPatientCardComponent < ViewComponent::Base
66
<% card.with_heading(level: heading_level) { "Child’s details" } %>
77
88
<% important_notices.each do |notice| %>
9-
<%= render AppStatusComponent.new(text: notice) %>
9+
<%= render AppStatusComponent.new(text: notice[:message]) %>
1010
<% end %>
1111
1212
<%= render AppChildSummaryComponent.new(
@@ -46,40 +46,5 @@ def initialize(
4646

4747
def show_school_and_year_group = patient.show_year_group?(team: current_team)
4848

49-
def important_notices
50-
notices = patient.important_notices.where(team_id: current_team.id)
51-
52-
[
53-
(
54-
if patient.restricted?
55-
notices.restricted.order(recorded_at: :desc).first&.message
56-
end
57-
),
58-
(
59-
if patient.invalidated?
60-
notices.invalidated.order(recorded_at: :desc).first&.message
61-
end
62-
),
63-
*notices.deceased.first&.message,
64-
*gillick_no_notify_notices
65-
].compact
66-
end
67-
68-
def gillick_no_notify_notices
69-
no_notify_vaccination_records =
70-
patient.vaccination_records.select do
71-
it.notify_parents == false && it.team == current_team
72-
end
73-
74-
if no_notify_vaccination_records.any?
75-
vaccinations_sentence =
76-
"#{no_notify_vaccination_records.map(&:programme).uniq.map(&:name).to_sentence} " \
77-
"#{"vaccination".pluralize(no_notify_vaccination_records.length)}"
78-
79-
"Child gave consent for #{vaccinations_sentence} under Gillick competence and " \
80-
"does not want their parents to be notified. " \
81-
"These records will not be automatically synced with GP records. " \
82-
"Your team must let the child's GP know they were vaccinated."
83-
end
84-
end
49+
def important_notices = ImportantNotices.call(patient:)
8550
end

app/controllers/dashboard_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class DashboardController < ApplicationController
77

88
def index
99
@notices_count =
10-
(policy_scope(ImportantNotice).count if policy(ImportantNotice).index?)
10+
if policy(:notices).index?
11+
ImportantNotices.call(patient_scope: policy_scope(Patient)).length
12+
end
1113
end
1214
end

app/controllers/imports/notices_controller.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ class Imports::NoticesController < ApplicationController
44
layout "full"
55

66
def index
7-
authorize ImportantNotice
7+
authorize :notices
88

9-
@notices =
10-
policy_scope(ImportantNotice).includes(
11-
vaccination_record: :programme
12-
).order(recorded_at: :desc)
9+
@notices = ImportantNotices.call(patient_scope: policy_scope(Patient))
1310
end
1411
end

app/lib/important_notices.rb

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# frozen_string_literal: true
2+
3+
class ImportantNotices
4+
def initialize(patient_scope: nil, patient: nil)
5+
@patient_scope = patient_scope
6+
@patient = patient
7+
8+
if patient_scope.nil? && patient.nil?
9+
raise "Pass either a patient_scope or a patient."
10+
end
11+
end
12+
13+
def call
14+
notices =
15+
patients.flat_map do |patient|
16+
notices_for_patient(patient).map { it.merge(patient:) }
17+
end
18+
19+
notices.sort_by { it[:date_time] }.reverse
20+
end
21+
22+
def self.call(...) = new(...).call
23+
24+
private_class_method :new
25+
26+
private
27+
28+
attr_reader :patient_scope, :patient
29+
30+
def patients
31+
if patient
32+
[patient]
33+
else
34+
patient_scope_with_notices.includes(vaccination_records: :programme)
35+
end
36+
end
37+
38+
def patient_scope_with_notices
39+
patient_scope
40+
.deceased
41+
.or(patient_scope.invalidated)
42+
.or(patient_scope.restricted)
43+
.or(patient_scope.has_vaccination_records_dont_notify_parents)
44+
end
45+
46+
def notices_for_patient(patient)
47+
notices = []
48+
49+
if patient.deceased?
50+
notices << {
51+
date_time: patient.date_of_death_recorded_at,
52+
message: "Record updated with child’s date of death"
53+
}
54+
end
55+
56+
if patient.invalidated?
57+
notices << {
58+
date_time: patient.invalidated_at,
59+
message: "Record flagged as invalid"
60+
}
61+
end
62+
63+
if patient.restricted?
64+
notices << {
65+
date_time: patient.restricted_at,
66+
message: "Record flagged as sensitive"
67+
}
68+
end
69+
70+
no_notify_vaccination_records =
71+
patient.vaccination_records.select { it.notify_parents == false }
72+
if no_notify_vaccination_records.any?
73+
vaccinations_sentence =
74+
"#{no_notify_vaccination_records.map(&:programme).uniq.map(&:name).to_sentence} " \
75+
"#{"vaccination".pluralize(no_notify_vaccination_records.length)}"
76+
77+
notices << {
78+
date_time: no_notify_vaccination_records.maximum(:performed_at),
79+
message:
80+
"Child gave consent for #{vaccinations_sentence} under Gillick competence and " \
81+
"does not want their parents to be notified. " \
82+
"These records will not be automatically synced with GP records. " \
83+
"Your team must let the child's GP know they were vaccinated."
84+
}
85+
end
86+
87+
notices
88+
end
89+
end

app/policies/notices_policy.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class NoticesPolicy < ApplicationPolicy
4+
def index?
5+
user.can_access_sensitive_flagged_records?
6+
end
7+
end

spec/components/app_patient_card_component_spec.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
let(:programmes) { [CachedProgramme.hpv] }
1414
let(:team) { create(:team, programmes:) }
15-
let(:session) { create(:session, team:, programmes:) }
1615
let(:school) { create(:school, team:) }
1716

1817
let(:patient) { create(:patient, school:, year_group: 8) }
@@ -25,19 +24,19 @@
2524
it { should have_content("Address") }
2625

2726
context "with a deceased patient" do
28-
let(:patient) { create(:patient, :deceased, session:) }
27+
let(:patient) { create(:patient, :deceased) }
2928

3029
it { should have_content("Record updated with child’s date of death") }
3130
end
3231

3332
context "with an invalidated patient" do
34-
let(:patient) { create(:patient, :invalidated, session:) }
33+
let(:patient) { create(:patient, :invalidated) }
3534

3635
it { should have_content("Record flagged as invalid") }
3736
end
3837

3938
context "with a restricted patient" do
40-
let(:patient) { create(:patient, :restricted, session:) }
39+
let(:patient) { create(:patient, :restricted) }
4140

4241
it { should have_content("Record flagged as sensitive") }
4342

spec/factories/patients.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,14 @@
160160
trait :deceased do
161161
date_of_death { Date.current }
162162
date_of_death_recorded_at { Time.current }
163-
after(:create) do |instance|
164-
ImportantNoticeGeneratorJob.perform_now(instance.id)
165-
end
166163
end
167164

168165
trait :invalidated do
169166
invalidated_at { Time.current }
170-
after(:create) do |instance|
171-
ImportantNoticeGeneratorJob.perform_now(instance.id)
172-
end
173167
end
174168

175169
trait :restricted do
176170
restricted_at { Time.current }
177-
after(:create) do |instance|
178-
ImportantNoticeGeneratorJob.perform_now(instance.id)
179-
end
180171
end
181172

182173
trait :archived do

0 commit comments

Comments
 (0)