@@ -41,147 +41,12 @@ class AppActivityLogComponent < ViewComponent::Base
4141
4242 def initialize ( team :, patient :, programme_type : nil , session : nil )
4343 @patient = patient
44-
45- @archive_reasons =
46- @patient . archive_reasons . where ( team :) . includes ( :created_by )
47-
48- @attendance_records =
49- patient
50- . attendance_records
51- . includes ( :location )
52- . then do |scope |
53- session ? scope . where ( location : session . location ) : scope
54- end
55-
56- @consents =
57- @patient
58- . consents
59- . includes (
60- :consent_form ,
61- :parent ,
62- :recorded_by ,
63- patient : :parent_relationships
64- )
65- . then do |scope |
66- if programme_type
67- scope . where ( programme_type :)
68- elsif session
69- scope . for_session ( session )
70- else
71- scope
72- end
73- end
74-
75- @gillick_assessments =
76- @patient
77- . gillick_assessments
78- . includes ( :performed_by )
79- . order ( :created_at )
80- . then do |scope |
81- if programme_type
82- scope . where ( programme_type :)
83- elsif session
84- scope . for_session ( session )
85- else
86- scope
87- end
88- end
89-
90- @notes =
91- @patient
92- . notes
93- . includes ( :created_by , :patient , :session )
94- . then { |scope | session ? scope . where ( session :) : scope }
95-
96- @notify_log_entries =
97- @patient
98- . notify_log_entries
99- . includes ( :sent_by )
100- . preload ( :notify_log_entry_programmes )
101- . then do |scope |
102- if programme_type
103- scope . for_programme_type ( programme_type )
104- elsif session
105- scope . for_session ( session )
106- else
107- scope
108- end
109- end
110-
111- @patient_locations =
112- @patient
113- . patient_locations
114- . includes ( :location )
115- . then do |scope |
116- session ? scope . where ( location : session . location ) : scope
117- end
118-
119- @patient_merge_log_entries =
120- @patient . patient_merge_log_entries . includes ( :user )
121-
122- @patient_specific_directions =
123- @patient
124- . patient_specific_directions
125- . includes ( :created_by )
126- . then do |scope |
127- if programme_type
128- scope . where ( programme_type :)
129- elsif session
130- scope . for_session ( session )
131- else
132- scope
133- end
134- end
135-
136- @pre_screenings =
137- @patient
138- . pre_screenings
139- . includes ( :performed_by )
140- . then do |scope |
141- if programme_type
142- scope . where ( programme_type :)
143- elsif session
144- scope . for_session ( session )
145- else
146- scope
147- end
148- end
149-
150- @triages =
151- @patient
152- . triages
153- . includes ( :performed_by )
154- . then do |scope |
155- if programme_type
156- scope . where ( programme_type :)
157- elsif session
158- scope . for_session ( session )
159- else
160- scope
161- end
162- end
163-
164- @vaccination_records =
165- @patient
166- . vaccination_records
167- . with_discarded
168- . includes ( :performed_by_user , :vaccine )
169- . then { |scope | programme_type ? scope . where ( programme_type :) : scope }
44+ @team = team
45+ @programme_type = programme_type
46+ @session = session
17047 end
17148
172- attr_reader :archive_reasons ,
173- :consents ,
174- :gillick_assessments ,
175- :notes ,
176- :notify_log_entries ,
177- :patient ,
178- :patient_locations ,
179- :patient_merge_log_entries ,
180- :patient_specific_directions ,
181- :pre_screenings ,
182- :attendance_records ,
183- :triages ,
184- :vaccination_records
49+ attr_reader :patient
18550
18651 def all_events
18752 [
@@ -290,6 +155,8 @@ def consent_events
290155 end
291156
292157 def expiration_events
158+ return [ ] unless include_programme_specific_events?
159+
293160 all_programmes = Programme . all . to_a
294161
295162 AcademicYear . all . flat_map do |academic_year |
@@ -568,6 +435,186 @@ def attendance_events
568435
569436 private
570437
438+ def include_programme_specific_events?
439+ @programme_type . present? || @session . present?
440+ end
441+
442+ def archive_reasons
443+ return [ ] if include_programme_specific_events?
444+
445+ @archive_reasons ||=
446+ @patient . archive_reasons . where ( team : @team ) . includes ( :created_by )
447+ end
448+
449+ def patient_merge_log_entries
450+ return [ ] if include_programme_specific_events?
451+
452+ @patient_merge_log_entries ||=
453+ @patient . patient_merge_log_entries . includes ( :user )
454+ end
455+
456+ def attendance_records
457+ return [ ] unless include_programme_specific_events?
458+
459+ @attendance_records ||=
460+ patient
461+ . attendance_records
462+ . includes ( :location )
463+ . then do |scope |
464+ @session ? scope . where ( location : @session . location ) : scope
465+ end
466+ end
467+
468+ def consents
469+ return [ ] unless include_programme_specific_events?
470+
471+ @consents ||=
472+ @patient
473+ . consents
474+ . includes (
475+ :consent_form ,
476+ :parent ,
477+ :recorded_by ,
478+ patient : :parent_relationships
479+ )
480+ . then do |scope |
481+ if @programme_type
482+ scope . where ( programme_type : @programme_type )
483+ elsif @session
484+ scope . for_session ( @session )
485+ else
486+ scope
487+ end
488+ end
489+ end
490+
491+ def gillick_assessments
492+ return [ ] unless include_programme_specific_events?
493+
494+ @gillick_assessments ||=
495+ @patient
496+ . gillick_assessments
497+ . includes ( :performed_by )
498+ . order ( :created_at )
499+ . then do |scope |
500+ if @programme_type
501+ scope . where ( programme_type : @programme_type )
502+ elsif @session
503+ scope . for_session ( @session )
504+ else
505+ scope
506+ end
507+ end
508+ end
509+
510+ def notes
511+ return [ ] unless include_programme_specific_events?
512+
513+ @notes ||=
514+ @patient
515+ . notes
516+ . includes ( :created_by , :patient , :session )
517+ . then { |scope | @session ? scope . where ( session : @session ) : scope }
518+ end
519+
520+ def notify_log_entries
521+ return [ ] unless include_programme_specific_events?
522+
523+ @notify_log_entries ||=
524+ @patient
525+ . notify_log_entries
526+ . includes ( :sent_by )
527+ . preload ( :notify_log_entry_programmes )
528+ . then do |scope |
529+ if @programme_type
530+ scope . for_programme_type ( @programme_type )
531+ elsif @session
532+ scope . for_session ( @session )
533+ else
534+ scope
535+ end
536+ end
537+ end
538+
539+ def patient_locations
540+ return [ ] unless include_programme_specific_events?
541+
542+ @patient_locations ||=
543+ @patient
544+ . patient_locations
545+ . includes ( :location )
546+ . then do |scope |
547+ @session ? scope . where ( location : @session . location ) : scope
548+ end
549+ end
550+
551+ def patient_specific_directions
552+ return [ ] unless include_programme_specific_events?
553+
554+ @patient_specific_directions ||=
555+ @patient
556+ . patient_specific_directions
557+ . includes ( :created_by )
558+ . then do |scope |
559+ if @programme_type
560+ scope . where ( programme_type : @programme_type )
561+ elsif @session
562+ scope . for_session ( @session )
563+ else
564+ scope
565+ end
566+ end
567+ end
568+
569+ def pre_screenings
570+ return [ ] unless include_programme_specific_events?
571+
572+ @pre_screenings ||=
573+ @patient
574+ . pre_screenings
575+ . includes ( :performed_by )
576+ . then do |scope |
577+ if @programme_type
578+ scope . where ( programme_type : @programme_type )
579+ elsif @session
580+ scope . for_session ( @session )
581+ else
582+ scope
583+ end
584+ end
585+ end
586+
587+ def triages
588+ return [ ] unless include_programme_specific_events?
589+
590+ @triages ||=
591+ @patient
592+ . triages
593+ . includes ( :performed_by )
594+ . then do |scope |
595+ if @programme_type
596+ scope . where ( programme_type : @programme_type )
597+ elsif @session
598+ scope . for_session ( @session )
599+ else
600+ scope
601+ end
602+ end
603+ end
604+
605+ def vaccination_records
606+ return [ ] unless include_programme_specific_events?
607+
608+ @vaccination_records ||=
609+ @patient
610+ . vaccination_records
611+ . with_discarded
612+ . includes ( :performed_by_user , :vaccine )
613+ . then do |scope |
614+ @programme_type ? scope . where ( programme_type : @programme_type ) : scope
615+ end
616+ end
617+
571618 def expired_items_for ( academic_year :, programmes :)
572619 {
573620 consents :,
0 commit comments