Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/components/app_patient_session_triage_component.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

class AppPatientSessionTriageComponent < ViewComponent::Base
def initialize(patient_session, programme:, triage_form: nil)
def initialize(patient_session, programme:, current_user:, triage_form: nil)
@patient_session = patient_session
@programme = programme
@current_user = current_user
@triage_form = triage_form || default_triage_form
end

Expand All @@ -13,7 +14,7 @@ def render?

private

attr_reader :patient_session, :programme, :triage_form
attr_reader :patient_session, :programme, :current_user, :triage_form

delegate :govuk_button_link_to, to: :helpers
delegate :patient, :session, to: :patient_session
Expand Down Expand Up @@ -50,5 +51,7 @@ def latest_triage
)
end

def default_triage_form = TriageForm.new(patient_session:, programme:)
def default_triage_form
TriageForm.new(patient_session:, programme:, current_user:)
end
end
8 changes: 4 additions & 4 deletions app/components/app_triage_form_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<%= f.govuk_radio_buttons_fieldset :status_and_vaccine_method, **fieldset_options do %>
<% form.safe_to_vaccinate_options.each do |option| %>
<%= f.govuk_radio_button :status_and_vaccine_method, option do %>
<% if show_psd_options?(option) %>
<%= f.govuk_radio_buttons_fieldset :psd_action,
<% if form.show_add_patient_specific_direction?(option) %>
<%= f.govuk_radio_buttons_fieldset :add_patient_specific_direction,
legend: { text: "Do you want to add a PSD?", size: "s" } do %>
<%= f.govuk_radio_button :add_psd, "true", label: { text: "Yes" } %>
<%= f.govuk_radio_button :add_psd, "false", label: { text: "No" } %>
<%= f.govuk_radio_button :add_patient_specific_direction, "true", label: { text: "Yes" } %>
<%= f.govuk_radio_button :add_patient_specific_direction, "false", label: { text: "No" } %>
<% end %>
<% end %>
<% end %>
Expand Down
6 changes: 0 additions & 6 deletions app/components/app_triage_form_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ def initialize(form, url:, method: :post, heading: true, continue: false)

def builder = GOVUKDesignSystemFormBuilder::FormBuilder

def show_psd_options?(option)
patient_session.session.psd_enabled? &&
option == "safe_to_vaccinate_nasal" &&
policy(PatientSpecificDirection).create?
end

def fieldset_options
text = "Is it safe to vaccinate #{patient.given_name}?"
hint =
Expand Down
13 changes: 12 additions & 1 deletion app/controllers/draft_consents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def handle_triage
@triage_form.assign_attributes(triage_form_params)

@draft_consent.assign_attributes(
triage_add_patient_specific_direction:
@triage_form.add_patient_specific_direction,
triage_form_valid: @triage_form.valid?,
triage_notes: @triage_form.notes,
triage_status_and_vaccine_method: @triage_form.status_and_vaccine_method,
Expand Down Expand Up @@ -145,7 +147,13 @@ def update_params
end

def triage_form_params
params.expect(triage_form: %i[status_and_vaccine_method notes])
params.expect(
triage_form: %i[
status_and_vaccine_method
notes
add_patient_specific_direction
]
)
end

def set_draft_consent
Expand Down Expand Up @@ -184,6 +192,9 @@ def set_triage_form
@triage_form =
if policy(Triage).new?
TriageForm.new(
add_patient_specific_direction:
@draft_consent.triage_add_patient_specific_direction,
current_user:,
notes: @draft_consent.triage_notes,
vaccine_methods: @draft_consent.vaccine_methods,
patient_session: @patient_session,
Expand Down
41 changes: 8 additions & 33 deletions app/controllers/patient_sessions/triages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def new

@triage_form =
TriageForm.new(
current_user:,
patient_session: @patient_session,
programme: @programme,
triage: previous_triage
Expand Down Expand Up @@ -46,8 +47,6 @@ def create
)
.each { send_triage_confirmation(@patient_session, @programme, it) }

@triage_form.add_psd? ? ensure_psd_exists : remove_existing_psd

redirect_to redirect_path, flash: { success: "Triage outcome updated" }
else
render "patient_sessions/programmes/show",
Expand All @@ -59,7 +58,13 @@ def create
private

def triage_form_params
params.expect(triage_form: %i[status_and_vaccine_method notes add_psd])
params.expect(
triage_form: %i[
status_and_vaccine_method
notes
add_patient_specific_direction
]
)
end

def redirect_path
Expand All @@ -70,34 +75,4 @@ def redirect_path
return_to: "triage"
)
end

def ensure_psd_exists
# TODO: Handle programmes with multiple nasal vaccines.
vaccine = @programme.vaccines.nasal.first

psd_attributes = {
academic_year: @academic_year,
delivery_site: "nose",
patient: @patient,
programme: @programme,
vaccine:,
vaccine_method: "nasal"
}

return if PatientSpecificDirection.exists?(**psd_attributes)

PatientSpecificDirection.create!(
psd_attributes.merge(created_by: current_user)
)
end

def remove_existing_psd
PatientSpecificDirection.find_by(
{
academic_year: @academic_year,
patient: @patient,
programme: @programme
}
)&.destroy!
end
end
79 changes: 67 additions & 12 deletions app/forms/triage_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ class TriageForm

attr_accessor :patient_session, :programme, :current_user

attribute :status_and_vaccine_method, :string
attribute :add_psd, :boolean
attribute :add_patient_specific_direction, :boolean
attribute :notes, :string
attribute :status_and_vaccine_method, :string
attribute :vaccine_methods, array: true, default: []

validates :add_patient_specific_direction,
inclusion: {
in: [true, false]
},
if: :requires_add_patient_specific_direction?
validates :status_and_vaccine_method,
inclusion: {
in: :status_and_vaccine_method_options
}
validates :notes, length: { maximum: 1000 }
validates :add_psd,
inclusion: {
in: [true, false]
},
if: -> { add_psd.present? }

def add_psd? = add_psd

def triage=(triage)
self.status_and_vaccine_method =
Expand All @@ -40,11 +38,14 @@ def triage=(triage)
end

def save
Triage.create!(triage_attributes) if valid?
save! if valid?
end

def save!
Triage.create!(triage_attributes)
ActiveRecord::Base.transaction do
handle_patient_specific_direction
Triage.create!(triage_attributes)
end
end

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

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

def show_add_patient_specific_direction?(option)
session.psd_enabled? && option == "safe_to_vaccinate_nasal" &&
can_create_patient_specific_directions?
end

private

delegate :team, :patient, :session, to: :patient_session
Expand All @@ -87,7 +93,7 @@ def triage_attributes
programme:,
status:,
vaccine_method:,
academic_year: session.academic_year
academic_year:
}
end

Expand All @@ -113,4 +119,53 @@ def vaccine_method
"nasal"
end
end

def requires_add_patient_specific_direction?
show_add_patient_specific_direction?(status_and_vaccine_method)
end

def can_create_patient_specific_directions?
PatientSpecificDirectionPolicy.new(
current_user,
PatientSpecificDirection
).create?
end

def handle_patient_specific_direction
if add_patient_specific_direction
create_patient_specific_direction!
elsif add_patient_specific_direction == false
destroy_patient_specific_directions!
end
end

def create_patient_specific_direction!
vaccine_method = "nasal"

# TODO: Handle programmes with multiple nasal vaccines.
vaccine = programme.vaccines.find_by(method: vaccine_method)

attributes = {
academic_year:,
delivery_site: "nose",
patient:,
programme:,
vaccine:,
vaccine_method:
}

return if patient.patient_specific_directions.exists?(attributes)

patient.patient_specific_directions.create!(
created_by: current_user,
**attributes
)
end

def destroy_patient_specific_directions!
patient
.patient_specific_directions
.where(academic_year:, programme:)
.destroy_all
end
end
3 changes: 3 additions & 0 deletions app/models/draft_consent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class DraftConsent
attribute :recorded_by_user_id, :integer
attribute :response, :string
attribute :route, :string
attribute :triage_add_patient_specific_direction, :boolean
attribute :triage_notes, :string
attribute :triage_status_and_vaccine_method, :string
attribute :vaccine_methods, array: true, default: []
Expand Down Expand Up @@ -309,6 +310,8 @@ def write_to!(consent, triage_form:)
consent.academic_year = academic_year if academic_year.present?

if triage_allowed? && response_given?
triage_form.add_patient_specific_direction =
triage_add_patient_specific_direction
triage_form.notes = triage_notes || ""
triage_form.current_user = recorded_by
triage_form.status_and_vaccine_method = triage_status_and_vaccine_method
Expand Down
2 changes: 1 addition & 1 deletion app/views/patient_sessions/programmes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="app-grid-column-patient-session">
<%= render AppPatientSessionConsentComponent.new(@patient_session, programme: @programme) %>

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

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

Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ en:
status_and_vaccine_method:
blank: Choose a status
inclusion: Choose a status
add_psd:
add_patient_specific_direction:
blank: Select yes or no
inclusion: Select yes or no
vaccinate_form:
Expand Down
1 change: 1 addition & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ def create_team_sessions(user, team)
create_user(:medical_secretary, team:, email: "admin.hope@example.com")
create_user(:superuser, team:, email: "superuser@example.com")
create_user(:healthcare_assistant, team:, email: "hca@example.com")
create_user(:prescriber, team:, email: "prescriber@example.com")

attach_sample_of_schools_to(team)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
describe AppPatientSessionTriageComponent do
subject { render_inline(component) }

let(:component) { described_class.new(patient_session, programme:) }
let(:component) do
described_class.new(patient_session, programme:, current_user:)
end

let(:programme) { create(:programme) }
let(:session) { create(:session, programmes: [programme]) }
let(:patient_session) { create(:patient_session, session:) }
let(:patient) { patient_session.patient }
let(:current_user) { create(:nurse) }

before do
patient_session.reload.strict_loading!(false)
Expand Down
Loading