-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathapp_activity_log_component.rb
More file actions
442 lines (384 loc) · 11.4 KB
/
app_activity_log_component.rb
File metadata and controls
442 lines (384 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# frozen_string_literal: true
class AppActivityLogComponent < ViewComponent::Base
erb_template <<-ERB
<% events_by_day.each do |day, events| %>
<h3 class="nhsuk-heading-xs nhsuk-u-secondary-text-colour
nhsuk-u-font-weight-normal">
<%= day.to_fs(:long) %>
</h3>
<% events.each do |event| %>
<%= render AppLogEventComponent.new(card: true, **event) %>
<% end %>
<% end %>
ERB
def initialize(team:, patient:, session: nil)
@patient = patient
@archive_reasons =
@patient.archive_reasons.where(team:).includes(:created_by)
@attendance_records =
patient
.attendance_records
.includes(:location)
.then do |scope|
session ? scope.where(location: session.location) : scope
end
@consents =
@patient
.consents
.includes(
:consent_form,
:parent,
:recorded_by,
patient: :parent_relationships
)
.then do |scope|
session ? scope.where(programme_type: session.programme_types) : scope
end
@gillick_assessments =
@patient
.gillick_assessments
.includes(:performed_by)
.order(:created_at)
.then { |scope| session ? scope.for_session(session) : scope }
@notes =
@patient
.notes
.includes(:created_by, :patient, :session)
.then { |scope| session ? scope.where(session:) : scope }
@notify_log_entries =
@patient
.notify_log_entries
.includes(:sent_by)
.preload(:notify_log_entry_programmes)
.then { |scope| session ? scope.for_session(session) : scope }
@patient_locations =
@patient
.patient_locations
.includes(:location)
.then do |scope|
session ? scope.where(location: session.location) : scope
end
@patient_specific_directions =
@patient
.patient_specific_directions
.includes(:created_by)
.then do |scope|
session ? scope.where(programme_type: session.programme_types) : scope
end
@pre_screenings =
@patient
.pre_screenings
.includes(:performed_by)
.then { |scope| session ? scope.for_session(session) : scope }
@triages =
@patient
.triages
.includes(:performed_by)
.then do |scope|
session ? scope.where(programme_type: session.programme_types) : scope
end
@vaccination_records =
@patient.vaccination_records.with_discarded.includes(
:performed_by_user,
:vaccine
)
end
attr_reader :archive_reasons,
:consents,
:gillick_assessments,
:notes,
:notify_log_entries,
:patient,
:patient_locations,
:patient_specific_directions,
:pre_screenings,
:attendance_records,
:triages,
:vaccination_records
def events_by_day
all_events.sort_by { it[:at] }.reverse.group_by { it[:at].to_date }
end
def all_events
[
archive_events,
attendance_events,
consent_events,
expiration_events,
gillick_assessment_events,
note_events,
notify_events,
patient_specific_direction_events,
pre_screening_events,
session_events,
triage_events,
vaccination_events
].flatten
end
def archive_events
archive_reasons.flat_map do |archive_reason|
{
title: "Record archived: #{archive_reason.human_enum_name(:type)}",
body: archive_reason.other_details,
at: archive_reason.created_at,
by: archive_reason.created_by
}
end
end
def consent_events
consents.flat_map do |consent|
events = []
original_response = consent.withdrawn? ? "given" : consent.response
events << if (consent_form = consent.consent_form)
{
title: "Consent #{original_response}",
at: consent_form.recorded_at,
by: consent_form.parent_relationship.label_with_parent,
programmes: [consent.programme]
}
else
{
title:
"Consent #{original_response} by #{consent.name} (#{consent.who_responded})",
at: consent.submitted_at,
by: consent.recorded_by,
programmes: [consent.programme]
}
end
if consent.matched_manually?
events << {
title: "Consent response manually matched with child record",
at: consent.created_at,
by: consent.recorded_by,
programmes: [consent.programme]
}
end
if consent.invalidated?
events << {
title: "Consent from #{consent.name} invalidated",
at: consent.invalidated_at,
programmes: [consent.programme]
}
end
if consent.withdrawn?
events << {
title: "Consent from #{consent.name} withdrawn",
at: consent.withdrawn_at,
programmes: [consent.programme]
}
end
events
end
end
def expiration_events
all_programmes = Programme.all.to_a
AcademicYear.all.flat_map do |academic_year|
next [] if academic_year >= AcademicYear.current
not_vaccinated_programmes =
all_programmes.reject do |programme|
patient.programme_status(programme, academic_year:).vaccinated?
end
vaccinated_but_seasonal_programmes =
all_programmes.select do |programme|
patient.programme_status(programme, academic_year:).vaccinated? &&
programme.seasonal?
end
expired_items =
{
vaccinated_but_seasonal: vaccinated_but_seasonal_programmes,
not_vaccinated: not_vaccinated_programmes
}.transform_values do |programmes|
expired_items_for(academic_year:, programmes:)
end
expired_items.map do |category, expired_items_in_category|
expired_item_names = []
if expired_items_in_category[:consents].any?
expired_item_names += ["consent", "health information"]
end
if expired_items_in_category[:triages].any?
expired_item_names << "triage outcome"
end
if expired_items_in_category[:patient_specific_directions].any?
expired_item_names << "PSD status"
end
next [] if expired_item_names.empty?
title =
"#{
expired_item_names.to_sentence(
words_connector: ", ",
last_word_connector: " and "
).upcase_first
} expired"
body =
case category
when :not_vaccinated
"#{patient.full_name} was not vaccinated."
when :vaccinated_but_seasonal
"#{patient.full_name} was vaccinated."
end
programmes = expired_items_in_category.values.flatten.uniq
{
title:,
body:,
at:
academic_year.to_academic_year_date_range.end.end_of_day - 1.second,
programmes:
}
end
end
end
def gillick_assessment_events
gillick_assessments.each_with_index.map do |gillick_assessment, index|
action = index.zero? ? "Completed" : "Updated"
outcome =
(
if gillick_assessment.gillick_competent?
"Gillick competent"
else
"not Gillick competent"
end
)
{
title: "#{action} Gillick assessment as #{outcome}",
body: gillick_assessment.notes,
at: gillick_assessment.created_at,
by: gillick_assessment.performed_by,
programmes: [gillick_assessment.programme]
}
end
end
def note_events
notes.map do |note|
{
title: "Note",
body: note.body,
at: note.created_at,
by: note.created_by,
programmes: note.programmes
}
end
end
def notify_events
notify_log_entries.map do |notify_log_entry|
{
title: "#{notify_log_entry.title} sent",
body: patient.restricted? ? "" : notify_log_entry.recipient,
at: notify_log_entry.created_at,
by: notify_log_entry.sent_by,
programmes: notify_log_entry.programmes
}
end
end
def patient_specific_direction_events
patient_specific_directions.flat_map do |patient_specific_direction|
events = []
events << {
title: "PSD added",
at: patient_specific_direction.created_at,
by: patient_specific_direction.created_by,
programmes: [patient_specific_direction.programme]
}
if patient_specific_direction.invalidated?
events << {
title: "PSD invalidated",
at: patient_specific_direction.invalidated_at,
programmes: [patient_specific_direction.programme]
}
end
events
end
end
def pre_screening_events
pre_screenings.map do |pre_screening|
{
title: "Completed pre-screening checks",
body: pre_screening.notes,
at: pre_screening.created_at,
by: pre_screening.performed_by,
programmes: [pre_screening.programme]
}
end
end
def session_events
patient_locations.map do |patient_location|
[
{
title: "Added to the session at #{patient_location.location.name}",
at: patient_location.created_at
}
]
end
end
def triage_events
triages.map do |triage|
title = "Triaged decision: #{triage.human_enum_name(:status)}"
if triage.vaccine_method.present? &&
triage.programme.has_multiple_vaccine_methods?
title += " with #{triage.human_enum_name(:vaccine_method)}"
end
{
title:,
body: triage.notes,
at: triage.created_at,
by: triage.performed_by,
programmes: [triage.programme]
}
end
end
def vaccination_events
vaccination_records.flat_map do |vaccination_record|
title =
if vaccination_record.administered?
if (vaccine = vaccination_record.vaccine)
"Vaccinated with #{vaccine.brand}"
else
"Vaccinated"
end
else
"Vaccination not given: #{vaccination_record.human_enum_name(:outcome)}"
end
kept = {
title:,
body: vaccination_record.notes,
at: vaccination_record.performed_at,
by: vaccination_record.performed_by,
programmes: [vaccination_record.programme]
}
discarded =
if vaccination_record.discarded?
{
title: "Vaccination record archived",
at: vaccination_record.discarded_at,
programmes: [vaccination_record.programme]
}
end
[kept, discarded].compact
end
end
def attendance_events
attendance_records.map do |attendance_record|
title =
(
if attendance_record.attending?
"Attended session"
else
"Absent from session"
end
)
title += " at #{attendance_record.location.name}"
{ title:, at: attendance_record.created_at }
end
end
private
def expired_items_for(academic_year:, programmes:)
{
consents:,
triages:,
patient_specific_directions:
}.transform_values do |items|
items
.select { it.academic_year == academic_year }
.map(&:programme)
.select { programmes.include?(it) }
end
end
end