11# frozen_string_literal: true
22
33class AppImportReviewIssuesSummaryComponent < ViewComponent ::Base
4- def initialize ( changesets : nil )
5- @changesets = changesets . sort_by ( &:row_number ) || [ ]
4+ def initialize ( import : nil , records : nil , show_actions : false )
5+ @import = import
6+ @records = Array ( records ) . sort_by { it . try ( :row_number ) || 0 }
7+ @show_actions = show_actions
68 end
79
810 def call
@@ -15,57 +17,145 @@ def call
1517 head . with_row do |row |
1618 row . with_cell ( text : "Name and NHS number" )
1719 row . with_cell ( text : "Issue to review" )
20+ row . with_cell ( text : "Actions" ) if @show_actions
1821 end
1922 end
20-
2123 table . with_body do |body |
22- @changesets . each do |changeset |
24+ @records . each do |record |
2325 body . with_row do |row |
24- row . with_cell do
25- heading =
26- tag . span (
27- "Name and NHS number" ,
28- class : "nhsuk-table-responsive__heading"
29- )
26+ row . with_cell { render_name_cell ( record ) }
27+ row . with_cell { render_issue_cell ( record ) }
28+ row . with_cell { render_action_cell ( record ) } if @show_actions
29+ end
30+ end
31+ end
32+ end
33+ end
3034
31- helpers . safe_join (
32- [
33- heading ,
34- tag . span (
35- FullNameFormatter . call ( changeset , context : :internal )
36- ) ,
37- tag . br ,
38- tag . span (
39- helpers . format_nhs_number ( changeset . nhs_number ) ,
40- class : "nhsuk-u-secondary-text-colour nhsuk-u-font-size-16"
41- )
42- ]
43- )
44- end
35+ private
4536
46- row . with_cell do
47- heading =
48- tag . span (
49- "Issue to review" ,
50- class : "nhsuk-table-responsive__heading"
51- )
37+ def render_name_cell ( record )
38+ heading =
39+ tag . span ( "Name and NHS number" , class : "nhsuk-table-responsive__heading" )
40+ name = format_name ( record )
41+ nhs_number = format_nhs_number ( record )
5242
53- pending_changes = changeset . review_data [ "pending_changes" ] || { }
54- issue_groups = helpers . issue_categories_for ( pending_changes . keys )
43+ helpers . safe_join (
44+ [
45+ heading ,
46+ tag . span ( name ) ,
47+ tag . br ,
48+ tag . span (
49+ nhs_number ,
50+ class : "nhsuk-u-secondary-text-colour nhsuk-u-font-size-16"
51+ )
52+ ]
53+ )
54+ end
5555
56- issue_text =
57- if changeset . matched_on_nhs_number?
58- "Matched on NHS number. " \
59- "#{ issue_groups . to_sentence . capitalize } #{ issue_groups . size == 1 ? "does not" : "do not" } match."
60- else
61- "Possible match found. Review and confirm."
62- end
56+ def render_issue_cell ( record )
57+ heading =
58+ tag . span ( "Issue to review" , class : "nhsuk-table-responsive__heading" )
59+ issue_text = determine_issue_text ( record )
60+ helpers . safe_join ( [ heading , tag . span ( issue_text ) ] )
61+ end
6362
64- helpers . safe_join ( [ heading , tag . span ( issue_text ) ] )
65- end
66- end
67- end
63+ def render_action_cell ( record )
64+ heading = tag . span ( "Actions" , class : "nhsuk-table-responsive__heading" )
65+ action_link = generate_action_link ( record )
66+ helpers . safe_join ( [ heading , action_link ] )
67+ end
68+
69+ def format_name ( record )
70+ case record
71+ when PatientChangeset
72+ FullNameFormatter . call ( record , context : :internal )
73+ when Patient
74+ record . full_name
75+ when VaccinationRecord
76+ record . patient &.full_name || "Unknown"
77+ else
78+ raise "Unknown record type: #{ record . class . name } "
79+ end
80+ end
81+
82+ def format_nhs_number ( record )
83+ case record
84+ when PatientChangeset
85+ helpers . format_nhs_number ( record . nhs_number )
86+ when Patient
87+ helpers . format_nhs_number ( record . nhs_number )
88+ when VaccinationRecord
89+ helpers . format_nhs_number ( record . patient &.nhs_number )
90+ else
91+ raise "Unknown record type: #{ record . class . name } "
92+ end
93+ end
94+
95+ def determine_issue_text ( record )
96+ case record
97+ when PatientChangeset , Patient
98+ patient_import_issue_text ( record )
99+ when VaccinationRecord
100+ "Imported record closely matches an existing record. Review and confirm."
101+ else
102+ raise "Unknown record type: #{ record . class . name } "
103+ end
104+ end
105+
106+ def patient_import_issue_text ( record )
107+ pending_changes =
108+ if record . is_a? ( PatientChangeset )
109+ record . review_data [ "patient" ] [ "pending_changes" ] || { }
110+ else
111+ record . pending_changes || { }
112+ end
113+ issue_groups = helpers . issue_categories_for ( pending_changes . keys )
114+
115+ if issue_groups . any? && matched_on_nhs_number? ( record )
116+ "Matched on NHS number. " \
117+ "#{ issue_groups . to_sentence . capitalize } #{ issue_groups . size == 1 ? "does not" : "do not" } match."
118+ else
119+ "Possible match found. Review and confirm."
120+ end
121+ end
122+
123+ def matched_on_nhs_number? ( record )
124+ if record . is_a? ( PatientChangeset )
125+ record . matched_on_nhs_number?
126+ elsif record . is_a? ( Patient )
127+ @import
128+ &.changesets
129+ &.find_by ( patient_id : record . id )
130+ &.matched_on_nhs_number?
131+ end
132+ end
133+
134+ def generate_action_link ( record )
135+ case record
136+ when Patient
137+ helpers . link_to ( imports_issue_path ( record , type : "patient" ) ) do
138+ helpers . safe_join (
139+ [
140+ "Review " ,
141+ tag . span ( record . full_name , class : "nhsuk-u-visually-hidden" )
142+ ]
143+ )
144+ end
145+ when VaccinationRecord
146+ helpers . link_to ( imports_issue_path ( record , type : "vaccination-record" ) ) do
147+ helpers . safe_join (
148+ [
149+ "Review " ,
150+ tag . span (
151+ record . patient &.full_name ,
152+ class : "nhsuk-u-visually-hidden"
153+ )
154+ ]
155+ )
68156 end
157+ else
158+ ""
69159 end
70160 end
71161end
0 commit comments