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
1 change: 1 addition & 0 deletions app/controllers/consents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def index
all_patient_sessions =
@session
.patient_sessions
.active
.strict_loading
.includes(
:campaign,
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/sessions/edit_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def update
case current_step
when :confirm
@session.draft = false
@session.patient_sessions.update_all(active: true)

if @session.send_consent_at.today?
ConsentRequestsSessionBatchJob.perform_later(@session)
end
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/triage_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def index
all_patient_sessions =
@session
.patient_sessions
.active
.strict_loading
.includes(
:campaign,
Expand Down Expand Up @@ -74,7 +75,8 @@ def set_patient
end

def set_patient_session
@patient_session = @patient.patient_sessions.find_by(session: @session)
@patient_session =
@patient.patient_sessions.active.find_by(session: @session)
end

def set_triage
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/vaccinations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def index
all_patient_sessions =
@session
.patient_sessions
.active
.strict_loading
.includes(
:campaign,
Expand Down Expand Up @@ -151,7 +152,8 @@ def set_draft_vaccination_record
end

def set_patient_session
@patient_session = @patient.patient_sessions.find_by(session: @session)
@patient_session =
@patient.patient_sessions.active.find_by(session: @session)
end

def set_todays_batch
Expand Down
9 changes: 7 additions & 2 deletions app/models/immunisation_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,14 @@ def record!

ActiveRecord::Base.transaction do
vaccination_records.draft.each do |vaccination_record|
if vaccination_record.session.draft?
vaccination_record.session.update!(draft: false)
if (patient_session = vaccination_record.patient_session).draft?
patient_session.update!(active: true)
end

if (session = vaccination_record.session).draft?
session.update!(draft: false)
end

vaccination_record.update!(recorded_at:)
end

Expand Down
20 changes: 10 additions & 10 deletions app/models/immunisation_import_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ def session
)
end

def patient_session
return unless valid?

@patient_session ||=
PatientSession.create_with(created_by: @user).find_or_create_by!(
patient:,
session:
)
end

def notes
"Vaccinated at #{school_name}" if school_name.present? && location.nil?
end
Expand Down Expand Up @@ -294,16 +304,6 @@ def generic_clinic
).find_or_create_by!(type: :generic_clinic, ods_code: @user.team.ods_code)
end

def patient_session
return unless valid?

@patient_session ||=
PatientSession.create_with(created_by: @user).find_or_create_by!(
patient:,
session:
)
end

def vaccine
return unless administered

Expand Down
8 changes: 7 additions & 1 deletion app/models/patient_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Table name: patient_sessions
#
# id :bigint not null, primary key
# state :string
# active :boolean default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
# created_by_user_id :bigint
Expand Down Expand Up @@ -50,6 +50,12 @@ class PatientSession < ApplicationRecord
through: :patient,
class_name: "Consent"

scope :active, -> { where(active: true) }

def draft?
!active
end

def vaccination_record
# HACK: in future, it will be possible to have multiple vaccination records for a patient session
vaccination_records.recorded.last
Expand Down
4 changes: 4 additions & 0 deletions app/models/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ class Session < ApplicationRecord
if: -> { close_consent_on == "custom" }
end

def active?
!draft
end

def health_questions
campaign.vaccines.first.health_questions
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class RemoveStateFromPatientSessions < ActiveRecord::Migration[7.2]
def change
remove_column :patient_sessions, :state, :string
end
end
10 changes: 10 additions & 0 deletions db/migrate/20240905090740_add_active_to_patient_sessions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class AddActiveToPatientSessions < ActiveRecord::Migration[7.2]
def change
# rubocop:disable Rails/BulkChangeTable
add_column :patient_sessions, :active, :boolean, default: true, null: false
change_column_default :patient_sessions, :active, from: true, to: false
# rubocop:enable Rails/BulkChangeTable
end
end
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_08_27_081332) do
ActiveRecord::Schema[7.2].define(version: 2024_09_05_090740) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -268,8 +268,8 @@
t.bigint "campaign_id", null: false
t.datetime "recorded_at"
t.datetime "processed_at"
t.integer "exact_duplicate_record_count"
t.integer "new_record_count"
t.integer "exact_duplicate_record_count"
t.integer "not_administered_record_count"
t.text "csv_filename", null: false
t.datetime "csv_removed_at"
Expand Down Expand Up @@ -320,8 +320,8 @@
t.bigint "patient_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "state"
t.bigint "created_by_user_id"
t.boolean "active", default: false, null: false
t.index ["created_by_user_id"], name: "index_patient_sessions_on_created_by_user_id"
t.index ["patient_id", "session_id"], name: "index_patient_sessions_on_patient_id_and_session_id", unique: true
t.index ["session_id", "patient_id"], name: "index_patient_sessions_on_session_id_and_patient_id", unique: true
Expand Down
Binary file modified erd.pdf
Binary file not shown.
6 changes: 3 additions & 3 deletions spec/components/app_outcome_banner_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
subject(:rendered) { render_inline(component) }

let(:user) { create :user }
let(:patient_session) { create :patient_session, user: }
let(:patient_session) { create :patient_session, created_by: user }
let(:component) { described_class.new(patient_session:, current_user: user) }
let(:triage_nurse_name) { patient_session.triage.last.performed_by.full_name }
let(:patient_name) { patient_session.patient.full_name }
Expand Down Expand Up @@ -33,7 +33,7 @@
create(
:patient_session,
:vaccinated,
user:,
created_by: user,
session_attributes: {
campaign:
}
Expand Down Expand Up @@ -68,7 +68,7 @@

context "state is triaged_do_not_vaccinate" do
let(:patient_session) do
create :patient_session, :triaged_do_not_vaccinate, user:
create :patient_session, :triaged_do_not_vaccinate, created_by: user
end
let(:vaccination_record) { patient_session.vaccination_records.first }
let(:location) { patient_session.session.location }
Expand Down
70 changes: 56 additions & 14 deletions spec/factories/example_campaigns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

create(:session, :in_progress, campaign:, location:).tap do |session|
patients_without_consent =
create_list(:patient_session, 4, session:, user:)
create_list(:patient_session, 4, session:, created_by: user)
unmatched_patients = patients_without_consent.sample(2).map(&:patient)
unmatched_patients.each do |patient|
create(
Expand All @@ -40,24 +40,36 @@
4,
:consent_given_triage_not_needed,
session:,
user:
created_by: user
)
create_list(
:patient_session,
4,
:consent_given_triage_needed,
session:,
user:
created_by: user
)
create_list(
:patient_session,
4,
:triaged_ready_to_vaccinate,
session:,
user:
created_by: user
)
create_list(
:patient_session,
4,
:consent_refused,
session:,
created_by: user
)
create_list(
:patient_session,
2,
:consent_conflicting,
session:,
created_by: user
)
create_list(:patient_session, 4, :consent_refused, session:, user:)
create_list(:patient_session, 2, :consent_conflicting, session:, user:)
end
end
end
Expand All @@ -68,9 +80,27 @@
user = context.user

create(:session, :in_past, campaign:, location:).tap do |session|
create_list(:patient_session, 4, :vaccinated, session:, user:)
create_list(:patient_session, 4, :delay_vaccination, session:, user:)
create_list(:patient_session, 4, :unable_to_vaccinate, session:, user:)
create_list(
:patient_session,
4,
:vaccinated,
session:,
created_by: user
)
create_list(
:patient_session,
4,
:delay_vaccination,
session:,
created_by: user
)
create_list(
:patient_session,
4,
:unable_to_vaccinate,
session:,
created_by: user
)
end
end
end
Expand All @@ -82,7 +112,7 @@

create(:session, :in_future, campaign:, location:).tap do |session|
patients_without_consent =
create_list(:patient_session, 16, session:, user:)
create_list(:patient_session, 16, session:, created_by: user)
unmatched_patients = patients_without_consent.sample(8).map(&:patient)
unmatched_patients.each do |patient|
create(
Expand All @@ -98,17 +128,29 @@
8,
:consent_given_triage_not_needed,
session:,
user:
created_by: user
)
create_list(
:patient_session,
8,
:consent_given_triage_needed,
session:,
user:
created_by: user
)
create_list(
:patient_session,
8,
:consent_refused,
session:,
created_by: user
)
create_list(
:patient_session,
4,
:consent_conflicting,
session:,
created_by: user
)
create_list(:patient_session, 8, :consent_refused, session:, user:)
create_list(:patient_session, 4, :consent_conflicting, session:, user:)
end
end
end
Expand Down
Loading