|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# == Schema Information |
| 4 | +# |
| 5 | +# Table name: important_notices |
| 6 | +# |
| 7 | +# id :bigint not null, primary key |
| 8 | +# dismissed_at :datetime |
| 9 | +# recorded_at :datetime not null |
| 10 | +# type :integer not null |
| 11 | +# created_at :datetime not null |
| 12 | +# updated_at :datetime not null |
| 13 | +# dismissed_by_user_id :bigint |
| 14 | +# patient_id :bigint not null |
| 15 | +# team_id :bigint not null |
| 16 | +# vaccination_record_id :bigint |
| 17 | +# |
| 18 | +# Indexes |
| 19 | +# |
| 20 | +# index_important_notices_on_dismissed_by_user_id (dismissed_by_user_id) |
| 21 | +# index_important_notices_on_patient_id (patient_id) |
| 22 | +# index_important_notices_on_team_id (team_id) |
| 23 | +# index_important_notices_on_vaccination_record_id (vaccination_record_id) |
| 24 | +# index_notices_on_patient_and_type_and_recorded_at_and_team (patient_id,type,recorded_at,team_id) UNIQUE |
| 25 | +# |
| 26 | +# Foreign Keys |
| 27 | +# |
| 28 | +# fk_rails_... (dismissed_by_user_id => users.id) |
| 29 | +# fk_rails_... (patient_id => patients.id) |
| 30 | +# fk_rails_... (team_id => teams.id) |
| 31 | +# fk_rails_... (vaccination_record_id => vaccination_records.id) |
| 32 | +# |
| 33 | +class ImportantNotice < ApplicationRecord |
| 34 | + self.inheritance_column = nil |
| 35 | + |
| 36 | + belongs_to :patient |
| 37 | + |
| 38 | + belongs_to :vaccination_record, optional: true |
| 39 | + |
| 40 | + enum :type, |
| 41 | + { deceased: 0, invalidated: 1, restricted: 2, gillick_no_notify: 3 } |
| 42 | + |
| 43 | + scope :active, ->(team:) { where(dismissed_at: nil, team_id: team.id) } |
| 44 | + scope :dismissed, |
| 45 | + ->(team:) { where.not(dismissed_at: nil).where(team_id: team.id) } |
| 46 | + |
| 47 | + validates :type, presence: true |
| 48 | + validates :recorded_at, presence: true |
| 49 | + validates :message, presence: true |
| 50 | + |
| 51 | + def dismiss!(user: nil) |
| 52 | + update!(dismissed_at: Time.current, dismissed_by_user_id: user&.id) |
| 53 | + end |
| 54 | + |
| 55 | + def message |
| 56 | + case type |
| 57 | + when "deceased" |
| 58 | + "Record updated with child’s date of death" |
| 59 | + when "invalidated" |
| 60 | + "Record flagged as invalid" |
| 61 | + when "restricted" |
| 62 | + "Record flagged as sensitive" |
| 63 | + when "gillick_no_notify" |
| 64 | + "Child gave consent for #{vaccination_record.programme.name} under Gillick competence and " \ |
| 65 | + "does not want their parents to be notified. " \ |
| 66 | + "These records will not be automatically synced with GP records. " \ |
| 67 | + "Your team must let the child's GP know they were vaccinated." |
| 68 | + else |
| 69 | + "Important notice" |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + def can_dismiss? |
| 74 | + type.in?(%w[deceased restricted gillick_no_notify]) |
| 75 | + end |
| 76 | +end |
0 commit comments