Skip to content

Commit 6997da8

Browse files
murugaplthomasleese
authored andcommitted
Merge pull request #4302 from nhsuk/perform-pds-search-for-all-patients
Perform pds search for all patients
2 parents fa49197 + f9d16b3 commit 6997da8

26 files changed

+535
-80
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<% return if discrepancies.blank? %>
2+
3+
<div class="nhsuk-table__panel-with-heading-tab">
4+
<h3 class="nhsuk-table__heading-tab">
5+
<%= pluralize(discrepancies.count, "NHS number") %>
6+
<%= discrepancies.count == 1 ? "was" : "were" %> updated
7+
</h3>
8+
9+
<p>
10+
<%= pluralize(discrepancies.count, "incorrect NHS number") %>
11+
updated with <%= discrepancies.count == 1 ? "that" : "those" %> found on Spine.
12+
Update your original data source if necessary.
13+
</p>
14+
15+
<%= govuk_table(html_attributes: { class: "nhsuk-table-responsive" }) do |table| %>
16+
<% table.with_head do |head| %>
17+
<% head.with_row do |row| %>
18+
<% row.with_cell(text: "Child record") %>
19+
<% row.with_cell(text: "NHS number (upload)") %>
20+
<% row.with_cell(text: "NHS number (Spine)") %>
21+
<% end %>
22+
<% end %>
23+
24+
<% table.with_body do |body| %>
25+
<% discrepancies.each do |changeset| %>
26+
<% patient = changeset.patient %>
27+
28+
<% body.with_row do |row| %>
29+
<% row.with_cell do %>
30+
<span class="nhsuk-table-responsive__heading">Child record</span>
31+
<span>
32+
<% if can_link_to?(patient) %>
33+
<%= link_to patient.full_name, patient_path(patient) %>
34+
<% else %>
35+
<%= patient.full_name %>
36+
<% end %>
37+
</span>
38+
<% end %>
39+
40+
<% row.with_cell do %>
41+
<span class="nhsuk-table-responsive__heading">Uploaded NHS number</span>
42+
<%= helpers.format_nhs_number(changeset.uploaded_nhs_number) %>
43+
<% end %>
44+
45+
<% row.with_cell do %>
46+
<span class="nhsuk-table-responsive__heading">PDS NHS number</span>
47+
<%= helpers.format_nhs_number(changeset.pds_nhs_number) %>
48+
<% end %>
49+
<% end %>
50+
<% end %>
51+
<% end %>
52+
<% end %>
53+
</div>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
class AppPatientPDSDiscrepancyTableComponent < ViewComponent::Base
4+
def initialize(discrepancies:, current_user:)
5+
super
6+
7+
@discrepancies = discrepancies
8+
@current_user = current_user
9+
end
10+
11+
private
12+
13+
attr_reader :discrepancies, :current_user
14+
15+
def can_link_to?(record)
16+
allowed_ids.include?(record.id)
17+
end
18+
19+
def allowed_ids
20+
@allowed_ids ||= PatientPolicy::Scope.new(current_user, Patient).resolve.ids
21+
end
22+
end

app/controllers/class_imports_controller.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ def show
4646

4747
@duplicates = @class_import.patients.with_pending_changes.distinct
4848

49+
@issues_text =
50+
@duplicates.each_with_object({}) do |patient, hash|
51+
changeset = @class_import.changesets.find_by(patient_id: patient.id)
52+
53+
issue_groups =
54+
helpers.issue_categories_for(patient.pending_changes.keys)
55+
56+
hash[patient.full_name] = if changeset&.matched_on_nhs_number
57+
"Matched on NHS number. #{issue_groups.to_sentence.capitalize}" +
58+
(issue_groups.size == 1 ? " does not match." : " do not match.")
59+
else
60+
"Possible match found. Review and confirm."
61+
end
62+
end
63+
64+
@nhs_discrepancies = @class_import.changesets.nhs_number_discrepancies
65+
4966
render template: "imports/show",
5067
layout: "full",
5168
locals: {

app/controllers/cohort_imports_controller.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ def show
4444

4545
@duplicates = @cohort_import.patients.with_pending_changes.distinct
4646

47+
@issues_text =
48+
@duplicates.each_with_object({}) do |patient, hash|
49+
changeset = @cohort_import.changesets.find_by(patient_id: patient.id)
50+
51+
issue_groups =
52+
helpers.issue_categories_for(patient.pending_changes.keys)
53+
54+
hash[patient.full_name] = if changeset&.matched_on_nhs_number
55+
"Matched on NHS number. #{issue_groups.to_sentence.capitalize}" +
56+
(issue_groups.size == 1 ? " does not match." : " do not match.")
57+
else
58+
"Possible match found. Review and confirm."
59+
end
60+
end
61+
62+
@nhs_discrepancies = @cohort_import.changesets.nhs_number_discrepancies
63+
4764
render template: "imports/show",
4865
layout: "full",
4966
locals: {

app/helpers/imports_helper.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
# frozen_string_literal: true
22

33
module ImportsHelper
4+
FIELD_GROUPS = {
5+
%w[address_line_1 address_line_2 address_town address_postcode] => :address,
6+
%w[date_of_birth] => :date_of_birth,
7+
%w[nhs_number] => :nhs_number,
8+
%w[gender_code] => :gender,
9+
%w[given_name family_name preferred_given_name preferred_family_name] =>
10+
:name,
11+
%w[birth_academic_year] => :year_group,
12+
%w[registration] => :registration
13+
}.freeze
14+
415
def import_issues_count
516
vaccination_records_with_issues =
617
policy_scope(VaccinationRecord).with_pending_changes.distinct.pluck(
@@ -11,4 +22,10 @@ def import_issues_count
1122

1223
(vaccination_records_with_issues + patients_with_issues).uniq.length
1324
end
25+
26+
def issue_categories_for(pending_changes)
27+
FIELD_GROUPS.filter_map do |(keys, group)|
28+
group.to_s.humanize if (pending_changes & keys).any?
29+
end
30+
end
1431
end

app/helpers/patients_helper.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ module PatientsHelper
44
# Replace each space in NHS number with a non-breaking space and
55
# zero-width word joiner to prevent telephone format detection
66
def patient_nhs_number(patient)
7+
format_nhs_number(patient.nhs_number, invalid: patient.try(:invalidated?))
8+
end
9+
10+
def format_nhs_number(nhs_number, invalid: false)
711
span =
8-
if patient.nhs_number.blank?
12+
if nhs_number.blank?
913
"Not provided"
1014
else
1115
tag.span(class: %w[app-u-monospace nhsuk-u-nowrap]) do
12-
patient
13-
.nhs_number
14-
.to_s
15-
.gsub(/(\d{3})(\d{3})(\d{4})/, "\\1 \\2 \\3")
16-
.html_safe
16+
nhs_number.to_s.gsub(/(\d{3})(\d{3})(\d{4})/, "\\1 \\2 \\3").html_safe
1717
end
1818
end
1919

20-
patient.try(:invalidated?) ? tag.s(span) : span
20+
invalid ? tag.s(span) : span
2121
end
2222

2323
def patient_date_of_birth(patient)

app/jobs/commit_patient_changesets_job.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def import_patients_and_parents(changesets, import)
4444
Patient.import(patients.to_a, on_duplicate_key_update: :all)
4545
link_records_to_import(import, Patient, patients)
4646

47+
changesets.each(&:assign_patient_id)
48+
PatientChangeset.import(changesets, on_duplicate_key_update: :all)
49+
4750
Parent.import(parents.to_a, on_duplicate_key_update: :all)
4851
link_records_to_import(import, Parent, parents)
4952

app/jobs/process_patient_changesets_job.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ class ProcessPatientChangesetsJob < ApplicationJob
88
def perform(patient_changeset)
99
attrs = patient_changeset.child_attributes
1010

11-
if attrs["nhs_number"].blank? &&
12-
(pds_patient = search_for_patient(attrs)).present?
11+
pds_patient = search_for_patient(attrs)
12+
if pds_patient.present?
1313
attrs["nhs_number"] = pds_patient.nhs_number
14+
patient_changeset.pds_nhs_number = pds_patient.nhs_number
1415
end
1516

1617
patient_changeset.processed!

app/models/class_imports_patient.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#
55
# Table name: class_imports_patients
66
#
7-
# class_import_id :bigint not null
8-
# patient_id :bigint not null
7+
# class_import_id :bigint not null
8+
# patient_id :bigint not null
99
#
1010
# Indexes
1111
#

app/models/cohort_imports_patient.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#
55
# Table name: cohort_imports_patients
66
#
7-
# cohort_import_id :bigint not null
8-
# patient_id :bigint not null
7+
# cohort_import_id :bigint not null
8+
# patient_id :bigint not null
99
#
1010
# Indexes
1111
#

0 commit comments

Comments
 (0)