@@ -14,59 +14,89 @@ class AppActivityLogComponent < ViewComponent::Base
1414 <% end %>
1515 ERB
1616
17- def initialize ( team :, patient : nil , patient_session : nil )
18- if patient . nil? && patient_session . nil?
19- raise "Pass either a patient or a patient session."
20- elsif patient && patient_session
21- raise "Pass only a patient or a patient session."
22- end
17+ def initialize ( team :, patient :, session : nil )
18+ @patient = patient
2319
24- @patient = patient || patient_session . patient
25- @patient_sessions =
26- patient_session ? [ patient_session ] : patient . patient_sessions
20+ @archive_reasons =
21+ @patient . archive_reasons . where ( team :) . includes ( :created_by )
2722
2823 @attendance_records =
29- ( patient || patient_session ) . attendance_records . includes ( :location )
24+ ( patient || patient_session )
25+ . attendance_records
26+ . includes ( :location )
27+ . then do |scope |
28+ session ? scope . where ( location : session . location ) : scope
29+ end
3030
3131 @consents =
32- @patient . consents . includes (
33- :consent_form ,
34- :parent ,
35- :recorded_by ,
36- :programme ,
37- patient : :parent_relationships
38- )
32+ @patient
33+ . consents
34+ . includes (
35+ :consent_form ,
36+ :parent ,
37+ :recorded_by ,
38+ :programme ,
39+ patient : :parent_relationships
40+ )
41+ . then do |scope |
42+ session ? scope . where ( programme : session . programmes ) : scope
43+ end
3944
4045 @gillick_assessments =
41- ( patient || patient_session )
46+ @ patient
4247 . gillick_assessments
4348 . includes ( :performed_by )
4449 . order ( :created_at )
50+ . then { |scope | session ? scope . where_session ( session ) : scope }
4551
4652 @notes =
47- ( patient || patient_session ) . notes . includes (
48- :created_by ,
49- :patient ,
50- session : :programmes
51- )
53+ @patient
54+ . notes
55+ . includes ( :created_by , :patient , session : :programmes )
56+ . then { |scope | session ? scope . where ( session :) : scope }
57+
58+ @notify_log_entries =
59+ @patient
60+ . notify_log_entries
61+ . includes ( :sent_by )
62+ . then do |scope |
63+ session ? scope . where ( programme_ids : session . programmes . ids ) : scope
64+ end
5265
53- @notify_log_entries = @patient . notify_log_entries . includes ( :sent_by )
66+ @patient_sessions =
67+ @patient
68+ . patient_sessions
69+ . includes_programmes
70+ . includes ( session : :location )
71+ . then { |scope | session ? scope . where ( session :) : scope }
72+
73+ @patient_specific_directions =
74+ @patient
75+ . patient_specific_directions
76+ . includes ( :created_by )
77+ . then do |scope |
78+ session ? scope . where ( programme : session . programmes ) : scope
79+ end
5480
5581 @pre_screenings =
56- ( patient || patient_session ) . pre_screenings . includes ( :performed_by )
82+ @patient
83+ . pre_screenings
84+ . includes ( :performed_by )
85+ . then { |scope | session ? scope . where_session ( session ) : scope }
5786
58- @triages = @patient . triages . includes ( :performed_by )
87+ @triages =
88+ @patient
89+ . triages
90+ . includes ( :performed_by )
91+ . then do |scope |
92+ session ? scope . where ( programme : session . programmes ) : scope
93+ end
5994
6095 @vaccination_records =
6196 @patient . vaccination_records . with_discarded . includes (
6297 :performed_by_user ,
6398 :vaccine
6499 )
65-
66- @patient_specific_directions =
67- @patient . patient_specific_directions . includes ( :created_by )
68-
69- @archive_reasons = @patient . archive_reasons . where ( team :)
70100 end
71101
72102 attr_reader :archive_reasons ,
@@ -216,9 +246,9 @@ def expiration_events
216246 body =
217247 case category
218248 when :not_vaccinated
219- "#{ @ patient. full_name } was not vaccinated."
249+ "#{ patient . full_name } was not vaccinated."
220250 when :vaccinated_but_seasonal
221- "#{ @ patient. full_name } was vaccinated."
251+ "#{ patient . full_name } was vaccinated."
222252 end
223253
224254 programmes = expired_items_in_category . values . flatten . uniq
@@ -317,11 +347,14 @@ def pre_screening_events
317347
318348 def session_events
319349 patient_sessions . map do |patient_session |
350+ patient = patient_session . patient
351+ session = patient_session . session
352+
320353 [
321354 {
322- title : "Added to the session at #{ patient_session . location . name } " ,
355+ title : "Added to the session at #{ session . location . name } " ,
323356 at : patient_session . created_at ,
324- programmes : programmes_for ( patient_session )
357+ programmes : session . programmes_for ( patient : )
325358 }
326359 ]
327360 end
0 commit comments