|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class ImportantNotices |
| 4 | + def initialize(patient_scope: nil, patient: nil) |
| 5 | + @patient_scope = patient_scope |
| 6 | + @patient = patient |
| 7 | + |
| 8 | + if patient_scope.nil? && patient.nil? |
| 9 | + raise "Pass either a patient_scope or a patient." |
| 10 | + end |
| 11 | + end |
| 12 | + |
| 13 | + def call |
| 14 | + notices = |
| 15 | + patients.flat_map do |patient| |
| 16 | + notices_for_patient(patient).map { it.merge(patient:) } |
| 17 | + end |
| 18 | + |
| 19 | + notices.sort_by { it[:date_time] }.reverse |
| 20 | + end |
| 21 | + |
| 22 | + def self.call(...) = new(...).call |
| 23 | + |
| 24 | + private_class_method :new |
| 25 | + |
| 26 | + private |
| 27 | + |
| 28 | + attr_reader :patient_scope, :patient |
| 29 | + |
| 30 | + def patients |
| 31 | + if patient |
| 32 | + [patient] |
| 33 | + else |
| 34 | + patient_scope_with_notices.includes(vaccination_records: :programme) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + def patient_scope_with_notices |
| 39 | + patient_scope |
| 40 | + .deceased |
| 41 | + .or(patient_scope.invalidated) |
| 42 | + .or(patient_scope.restricted) |
| 43 | + .or(patient_scope.has_vaccination_records_dont_notify_parents) |
| 44 | + end |
| 45 | + |
| 46 | + def notices_for_patient(patient) |
| 47 | + notices = [] |
| 48 | + |
| 49 | + if patient.deceased? |
| 50 | + notices << { |
| 51 | + date_time: patient.date_of_death_recorded_at, |
| 52 | + message: "Record updated with child’s date of death" |
| 53 | + } |
| 54 | + end |
| 55 | + |
| 56 | + if patient.invalidated? |
| 57 | + notices << { |
| 58 | + date_time: patient.invalidated_at, |
| 59 | + message: "Record flagged as invalid" |
| 60 | + } |
| 61 | + end |
| 62 | + |
| 63 | + if patient.restricted? |
| 64 | + notices << { |
| 65 | + date_time: patient.restricted_at, |
| 66 | + message: "Record flagged as sensitive" |
| 67 | + } |
| 68 | + end |
| 69 | + |
| 70 | + no_notify_vaccination_records = |
| 71 | + patient.vaccination_records.select { it.notify_parents == false } |
| 72 | + if no_notify_vaccination_records.any? |
| 73 | + vaccinations_sentence = |
| 74 | + "#{no_notify_vaccination_records.map(&:programme).uniq.map(&:name).to_sentence} " \ |
| 75 | + "#{"vaccination".pluralize(no_notify_vaccination_records.length)}" |
| 76 | + |
| 77 | + notices << { |
| 78 | + date_time: no_notify_vaccination_records.maximum(:performed_at), |
| 79 | + message: |
| 80 | + "Child gave consent for #{vaccinations_sentence} under Gillick competence and " \ |
| 81 | + "does not want their parents to be notified. " \ |
| 82 | + "These records will not be automatically synced with GP records. " \ |
| 83 | + "Your team must let the child's GP know they were vaccinated." |
| 84 | + } |
| 85 | + end |
| 86 | + |
| 87 | + notices |
| 88 | + end |
| 89 | +end |
0 commit comments