Skip to content

Commit 29d4ceb

Browse files
committed
Ensure PSDs are created after verbal consent
When a prescriber records verbal consent, we also ask the user if they would like to add a PSD to the patient. This wasn't working correctly, and instead a PSD wasn't being added to the patient. This is because the logic related to creating PSDs was specific to the triage journey that occurs outside of the verbal consent journey. Instead I've moved the logic in to the `TriageForm` ensuring that both triage journeys will have the same logic. Jira-Issue: MAV-1905
1 parent 2377f7e commit 29d4ceb

File tree

13 files changed

+228
-63
lines changed

13 files changed

+228
-63
lines changed

app/components/app_patient_session_triage_component.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# frozen_string_literal: true
22

33
class AppPatientSessionTriageComponent < ViewComponent::Base
4-
def initialize(patient_session, programme:, triage_form: nil)
4+
def initialize(patient_session, programme:, current_user:, triage_form: nil)
55
@patient_session = patient_session
66
@programme = programme
7+
@current_user = current_user
78
@triage_form = triage_form || default_triage_form
89
end
910

@@ -13,7 +14,7 @@ def render?
1314

1415
private
1516

16-
attr_reader :patient_session, :programme, :triage_form
17+
attr_reader :patient_session, :programme, :current_user, :triage_form
1718

1819
delegate :govuk_button_link_to, to: :helpers
1920
delegate :patient, :session, to: :patient_session
@@ -50,5 +51,7 @@ def latest_triage
5051
)
5152
end
5253

53-
def default_triage_form = TriageForm.new(patient_session:, programme:)
54+
def default_triage_form
55+
TriageForm.new(patient_session:, programme:, current_user:)
56+
end
5457
end

app/components/app_triage_form_component.html.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
<%= f.govuk_radio_buttons_fieldset :status_and_vaccine_method, **fieldset_options do %>
55
<% form.safe_to_vaccinate_options.each do |option| %>
66
<%= f.govuk_radio_button :status_and_vaccine_method, option do %>
7-
<% if show_psd_options?(option) %>
8-
<%= f.govuk_radio_buttons_fieldset :psd_action,
7+
<% if form.show_add_patient_specific_direction?(option) %>
8+
<%= f.govuk_radio_buttons_fieldset :add_patient_specific_direction,
99
legend: { text: "Do you want to add a PSD?", size: "s" } do %>
10-
<%= f.govuk_radio_button :add_psd, "true", label: { text: "Yes" } %>
11-
<%= f.govuk_radio_button :add_psd, "false", label: { text: "No" } %>
10+
<%= f.govuk_radio_button :add_patient_specific_direction, "true", label: { text: "Yes" } %>
11+
<%= f.govuk_radio_button :add_patient_specific_direction, "false", label: { text: "No" } %>
1212
<% end %>
1313
<% end %>
1414
<% end %>

app/components/app_triage_form_component.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ def initialize(form, url:, method: :post, heading: true, continue: false)
1919

2020
def builder = GOVUKDesignSystemFormBuilder::FormBuilder
2121

22-
def show_psd_options?(option)
23-
patient_session.session.psd_enabled? &&
24-
option == "safe_to_vaccinate_nasal" &&
25-
policy(PatientSpecificDirection).create?
26-
end
27-
2822
def fieldset_options
2923
text = "Is it safe to vaccinate #{patient.given_name}?"
3024
hint =

app/controllers/draft_consents_controller.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ def handle_triage
106106
@triage_form.assign_attributes(triage_form_params)
107107

108108
@draft_consent.assign_attributes(
109+
triage_add_patient_specific_direction:
110+
@triage_form.add_patient_specific_direction,
109111
triage_form_valid: @triage_form.valid?,
110112
triage_notes: @triage_form.notes,
111113
triage_status_and_vaccine_method: @triage_form.status_and_vaccine_method,
@@ -145,7 +147,13 @@ def update_params
145147
end
146148

147149
def triage_form_params
148-
params.expect(triage_form: %i[status_and_vaccine_method notes])
150+
params.expect(
151+
triage_form: %i[
152+
status_and_vaccine_method
153+
notes
154+
add_patient_specific_direction
155+
]
156+
)
149157
end
150158

151159
def set_draft_consent
@@ -184,6 +192,9 @@ def set_triage_form
184192
@triage_form =
185193
if policy(Triage).new?
186194
TriageForm.new(
195+
add_patient_specific_direction:
196+
@draft_consent.triage_add_patient_specific_direction,
197+
current_user:,
187198
notes: @draft_consent.triage_notes,
188199
vaccine_methods: @draft_consent.vaccine_methods,
189200
patient_session: @patient_session,

app/controllers/patient_sessions/triages_controller.rb

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def new
1818

1919
@triage_form =
2020
TriageForm.new(
21+
current_user:,
2122
patient_session: @patient_session,
2223
programme: @programme,
2324
triage: previous_triage
@@ -46,8 +47,6 @@ def create
4647
)
4748
.each { send_triage_confirmation(@patient_session, @programme, it) }
4849

49-
@triage_form.add_psd? ? ensure_psd_exists : remove_existing_psd
50-
5150
redirect_to redirect_path, flash: { success: "Triage outcome updated" }
5251
else
5352
render "patient_sessions/programmes/show",
@@ -59,7 +58,13 @@ def create
5958
private
6059

6160
def triage_form_params
62-
params.expect(triage_form: %i[status_and_vaccine_method notes add_psd])
61+
params.expect(
62+
triage_form: %i[
63+
status_and_vaccine_method
64+
notes
65+
add_patient_specific_direction
66+
]
67+
)
6368
end
6469

6570
def redirect_path
@@ -70,34 +75,4 @@ def redirect_path
7075
return_to: "triage"
7176
)
7277
end
73-
74-
def ensure_psd_exists
75-
# TODO: Handle programmes with multiple nasal vaccines.
76-
vaccine = @programme.vaccines.nasal.first
77-
78-
psd_attributes = {
79-
academic_year: @academic_year,
80-
delivery_site: "nose",
81-
patient: @patient,
82-
programme: @programme,
83-
vaccine:,
84-
vaccine_method: "nasal"
85-
}
86-
87-
return if PatientSpecificDirection.exists?(**psd_attributes)
88-
89-
PatientSpecificDirection.create!(
90-
psd_attributes.merge(created_by: current_user)
91-
)
92-
end
93-
94-
def remove_existing_psd
95-
PatientSpecificDirection.find_by(
96-
{
97-
academic_year: @academic_year,
98-
patient: @patient,
99-
programme: @programme
100-
}
101-
)&.destroy!
102-
end
10378
end

app/forms/triage_form.rb

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,21 @@ class TriageForm
66

77
attr_accessor :patient_session, :programme, :current_user
88

9-
attribute :status_and_vaccine_method, :string
10-
attribute :add_psd, :boolean
9+
attribute :add_patient_specific_direction, :boolean
1110
attribute :notes, :string
11+
attribute :status_and_vaccine_method, :string
1212
attribute :vaccine_methods, array: true, default: []
1313

14+
validates :add_patient_specific_direction,
15+
inclusion: {
16+
in: [true, false]
17+
},
18+
if: :requires_add_patient_specific_direction?
1419
validates :status_and_vaccine_method,
1520
inclusion: {
1621
in: :status_and_vaccine_method_options
1722
}
1823
validates :notes, length: { maximum: 1000 }
19-
validates :add_psd,
20-
inclusion: {
21-
in: [true, false]
22-
},
23-
if: -> { add_psd.present? }
24-
25-
def add_psd? = add_psd
2624

2725
def triage=(triage)
2826
self.status_and_vaccine_method =
@@ -40,11 +38,14 @@ def triage=(triage)
4038
end
4139

4240
def save
43-
Triage.create!(triage_attributes) if valid?
41+
save! if valid?
4442
end
4543

4644
def save!
47-
Triage.create!(triage_attributes)
45+
ActiveRecord::Base.transaction do
46+
handle_patient_specific_direction
47+
Triage.create!(triage_attributes)
48+
end
4849
end
4950

5051
def safe_to_vaccinate_options
@@ -67,6 +68,11 @@ def consented_to_injection? = consented_vaccine_methods.include?("injection")
6768

6869
def consented_to_injection_only? = consented_vaccine_methods == ["injection"]
6970

71+
def show_add_patient_specific_direction?(option)
72+
session.psd_enabled? && option == "safe_to_vaccinate_nasal" &&
73+
can_create_patient_specific_directions?
74+
end
75+
7076
private
7177

7278
delegate :team, :patient, :session, to: :patient_session
@@ -87,7 +93,7 @@ def triage_attributes
8793
programme:,
8894
status:,
8995
vaccine_method:,
90-
academic_year: session.academic_year
96+
academic_year:
9197
}
9298
end
9399

@@ -113,4 +119,53 @@ def vaccine_method
113119
"nasal"
114120
end
115121
end
122+
123+
def requires_add_patient_specific_direction?
124+
show_add_patient_specific_direction?(status_and_vaccine_method)
125+
end
126+
127+
def can_create_patient_specific_directions?
128+
PatientSpecificDirectionPolicy.new(
129+
current_user,
130+
PatientSpecificDirection
131+
).create?
132+
end
133+
134+
def handle_patient_specific_direction
135+
if add_patient_specific_direction
136+
create_patient_specific_direction!
137+
elsif add_patient_specific_direction == false
138+
destroy_patient_specific_directions!
139+
end
140+
end
141+
142+
def create_patient_specific_direction!
143+
vaccine_method = "nasal"
144+
145+
# TODO: Handle programmes with multiple nasal vaccines.
146+
vaccine = programme.vaccines.find_by(method: vaccine_method)
147+
148+
attributes = {
149+
academic_year:,
150+
delivery_site: "nose",
151+
patient:,
152+
programme:,
153+
vaccine:,
154+
vaccine_method:
155+
}
156+
157+
return if patient.patient_specific_directions.exists?(attributes)
158+
159+
patient.patient_specific_directions.create!(
160+
created_by: current_user,
161+
**attributes
162+
)
163+
end
164+
165+
def destroy_patient_specific_directions!
166+
patient
167+
.patient_specific_directions
168+
.where(academic_year:, programme:)
169+
.destroy_all
170+
end
116171
end

app/models/draft_consent.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class DraftConsent
3232
attribute :recorded_by_user_id, :integer
3333
attribute :response, :string
3434
attribute :route, :string
35+
attribute :triage_add_patient_specific_direction, :boolean
3536
attribute :triage_notes, :string
3637
attribute :triage_status_and_vaccine_method, :string
3738
attribute :vaccine_methods, array: true, default: []
@@ -309,6 +310,8 @@ def write_to!(consent, triage_form:)
309310
consent.academic_year = academic_year if academic_year.present?
310311

311312
if triage_allowed? && response_given?
313+
triage_form.add_patient_specific_direction =
314+
triage_add_patient_specific_direction
312315
triage_form.notes = triage_notes || ""
313316
triage_form.current_user = recorded_by
314317
triage_form.status_and_vaccine_method = triage_status_and_vaccine_method

app/views/patient_sessions/programmes/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<div class="app-grid-column-patient-session">
99
<%= render AppPatientSessionConsentComponent.new(@patient_session, programme: @programme) %>
1010

11-
<%= render AppPatientSessionTriageComponent.new(@patient_session, programme: @programme, triage_form: @triage_form) %>
11+
<%= render AppPatientSessionTriageComponent.new(@patient_session, programme: @programme, current_user:, triage_form: @triage_form) %>
1212

1313
<%= render AppPatientSessionRecordComponent.new(@patient_session, programme: @programme, current_user:, vaccinate_form: @vaccinate_form) %>
1414

config/locales/en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ en:
146146
status_and_vaccine_method:
147147
blank: Choose a status
148148
inclusion: Choose a status
149-
add_psd:
149+
add_patient_specific_direction:
150150
blank: Select yes or no
151151
inclusion: Select yes or no
152152
vaccinate_form:

db/seeds.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ def create_team_sessions(user, team)
261261
create_user(:medical_secretary, team:, email: "admin.hope@example.com")
262262
create_user(:superuser, team:, email: "superuser@example.com")
263263
create_user(:healthcare_assistant, team:, email: "hca@example.com")
264+
create_user(:prescriber, team:, email: "prescriber@example.com")
264265

265266
attach_sample_of_schools_to(team)
266267

0 commit comments

Comments
 (0)