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
23 changes: 10 additions & 13 deletions app/components/app_activity_log_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ def initialize(team:, patient:, session: nil)
session ? scope.where(programme_ids: session.programmes.ids) : scope
end

@patient_sessions =
@patient_locations =
@patient
.patient_sessions
.includes_programmes
.includes(session: :location)
.then { |scope| session ? scope.where(session:) : scope }
.patient_locations
.includes(:location)
.then do |scope|
session ? scope.where(location: session.location) : scope
end

@patient_specific_directions =
@patient
Expand Down Expand Up @@ -105,7 +106,7 @@ def initialize(team:, patient:, session: nil)
:notes,
:notify_log_entries,
:patient,
:patient_sessions,
:patient_locations,
:patient_specific_directions,
:pre_screenings,
:attendance_records,
Expand Down Expand Up @@ -346,15 +347,11 @@ def pre_screening_events
end

def session_events
patient_sessions.map do |patient_session|
patient = patient_session.patient
session = patient_session.session

patient_locations.map do |patient_location|
[
{
title: "Added to the session at #{session.location.name}",
at: patient_session.created_at,
programmes: session.programmes_for(patient:)
title: "Added to the session at #{patient_location.location.name}",
at: patient_location.created_at
}
]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ def initialize(discrepancies:, current_user:)

delegate :format_nhs_number, :govuk_table, to: :helpers

def can_link_to?(record)
allowed_ids.include?(record.id)
end
def can_link_to?(record) = allowed_ids.include?(record.id)

def allowed_ids
@allowed_ids ||= PatientPolicy::Scope.new(current_user, Patient).resolve.ids
Expand Down
2 changes: 1 addition & 1 deletion app/components/app_patient_programmes_table_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def vaccination_records_for(programme:, academic_year: nil)
end

def eligible_year_groups_for(programme:)
location_ids = patient.patient_sessions.joins(:session).select(:location_id)
location_ids = patient.patient_locations.select(:location_id)

LocationProgrammeYearGroup
.where(location_id: location_ids)
Expand Down
40 changes: 28 additions & 12 deletions app/components/app_programme_session_table_component.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
# frozen_string_literal: true

class AppProgrammeSessionTableComponent < ViewComponent::Base
def initialize(sessions, programme:)
def initialize(sessions, programme:, academic_year:)
@sessions = sessions
@programme = programme
@academic_year = academic_year
end

private

attr_reader :sessions, :programme
attr_reader :sessions, :programme, :academic_year

delegate :govuk_table, to: :helpers

def cohort_count(session:)
format_number(patient_sessions(session:).count)
format_number(patients(session:).count)
end

def no_response_scope(session:)
patient_sessions(session:).has_consent_status(:no_response, programme:)
patients(session:).has_consent_status(
:no_response,
programme:,
academic_year:
)
end

def no_response_count(session:)
Expand All @@ -27,13 +32,17 @@ def no_response_count(session:)
def no_response_percentage(session:)
format_percentage(
no_response_scope(session:).count,
patient_sessions(session:).count
patients(session:).count
)
end

def triage_needed_count(session:)
format_number(
patient_sessions(session:).has_triage_status(:required, programme:).count
patients(session:).has_triage_status(
:required,
programme:,
academic_year:
).count
)
end

Expand All @@ -48,15 +57,22 @@ def vaccinated_count(session:)
def vaccinated_percentage(session:)
format_percentage(
vaccinated_scope(session:).count,
patient_sessions(session:).count
patients(session:).count
)
end

def patients(session:)
@patients ||= {}
@patients[session] = session.patients.where(
birth_academic_year: birth_academic_years(session:)
)
end

def patient_sessions(session:)
session
.patient_sessions
.joins(:patient, :session)
.appear_in_programmes([programme])
def birth_academic_years(session:)
@birth_academic_years ||= {}
@birth_academic_years[session] = session.programme_birth_academic_years[
programme
]
end

def format_number(count) = count.to_s
Expand Down
41 changes: 23 additions & 18 deletions app/components/app_session_actions_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ def render? = rows.any?
delegate :govuk_summary_list, to: :helpers
delegate :academic_year, :programmes, to: :session

def patient_sessions
session
.patient_sessions
.joins(:patient, :session)
.appear_in_programmes(programmes)
end
def patients = session.patients

def rows
@rows ||= [
Expand All @@ -38,7 +33,7 @@ def rows
end

def no_nhs_number_row
count = patient_sessions.merge(Patient.without_nhs_number).count
count = patients.without_nhs_number.count
href = session_patients_path(session, missing_nhs_number: true)

generate_row(:children_without_nhs_number, count:, href:)
Expand All @@ -47,7 +42,11 @@ def no_nhs_number_row
def no_consent_response_row
status = "no_response"
count =
patient_sessions.has_consent_status(status, programme: programmes).count
patients.has_consent_status(
status,
programme: programmes,
academic_year:
).count
href = session_consent_path(session, consent_statuses: [status])
actions = [
{
Expand All @@ -61,7 +60,11 @@ def no_consent_response_row
def conflicting_consent_row
status = "conflicts"
count =
patient_sessions.has_consent_status(status, programme: programmes).count
patients.has_consent_status(
status,
programme: programmes,
academic_year:
).count
href = session_consent_path(session, consent_statuses: [status])

generate_row(:children_with_conflicting_consent_response, count:, href:)
Expand All @@ -70,7 +73,11 @@ def conflicting_consent_row
def triage_required_row
status = "required"
count =
patient_sessions.has_triage_status(status, programme: programmes).count
patients.has_triage_status(
status,
programme: programmes,
academic_year:
).count
href = session_triage_path(session, triage_status: status)

generate_row(:children_requiring_triage, count:, href:)
Expand All @@ -80,7 +87,7 @@ def register_attendance_row
return nil unless session.requires_registration? && session.today?

status = "unknown"
count = patient_sessions.has_registration_status(status).count
count = patients.has_registration_status(status, session:).count
href = session_register_path(session, register_status: status)

generate_row(:children_to_register, count:, href:)
Expand All @@ -91,13 +98,11 @@ def ready_for_vaccinator_row

counts_by_programme =
session.programmes.index_with do |programme|
patient_sessions
.has_registration_status(%w[attending completed])
.includes(
patient: %i[consent_statuses triage_statuses vaccination_statuses]
)
.count do |patient_session|
patient_session.patient.consent_given_and_safe_to_vaccinate?(
patients
.has_registration_status(%w[attending completed], session:)
.includes(:consent_statuses, :triage_statuses, :vaccination_statuses)
.count do |patient|
patient.consent_given_and_safe_to_vaccinate?(
programme:,
academic_year:
)
Expand Down
17 changes: 8 additions & 9 deletions app/components/app_session_details_summary_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@ def call
attr_reader :session

delegate :govuk_summary_list, to: :helpers
delegate :programmes, to: :session
delegate :programmes, :academic_year, to: :session

def patient_sessions
session
.patient_sessions
.joins(:patient, :session)
.appear_in_programmes(programmes)
end
def patients = session.patients

def cohort_row
count = patient_sessions.count
count = patients.count

{ key: { text: "Cohort" }, value: { text: I18n.t("children", count:) } }
end
Expand All @@ -33,7 +28,11 @@ def consent_refused_row
status = "refused"

count =
patient_sessions.has_consent_status(status, programme: programmes).count
patients.has_consent_status(
status,
programme: programmes,
academic_year:
).count

href = session_consent_path(session, consent_statuses: [status])

Expand Down
10 changes: 2 additions & 8 deletions app/components/app_session_needs_review_warning_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def warning_href

def warning_counts
@warning_counts ||= {
children_without_nhs_number:
patient_sessions.merge(Patient.without_nhs_number).count
children_without_nhs_number: patients.without_nhs_number.count
}
end

Expand All @@ -42,10 +41,5 @@ def make_row_from_warning(warning)
link_to(t(warning, count: warning_counts[warning]), warning_href[warning])
end

def patient_sessions
@session
.patient_sessions
.joins(:patient, :session)
.appear_in_programmes(@session.programmes)
end
def patients = @session.patients
end
6 changes: 4 additions & 2 deletions app/controllers/api/testing/teams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def destroy
patient_ids = team.patients.pluck(:id)
consent_form_ids = team.consent_forms.pluck(:id)

log_destroy(PatientSession.where(session: sessions))
log_destroy(
PatientLocation.where(location_id: sessions.select(:location_id))
)

log_destroy(AccessLogEntry.where(patient_id: patient_ids))
log_destroy(ArchiveReason.where(patient_id: patient_ids))
Expand All @@ -41,7 +43,7 @@ def destroy
log_destroy(NotifyLogEntry.where(patient_id: patient_ids))
log_destroy(NotifyLogEntry.where(consent_form_id: consent_form_ids))
log_destroy(PatientChangeset.where(patient_id: patient_ids))
log_destroy(PatientSession.where(patient_id: patient_ids))
log_destroy(PatientLocation.where(patient_id: patient_ids))
log_destroy(PatientSpecificDirection.where(patient_id: patient_ids))
log_destroy(PDSSearchResult.where(patient_id: patient_ids))
log_destroy(PreScreening.where(patient_id: patient_ids))
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/consent_forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ def update_match

session =
@patient
.pending_sessions
.sessions
.includes(:location_programme_year_groups, :programmes)
.has_programmes(@consent_form.programmes)
.first || @consent_form.original_session
.find_by(academic_year: AcademicYear.pending) ||
@consent_form.original_session

programme = session.programmes_for(patient: @patient).first

Expand Down Expand Up @@ -136,7 +137,6 @@ def set_patient
@patient =
policy_scope(Patient).includes(
parent_relationships: :parent,
pending_sessions: :programmes,
vaccination_records: :programme
).find(params[:patient_id])
end
Expand Down
1 change: 0 additions & 1 deletion app/controllers/imports/issues_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def set_import_issues
@patients =
policy_scope(Patient).with_pending_changes.includes(
:gp_practice,
:pending_sessions,
:school,
:school_moves
)
Expand Down
12 changes: 8 additions & 4 deletions app/controllers/patient_sessions/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class PatientSessions::BaseController < ApplicationController
before_action :set_session
before_action :set_academic_year
before_action :set_patient
before_action :set_patient_session
before_action :set_patient_location
before_action :set_programme
before_action :set_breadcrumb_item

Expand Down Expand Up @@ -34,9 +34,13 @@ def set_patient
)
end

def set_patient_session
@patient_session =
PatientSession.find_by!(patient: @patient, session: @session)
def set_patient_location
@patient_location =
PatientLocation.find_by!(
patient: @patient,
location: @session.location,
academic_year: @session.academic_year
)
end

def set_programme
Expand Down
Loading