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
21 changes: 15 additions & 6 deletions app/components/app_patient_session_record_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

class AppPatientSessionRecordComponent < ViewComponent::Base
erb_template <<-ERB
<h3 class="nhsuk-heading-m"><%= heading %></h3>

<% if helpers.policy(VaccinationRecord).new? %>
<% if helpers.policy(vaccination_record).new? %>
<h3 class="nhsuk-heading-m"><%= heading %></h3>
<%= render AppVaccinateFormComponent.new(vaccinate_form) %>
<% end %>
ERB

def initialize(patient_session, programme:, vaccinate_form: nil)
def initialize(patient_session, programme:, current_user:, vaccinate_form:)
super

@patient_session = patient_session
@programme = programme
@current_user = current_user
@vaccinate_form = vaccinate_form || default_vaccinate_form
end

Expand All @@ -28,15 +28,24 @@ def render?

private

attr_reader :patient_session, :programme, :vaccinate_form
attr_reader :patient_session, :current_user, :programme, :vaccinate_form

delegate :patient, :session, to: :patient_session
delegate :academic_year, to: :session

def vaccination_record
VaccinationRecord.new(patient:, session:, programme:)
end

def default_vaccinate_form
pre_screening_confirmed = patient.pre_screenings.today.exists?(programme:)

VaccinateForm.new(patient_session:, programme:, pre_screening_confirmed:)
VaccinateForm.new(
current_user:,
patient_session:,
programme:,
pre_screening_confirmed:
)
end

def heading
Expand Down
16 changes: 15 additions & 1 deletion app/components/app_vaccinate_form_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= form_with(
model: vaccinate_form,
model: form,
url:,
method: :post,
class: "nhsuk-card",
Expand Down Expand Up @@ -54,6 +54,20 @@
<% end %>

<%= f.govuk_text_area :pre_screening_notes, label: { text: "Pre-screening notes (optional)" }, rows: 3 %>

<% if form.requires_supplied_by_user_id? %>
<%= f.govuk_select :supplied_by_user_id,
label: { text: "Which nurse identified and pre-screened the child and supplied the vaccine?" },
data: { module: "autocomplete" } do %>
<%= tag.option "", value: "" %>
<% form.supplied_by_users.each do |user| %>
<%= tag.option user.full_name,
value: user.id,
selected: user.id == form.supplied_by_user_id,
data: { hint: user.email } %>
<% end %>
<% end %>
<% end %>
</section>

<hr class="nhsuk-section-break nhsuk-section-break--visible nhsuk-section-break--l">
Expand Down
8 changes: 4 additions & 4 deletions app/components/app_vaccinate_form_component.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# frozen_string_literal: true

class AppVaccinateFormComponent < ViewComponent::Base
def initialize(vaccinate_form)
def initialize(form)
super

@vaccinate_form = vaccinate_form
@form = form
end

private

attr_reader :vaccinate_form
attr_reader :form

delegate :patient_session, :programme, to: :vaccinate_form
delegate :patient_session, :programme, to: :form
delegate :patient, :session, to: :patient_session
delegate :academic_year, to: :session

Expand Down
21 changes: 21 additions & 0 deletions app/components/app_vaccination_record_summary_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,20 @@ def call
end
end

if @vaccination_record.supplied_by.present?
summary_list.with_row do |row|
row.with_key { "Supplier" }
row.with_value { supplier_value }
if (href = @change_links[:supplier])
row.with_action(
text: "Change",
visually_hidden_text: "supplier",
href:
)
end
end
end

if @vaccination_record.performed_by.present?
summary_list.with_row do |row|
row.with_key { "Vaccinator" }
Expand Down Expand Up @@ -331,6 +345,13 @@ def time_value
)
end

def supplier_value
highlight_if(
@vaccination_record.supplied_by&.full_name,
@vaccination_record.supplied_by_user_id_changed?
)
end

def vaccinator_value
value =
if @vaccination_record.performed_by == @current_user
Expand Down
15 changes: 13 additions & 2 deletions app/controllers/draft_vaccination_records_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DraftVaccinationRecordsController < ApplicationController
before_action :validate_params, only: :update
before_action :set_batches, if: -> { current_step == :batch }
before_action :set_locations, if: -> { current_step == :location }
before_action :set_supplied_by_users, if: -> { current_step == :supplier }
before_action :set_back_link_path

after_action :verify_authorized
Expand Down Expand Up @@ -165,7 +166,8 @@ def update_params
],
location: %i[location_name],
notes: %i[notes],
outcome: %i[outcome]
outcome: %i[outcome],
supplier: %i[supplied_by_user_id]
}.fetch(current_step)

params
Expand Down Expand Up @@ -193,7 +195,12 @@ def set_programme

def set_vaccination_record
@vaccination_record =
@draft_vaccination_record.vaccination_record || VaccinationRecord.new
@draft_vaccination_record.vaccination_record ||
VaccinationRecord.new(
patient: @patient,
session: @session,
programme: @programme
)
end

def set_steps
Expand Down Expand Up @@ -226,6 +233,10 @@ def set_locations
@locations = policy_scope(Location).community_clinic
end

def set_supplied_by_users
@supplied_by_users = current_team.users.show_in_suppliers
end

def set_back_link_path
@back_link_path =
if @draft_vaccination_record.editing?
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/patient_sessions/programmes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ def show
end

def record_already_vaccinated
unless @patient_session.can_record_as_already_vaccinated?(
programme: @programme
)
redirect_to session_patient_path and return
end
authorize VaccinationRecord.new(
patient: @patient,
session: @session,
programme: @programme
)

draft_vaccination_record =
DraftVaccinationRecord.new(request_session: session, current_user:)
Expand Down
12 changes: 9 additions & 3 deletions app/controllers/patient_sessions/vaccinations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class PatientSessions::VaccinationsController < PatientSessions::BaseController
after_action :verify_authorized

def create
authorize VaccinationRecord
authorize VaccinationRecord.new(
patient: @patient,
session: @session,
programme: @programme
)

draft_vaccination_record =
DraftVaccinationRecord.new(request_session: session, current_user:)
Expand All @@ -26,9 +30,10 @@ def create
if @vaccinate_form.save(draft_vaccination_record:)
steps = draft_vaccination_record.wizard_steps

steps.delete(:notes) # this is on the confirmation page
steps.delete(:identity) # this can only be changed from confirmation page
steps.delete(:dose) # this can only be changed from confirmation page
steps.delete(:identity) # this can only be changed from confirmation page
steps.delete(:notes) # this is on the confirmation page
steps.delete(:supplier) # this can only be changed from confirmation page

steps.delete(:date_and_time)
steps.delete(:outcome) if draft_vaccination_record.administered?
Expand Down Expand Up @@ -63,6 +68,7 @@ def vaccinate_form_params
identity_check_confirmed_by_patient
pre_screening_confirmed
pre_screening_notes
supplied_by_user_id
vaccine_id
vaccine_method
]
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/sessions/record_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ def show
scope = scope.has_registration_status(%w[attending completed])
end

@vaccine_methods = @session.vaccine_methods_for(user: current_user)

if @vaccine_methods != @session.vaccine_methods
scope =
if @vaccine_methods.empty?
scope.none
else
@vaccine_methods.reduce(scope) do |accumulator, vaccine_method|
accumulator.has_vaccine_method(
vaccine_method,
programme: @session.programmes
)
end
end
end

patient_sessions =
@form.apply(scope).consent_given_and_ready_to_vaccinate(
programmes: @form.programmes,
Expand Down
21 changes: 19 additions & 2 deletions app/forms/vaccinate_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class VaccinateForm
attribute :pre_screening_confirmed, :boolean
attribute :pre_screening_notes, :string

attribute :supplied_by_user_id, :integer

attribute :vaccine_method, :string
attribute :delivery_site, :string
attribute :dose_sequence, :integer
Expand All @@ -31,10 +33,16 @@ class VaccinateForm
maximum: 300
}

validates :vaccine_method, inclusion: { in: :vaccine_method_options }
validates :pre_screening_notes, length: { maximum: 1000 }

validates :pre_screening_confirmed, presence: true, if: :administered?

validates :supplied_by_user_id,
inclusion: {
in: :supplied_by_user_id_values
},
if: :requires_supplied_by_user_id?

validates :vaccine_method, inclusion: { in: :vaccine_method_options }
validates :delivery_site,
inclusion: {
in: :delivery_site_options
Expand All @@ -59,6 +67,12 @@ def delivery_site
super
end

def supplied_by_users
current_user.selected_team.users.show_in_suppliers
end

def requires_supplied_by_user_id? = !current_user.show_in_suppliers

def save(draft_vaccination_record:)
return nil if invalid?

Expand Down Expand Up @@ -93,6 +107,7 @@ def save(draft_vaccination_record:)
draft_vaccination_record.patient_id = patient_session.patient_id
draft_vaccination_record.performed_at = Time.current
draft_vaccination_record.performed_by_user = current_user
draft_vaccination_record.supplied_by_user_id = supplied_by_user_id
draft_vaccination_record.performed_ods_code = organisation.ods_code
draft_vaccination_record.programme = programme
draft_vaccination_record.session_id = patient_session.session_id
Expand All @@ -106,6 +121,8 @@ def save(draft_vaccination_record:)

def administered? = vaccine_method != "none"

def supplied_by_user_id_values = supplied_by_users.pluck(:id)

def vaccine_method_options
programme.vaccine_methods + ["none"]
end
Expand Down
29 changes: 25 additions & 4 deletions app/models/draft_vaccination_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class DraftVaccinationRecord
attribute :delivery_method, :string
attribute :delivery_site, :string
attribute :dose_sequence, :integer
attribute :first_active_wizard_step, :string
attribute :full_dose, :boolean
attribute :protocol, :string
attribute :identity_check_confirmed_by_other_name, :string
attribute :identity_check_confirmed_by_other_relationship, :string
attribute :identity_check_confirmed_by_patient, :boolean
Expand All @@ -27,8 +27,9 @@ class DraftVaccinationRecord
attribute :performed_by_user_id, :integer
attribute :performed_ods_code, :string
attribute :programme_id, :integer
attribute :protocol, :string
attribute :session_id, :integer
attribute :first_active_wizard_step, :string
attribute :supplied_by_user_id, :integer

def initialize(current_user:, **attributes)
@current_user = current_user
Expand All @@ -47,6 +48,7 @@ def wizard_steps
:notes,
:date_and_time,
(:outcome if can_change_outcome?),
(:supplier if requires_supplied_by?),
(:delivery if administered?),
(:dose if administered? && can_be_half_dose?),
(:batch if administered?),
Expand Down Expand Up @@ -187,13 +189,27 @@ def programme=(value)
def session
return nil if session_id.nil?

SessionPolicy::Scope.new(@current_user, Session).resolve.find(session_id)
SessionPolicy::Scope
.new(@current_user, Session)
.resolve
.includes(:programmes)
.find(session_id)
end

def session=(value)
self.session_id = value.id
end

def supplied_by
return nil if supplied_by_user_id.nil?

User.find(supplied_by_user_id)
end

def supplied_by=(value)
self.supplied_by_user_id = value.id
end

def vaccination_record
return nil if editing_id.nil?

Expand Down Expand Up @@ -272,7 +288,6 @@ def writable_attribute_names
delivery_site
dose_sequence
full_dose
protocol
identity_check
location_id
location_name
Expand All @@ -285,7 +300,9 @@ def writable_attribute_names
performed_by_user_id
performed_ods_code
programme_id
protocol
session_id
supplied_by_user_id
vaccine_id
]
end
Expand Down Expand Up @@ -344,6 +361,10 @@ def can_change_outcome?
outcome != "already_had" || editing? || session.nil? || session.today?
end

def requires_supplied_by?
performed_by_user && !performed_by_user&.show_in_suppliers
end

def delivery_site_matches_delivery_method
return if delivery_method.blank?

Expand Down
Loading