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
10 changes: 8 additions & 2 deletions app/components/app_patient_session_record_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def render?
(
patient_session.registration_status&.attending? ||
patient_session.registration_status&.completed? ||
!patient_session.session.requires_registration?
!session.requires_registration?
)
end

Expand All @@ -35,8 +35,14 @@ def render?

def default_vaccinate_form
pre_screening_confirmed = patient.pre_screenings.today.exists?(programme:)
session_date = session.session_dates.today.first

VaccinateForm.new(patient_session:, programme:, pre_screening_confirmed:)
VaccinateForm.new(
patient:,
session_date:,
programme:,
pre_screening_confirmed:
)
end

def heading
Expand Down
3 changes: 1 addition & 2 deletions app/components/app_vaccinate_form_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ def initialize(vaccinate_form)

attr_reader :vaccinate_form

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

def url
Expand Down
9 changes: 4 additions & 5 deletions app/controllers/api/testing/teams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ def destroy

patient_ids = team.patients.pluck(:id)

patient_sessions = PatientSession.where(session: sessions)
log_destroy(PreScreening.where(patient_session: patient_sessions))
patient_sessions.in_batches { log_destroy(it) }

log_destroy(SessionDate.where(session: sessions))
log_destroy(PatientSession.where(session: sessions))

log_destroy(AccessLogEntry.where(patient_id: patient_ids))
log_destroy(ArchiveReason.where(patient_id: patient_ids))
Expand All @@ -41,11 +37,14 @@ def destroy
# In local dev we can end up with NotifyLogEntries without a patient
log_destroy(NotifyLogEntry.where(patient_id: nil))
log_destroy(NotifyLogEntry.where(patient_id: patient_ids))
log_destroy(PreScreening.where(patient_id: patient_ids))
log_destroy(SchoolMove.where(patient_id: patient_ids))
log_destroy(SchoolMove.where(team:))
log_destroy(SchoolMoveLogEntry.where(patient_id: patient_ids))
log_destroy(VaccinationRecord.where(patient_id: patient_ids))

log_destroy(SessionDate.where(session: sessions))

log_destroy(ConsentForm.where(team:))
log_destroy(Consent.where(team:))
log_destroy(Triage.where(team:))
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/patient_sessions/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def set_session
)
end

def set_session_date
@session_date = @session.session_dates.find_by!(value: Date.current)
end

def set_academic_year
@academic_year = @session.academic_year
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ def update

private

def set_session_date
@session_date = @session.session_dates.find_by!(value: Date.current)
end

def set_session_attendance
@session_attendance =
authorize @patient_session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class PatientSessions::VaccinationsController < PatientSessions::BaseController
include VaccinationMailerConcern

before_action :set_todays_batch
before_action :set_session_date

after_action :verify_authorized

Expand All @@ -17,7 +18,8 @@ def create
@vaccinate_form =
VaccinateForm.new(
current_user:,
patient_session: @patient_session,
patient: @patient,
session_date: @session_date,
programme: @programme,
todays_batch: @todays_batch,
**vaccinate_form_params
Expand Down
19 changes: 13 additions & 6 deletions app/forms/vaccinate_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ class VaccinateForm
include ActiveModel::Model
include ActiveModel::Attributes

attr_accessor :patient_session, :programme, :current_user, :todays_batch
attr_accessor :patient,
:session_date,
:programme,
:current_user,
:todays_batch

delegate :session, to: :session_date

attribute :identity_check_confirmed_by_other_name, :string
attribute :identity_check_confirmed_by_other_relationship, :string
Expand Down Expand Up @@ -90,19 +96,19 @@ def save(draft_vaccination_record:)
identity_check_confirmed_by_patient
draft_vaccination_record.location_id =
session.location_id unless session.generic_clinic?
draft_vaccination_record.patient_id = patient_session.patient_id
draft_vaccination_record.patient_id = patient.id
draft_vaccination_record.performed_at = Time.current
draft_vaccination_record.performed_by_user = current_user
draft_vaccination_record.performed_ods_code = organisation.ods_code
draft_vaccination_record.programme = programme
draft_vaccination_record.session_id = patient_session.session_id
draft_vaccination_record.session_id = session.id

draft_vaccination_record.save # rubocop:disable Rails/SaveBang
end

private

delegate :organisation, :session, to: :patient_session
delegate :organisation, to: :session

def administered? = vaccine_method != "none"

Expand Down Expand Up @@ -131,10 +137,11 @@ def delivery_method

def pre_screening
@pre_screening ||=
patient_session.pre_screenings.build(
patient.pre_screenings.build(
notes: pre_screening_notes,
performed_by: current_user,
programme:
programme:,
session_date:
)
end
end
18 changes: 8 additions & 10 deletions app/lib/patient_merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def call
patient_id: patient_to_keep.id
)

patient_to_destroy.pre_screenings.update_all(
patient_id: patient_to_keep.id
)

patient_to_destroy.school_moves.find_each do |school_move|
if patient_to_keep.school_moves.exists?(
home_educated: school_move.home_educated,
Expand Down Expand Up @@ -78,18 +82,12 @@ def call
end

patient_to_destroy.patient_sessions.each do |patient_session|
if (
existing_patient_session =
patient_to_keep.patient_sessions.find_by(
session_id: patient_session.session_id
)
if patient_to_keep.patient_sessions.exists?(
session_id: patient_session.session_id
)
patient_session.pre_screenings.update_all(
patient_session_id: existing_patient_session.id
)
else
patient_session.update!(patient: patient_to_keep)
next
end
patient_session.update!(patient: patient_to_keep)
end

PatientSession.where(patient: patient_to_destroy).destroy_all
Expand Down
2 changes: 1 addition & 1 deletion app/models/patient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Patient < ApplicationRecord
has_many :parent_relationships, -> { order(:created_at) }
has_many :patient_sessions
has_many :pds_search_results
has_many :pre_screenings
has_many :school_move_log_entries
has_many :school_moves
has_many :session_notifications
Expand All @@ -81,7 +82,6 @@ class Patient < ApplicationRecord
has_many :gillick_assessments
has_many :parents, through: :parent_relationships
has_many :patient_specific_directions
has_many :pre_screenings, through: :patient_sessions
has_many :session_attendances, through: :patient_sessions
has_many :sessions, through: :patient_sessions
has_many :teams, -> { distinct }, through: :sessions
Expand Down
5 changes: 4 additions & 1 deletion app/models/patient_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class PatientSession < ApplicationRecord
belongs_to :patient
belongs_to :session

has_many :pre_screenings
has_many :session_attendances, dependent: :destroy
has_one :registration_status

Expand All @@ -64,6 +63,10 @@ class PatientSession < ApplicationRecord
through: :patient,
source: :notes

has_many :pre_screenings,
-> { where(patient_id: it.patient_id) },
through: :session

has_many :session_notifications,
-> { where(session_id: it.session_id) },
through: :patient
Expand Down
18 changes: 10 additions & 8 deletions app/models/pre_screening.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,36 @@
# notes :text default(""), not null
# created_at :datetime not null
# updated_at :datetime not null
# patient_session_id :bigint not null
# patient_id :bigint not null
# performed_by_user_id :bigint not null
# programme_id :bigint not null
# session_date_id :bigint not null
#
# Indexes
#
# index_pre_screenings_on_patient_session_id (patient_session_id)
# index_pre_screenings_on_patient_id (patient_id)
# index_pre_screenings_on_performed_by_user_id (performed_by_user_id)
# index_pre_screenings_on_programme_id (programme_id)
# index_pre_screenings_on_session_date_id (session_date_id)
#
# Foreign Keys
#
# fk_rails_... (patient_session_id => patient_sessions.id)
# fk_rails_... (patient_id => patients.id)
# fk_rails_... (performed_by_user_id => users.id)
# fk_rails_... (programme_id => programmes.id)
# fk_rails_... (session_date_id => session_dates.id)
#
class PreScreening < ApplicationRecord
audited associated_with: :patient_session
audited associated_with: :patient

belongs_to :patient_session
belongs_to :patient
belongs_to :session_date
belongs_to :programme
belongs_to :performed_by,
class_name: "User",
foreign_key: :performed_by_user_id

has_one :patient, through: :patient_session

scope :today, -> { where(created_at: Date.current.all_day) }
scope :today, -> { joins(:session_date).merge(SessionDate.today) }

encrypts :notes

Expand Down
1 change: 1 addition & 0 deletions app/models/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Session < ApplicationRecord

has_one :organisation, through: :team
has_one :subteam, through: :location
has_many :pre_screenings, through: :session_dates
has_many :programmes, through: :session_programmes
has_many :gillick_assessments, through: :session_dates
has_many :patients, through: :patient_sessions
Expand Down
3 changes: 2 additions & 1 deletion app/models/session_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SessionDate < ApplicationRecord
belongs_to :session

has_many :gillick_assessments, dependent: :restrict_with_error
has_many :pre_screenings, dependent: :restrict_with_error
has_many :session_attendances, dependent: :restrict_with_error

scope :for_session, -> { where("session_id = sessions.id") }
Expand All @@ -44,7 +45,7 @@ def today_or_past? = today? || past?
def today_or_future? = today? || future?

def has_been_attended?
gillick_assessments.any? || session_attendances.any?
gillick_assessments.any? || pre_screenings.any? || session_attendances.any?
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

class RemovePatientSessionFromPreScreenings < ActiveRecord::Migration[8.0]
def up
change_table :pre_screenings, bulk: true do |t|
t.references :patient, foreign_key: true
t.references :session_date, foreign_key: true
end

PreScreening.find_each do |pre_screening|
patient_session = PatientSession.find(pre_screening.patient_session_id)
patient_id = patient_session.patient_id
session_id = patient_session.session_id
session_date_id =
SessionDate.find_by!(
session_id:,
value: pre_screening.created_at.to_date
)
pre_screening.update_columns(patient_id:, session_date_id:)
end

change_table :pre_screenings, bulk: true do |t|
t.change_null :patient_id, false
t.change_null :session_date_id, false
t.remove_references :patient_session
end
end

def down
add_reference :pre_screenings, :patient_session

PreScreening.find_each do |pre_screening|
session_id = SessionDate.find(pre_screening.session_date_id).session_id
patient_session =
PatientSession.find_by!(
patient_id: pre_screening.patient_id,
session_id:
)
pre_screening.update_column(:patient_session_id, patient_session.id)
end

change_table :pre_screenings, bulk: true do |t|
t.change_null :patient_session_id, false
t.remove_references :patient, :session_date
end
end
end
9 changes: 6 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -722,15 +722,17 @@
end

create_table "pre_screenings", force: :cascade do |t|
t.bigint "patient_session_id", null: false
t.bigint "performed_by_user_id", null: false
t.text "notes", default: "", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "programme_id", null: false
t.index ["patient_session_id"], name: "index_pre_screenings_on_patient_session_id"
t.bigint "patient_id", null: false
t.bigint "session_date_id", null: false
t.index ["patient_id"], name: "index_pre_screenings_on_patient_id"
t.index ["performed_by_user_id"], name: "index_pre_screenings_on_performed_by_user_id"
t.index ["programme_id"], name: "index_pre_screenings_on_programme_id"
t.index ["session_date_id"], name: "index_pre_screenings_on_session_date_id"
end

create_table "programmes", force: :cascade do |t|
Expand Down Expand Up @@ -1072,8 +1074,9 @@
add_foreign_key "patients", "locations", column: "gp_practice_id"
add_foreign_key "patients", "locations", column: "school_id"
add_foreign_key "pds_search_results", "patients"
add_foreign_key "pre_screenings", "patient_sessions"
add_foreign_key "pre_screenings", "patients"
add_foreign_key "pre_screenings", "programmes"
add_foreign_key "pre_screenings", "session_dates"
add_foreign_key "pre_screenings", "users", column: "performed_by_user_id"
add_foreign_key "reporting_api_one_time_tokens", "users"
add_foreign_key "school_move_log_entries", "locations", column: "school_id"
Expand Down
3 changes: 2 additions & 1 deletion spec/components/app_activity_log_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@
create(
:pre_screening,
performed_by: user,
patient_session:,
patient:,
session: patient_session.session,
notes: "Some notes",
created_at: Time.zone.local(2025, 6, 1, 12)
)
Expand Down
Loading