Skip to content

Commit 2b4b2fe

Browse files
authored
Merge pull request #4338 from nhsuk/v3.1.1-wip
Version 3.1.1
2 parents 906e02e + 2e3112a commit 2b4b2fe

28 files changed

+385
-90
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# frozen_string_literal: true
22

33
class AppProgrammeStatsComponent < ViewComponent::Base
4-
def initialize(programme, academic_year:, patients:)
4+
def initialize(programme, academic_year:, patient_ids:)
55
super
66

77
@programme = programme
88
@academic_year = academic_year
9-
@patients = patients
9+
@patient_ids = patient_ids
1010
end
1111

12-
delegate :count, to: :patients, prefix: true
12+
def patients_count = patient_ids.length
1313

1414
def vaccinations_count
1515
helpers
1616
.policy_scope(VaccinationRecord)
1717
.administered
18-
.where(patient: patients, programme:)
18+
.where(patient_id: patient_ids, programme:)
1919
.count
2020
end
2121

@@ -25,11 +25,11 @@ def consent_notifications_count
2525
.has_programme(programme)
2626
.joins(:session)
2727
.where(session: { academic_year: })
28-
.where(patient: patients)
28+
.where(patient_id: patient_ids)
2929
.count
3030
end
3131

3232
private
3333

34-
attr_reader :programme, :academic_year, :patients
34+
attr_reader :programme, :academic_year, :patient_ids
3535
end

app/controllers/programmes/base_controller.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,23 @@ def set_academic_year
2020
end
2121
end
2222

23-
def patients
24-
# We do this instead of using `team.patients` as that has a `distinct` on
25-
# it which means we cannot apply ordering or grouping.
26-
@patients ||=
27-
Patient
28-
.where(id: current_team.patient_sessions.select(:patient_id).distinct)
29-
.appear_in_programmes([@programme], academic_year: @academic_year)
23+
def patient_ids
24+
@patient_ids ||=
25+
PatientSession
26+
.distinct
27+
.joins(:patient, :session)
28+
.where(session_id: session_ids)
29+
.appear_in_programmes([@programme])
3030
.not_archived(team: current_team)
31+
.pluck(:patient_id)
32+
end
33+
34+
def session_ids
35+
@session_ids ||=
36+
current_team
37+
.sessions
38+
.where(academic_year: @academic_year)
39+
.has_programmes([@programme])
40+
.pluck(:id)
3141
end
3242
end

app/controllers/programmes/overview_controller.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class Programmes::OverviewController < Programmes::BaseController
44
before_action :set_consents
5-
before_action :set_patients
5+
before_action :set_patient_ids
66
before_action :set_patient_count_by_year_group
77

88
def show
@@ -13,21 +13,21 @@ def show
1313
def set_consents
1414
@consents =
1515
policy_scope(Consent).where(
16-
patient: patients,
16+
patient_id: patient_ids,
1717
programme: @programme,
1818
academic_year: @academic_year
1919
)
2020
end
2121

22-
def set_patients
23-
@patients = patients
22+
def set_patient_ids
23+
@patient_ids = patient_ids
2424
end
2525

2626
def set_patient_count_by_year_group
2727
year_groups = current_team.programme_year_groups[@programme]
2828

2929
patient_count_by_birth_academic_year =
30-
patients.group(:birth_academic_year).count
30+
Patient.where(id: patient_ids).group(:birth_academic_year).count
3131

3232
@patient_count_by_year_group =
3333
year_groups.index_with do |year_group|

app/controllers/programmes/patients_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def index
99
@year_groups = current_team.programme_year_groups[@programme]
1010

1111
scope =
12-
patients.includes(
12+
Patient.where(id: patient_ids).includes(
1313
:consent_statuses,
1414
:triage_statuses,
1515
:vaccination_statuses,

app/helpers/imports_helper.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
module ImportsHelper
44
def import_issues_count
55
vaccination_records_with_issues =
6-
policy_scope(VaccinationRecord).with_pending_changes.distinct
6+
policy_scope(VaccinationRecord).with_pending_changes.distinct.pluck(
7+
:patient_id
8+
)
79

8-
patients_with_issues = policy_scope(Patient).with_pending_changes
10+
patients_with_issues = policy_scope(Patient).with_pending_changes.pluck(:id)
911

10-
unique_import_issues =
11-
(vaccination_records_with_issues + patients_with_issues).uniq do |record|
12-
record.is_a?(VaccinationRecord) ? record.patient_id : record.id
13-
end
14-
15-
unique_import_issues.count
12+
(vaccination_records_with_issues + patients_with_issues).uniq.length
1613
end
1714
end

app/jobs/commit_patient_changesets_job.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def perform(import)
2222
increment_column_counts!(import, counts, changesets)
2323
end
2424

25+
import.postprocess_rows!
26+
2527
import.update_columns(
2628
processed_at: Time.zone.now,
2729
status: :processed,

app/lib/patient_merger.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ def call
8989

9090
PatientSession.where(patient: patient_to_destroy).destroy_all
9191

92+
patient_to_destroy.changesets.update_all(patient_id: nil)
93+
9294
patient_to_destroy.class_imports.each do |import|
9395
unless patient_to_keep.class_imports.include?(import)
9496
patient_to_keep.class_imports << import

app/models/class_import.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ class ClassImport < PatientImport
4747
as: :import,
4848
dependent: :destroy
4949

50-
private
51-
52-
def parse_row(data)
53-
ClassImportRow.new(data:, team:, academic_year:, location:, year_groups:)
54-
end
55-
5650
def postprocess_rows!
5751
# Remove patients already in the sessions but not in the class list.
5852

@@ -86,4 +80,10 @@ def postprocess_rows!
8680

8781
PatientsAgedOutOfSchoolJob.perform_later
8882
end
83+
84+
private
85+
86+
def parse_row(data)
87+
ClassImportRow.new(data:, team:, academic_year:, location:, year_groups:)
88+
end
8989
end

app/models/cohort_import.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ class CohortImport < PatientImport
4141
as: :import,
4242
dependent: :destroy
4343

44+
def postprocess_rows!
45+
PatientsAgedOutOfSchoolJob.perform_later
46+
end
47+
4448
private
4549

4650
def parse_row(data)
4751
CohortImportRow.new(data:, team:, academic_year:)
4852
end
49-
50-
def postprocess_rows!
51-
PatientsAgedOutOfSchoolJob.perform_later
52-
end
5353
end

app/models/patient.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class Patient < ApplicationRecord
6161

6262
has_many :access_log_entries
6363
has_many :archive_reasons
64+
has_many :changesets, class_name: "PatientChangeset"
6465
has_many :consent_notifications
6566
has_many :consent_statuses
6667
has_many :consents

0 commit comments

Comments
 (0)