From b52501cc88722f0aabd84080456b01ceef9aec00 Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Fri, 25 Oct 2024 19:00:32 +0100 Subject: [PATCH] Show notify log entries in activity logs When looking at the activity log for a patient, we can show the notify log entries which allows us to see clearly which emails and texts have been sent out. --- app/components/app_activity_log_component.rb | 29 ++++------------- app/models/notify_log_entry.rb | 15 +++++++++ config/locales/en.yml | 4 +++ .../app_activity_log_component_spec.rb | 31 ++++++------------- spec/models/notify_log_entry_spec.rb | 25 +++++++++++++++ 5 files changed, 59 insertions(+), 45 deletions(-) diff --git a/app/components/app_activity_log_component.rb b/app/components/app_activity_log_component.rb index 9a4ca94ba..6a2279a76 100644 --- a/app/components/app_activity_log_component.rb +++ b/app/components/app_activity_log_component.rb @@ -14,9 +14,8 @@ def events_by_day def all_events [ consent_events, - consent_notification_events, + notify_events, session_events, - session_notification_events, triage_events, vaccination_events ].flatten @@ -31,16 +30,12 @@ def consent_events end end - def consent_notification_events - @patient_session.patient.consent_notifications.map do + def notify_events + @patient_session.patient.notify_log_entries.map do { - title: - if _1.request? - "Consent request sent for #{_1.programme.name}" - else - "Consent reminder sent for #{_1.programme.name}" - end, - time: _1.sent_at + title: "#{_1.title} sent", + time: _1.created_at, + notes: @patient_session.patient.restricted? ? "" : _1.recipient } end end @@ -54,18 +49,6 @@ def session_events ] end - def session_notification_events - @patient_session.patient.session_notifications.map do |notification| - title = - if notification.school_reminder? - "School session reminder sent" - elsif notification.clinic_invitation? - "Clinic invitation sent" - end - { title:, time: notification.sent_at } - end - end - def triage_events @patient_session.triages.map do { diff --git a/app/models/notify_log_entry.rb b/app/models/notify_log_entry.rb index 36869f84b..66eb441c7 100644 --- a/app/models/notify_log_entry.rb +++ b/app/models/notify_log_entry.rb @@ -34,4 +34,19 @@ class NotifyLogEntry < ApplicationRecord validates :recipient, presence: true encrypts :recipient + + def title + template_name&.to_s&.humanize.presence || + "Unknown #{human_enum_name(:type)}" + end + + private + + def template_name + if email? + GOVUK_NOTIFY_EMAIL_TEMPLATES.key(template_id) + elsif sms? + GOVUK_NOTIFY_TEXT_TEMPLATES.key(template_id) + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 627f2dd05..08e8f0647 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -257,6 +257,10 @@ en: given: Consent given not_provided: Not provided refused: Consent refused + notify_log_entry: + types: + email: Email + sms: SMS parent: contact_method_types: any: No specific needs diff --git a/spec/components/app_activity_log_component_spec.rb b/spec/components/app_activity_log_component_spec.rb index f26c7d71f..ffeaedd2e 100644 --- a/spec/components/app_activity_log_component_spec.rb +++ b/spec/components/app_activity_log_component_spec.rb @@ -97,19 +97,13 @@ ) create( - :consent_notification, - :request, - programme:, - patient:, - sent_at: Date.new(2024, 5, 10) - ) - - create( - :session_notification, - :school_reminder, - session:, + :notify_log_entry, + :email, + template_id: GOVUK_NOTIFY_EMAIL_TEMPLATES[:consent_school_request], patient:, - sent_at: Date.new(2024, 5, 30) + consent_form: nil, + recipient: "test@example.com", + created_at: Date.new(2024, 5, 10) ) create( @@ -140,14 +134,6 @@ date: "31 May 2024 at 1:00pm", notes: "Some notes" - include_examples "card", - title: "School session reminder sent", - date: "30 May 2024 at 12:00am" - - include_examples "card", - title: "Clinic invitation sent", - date: "30 May 2024 at 12:00am" - include_examples "card", title: "Triaged decision: Safe to vaccinate", date: "30 May 2024 at 2:30pm", @@ -172,8 +158,9 @@ date: "29 May 2024 at 12:00pm" include_examples "card", - title: "Consent request sent for HPV", - date: "10 May 2024 at 12:00am" + title: "Consent school request sent", + date: "10 May 2024 at 12:00am", + notes: "test@example.com" end describe "self-consent" do diff --git a/spec/models/notify_log_entry_spec.rb b/spec/models/notify_log_entry_spec.rb index a2880b3e8..c74169228 100644 --- a/spec/models/notify_log_entry_spec.rb +++ b/spec/models/notify_log_entry_spec.rb @@ -36,4 +36,29 @@ it { should be_valid } end + + describe "#title" do + subject(:title) { notify_log_entry.title } + + context "with a known template" do + let(:notify_log_entry) do + build( + :notify_log_entry, + :email, + template_id: + GOVUK_NOTIFY_EMAIL_TEMPLATES.fetch(:consent_clinic_request) + ) + end + + it { should eq("Consent clinic request") } + end + + context "with an unknown template" do + let(:notify_log_entry) do + build(:notify_log_entry, :sms, template_id: SecureRandom.uuid) + end + + it { should eq("Unknown SMS") } + end + end end