From 498ce90c255f1c50c09093a21ec6b268ffef98f3 Mon Sep 17 00:00:00 2001 From: Peter Hamilton Date: Wed, 19 Jun 2024 15:22:29 +0100 Subject: [PATCH] Add mail success/failure running total and redact notify callback on user (#1231) --- app/models/data_analysis/user_overview.rb | 4 ++++ app/models/user.rb | 13 +++++++------ spec/models/data_analysis/user_overview_spec.rb | 8 +++++++- spec/models/user_spec.rb | 1 + spec/requests/webhooks_spec.rb | 4 +--- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/app/models/data_analysis/user_overview.rb b/app/models/data_analysis/user_overview.rb index 54f319cb7..9e4da14b3 100644 --- a/app/models/data_analysis/user_overview.rb +++ b/app/models/data_analysis/user_overview.rb @@ -38,6 +38,8 @@ def column_names 'Terms and Conditions Agreed', 'Received Mail Yesterday', 'Received Mail Today', + 'User Email Success', + 'User Email Fail', ] end @@ -76,6 +78,8 @@ def dashboard terms_and_conditions_agreed: terms_and_conditions_agreed_count, mail_yesterday: User.email_delivered_days_ago(1).count, mail_today: User.email_delivered_today.count, + mail_delivered: User.email_status('delivered').count, + mail_undelivered: User.email_status('undelivered').count, }] end diff --git a/app/models/user.rb b/app/models/user.rb index 2018c5227..46ea6a4ac 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -170,15 +170,15 @@ def test_user? scope :with_mail_events, -> { joins(:mail_events) } scope :with_new_module_mail_events, -> { with_mail_events.merge(MailEvent.newest_module).distinct } - scope :email_delivered, lambda { - training_email_recipients.or(early_years_email_recipients).where('notify_callback @> ?', { notification_type: 'email', status: 'delivered' }.to_json).distinct + scope :email_status, lambda { |status| + training_email_recipients.or(early_years_email_recipients).where('notify_callback @> ?', { notification_type: 'email', status: status }.to_json).distinct } scope :email_delivered_days_ago, lambda { |num| - email_delivered.where("CAST(notify_callback ->> 'sent_at' AS DATE) = CURRENT_DATE - #{num}") + email_status('delivered').where("CAST(notify_callback ->> 'sent_at' AS DATE) = CURRENT_DATE - #{num}") } scope :email_delivered_today, -> { email_delivered_days_ago(0) } scope :last_email_delivered, lambda { |template_id| - email_delivered.where('notify_callback @> ?', { template_id: template_id }.to_json) + email_status('delivered').where('notify_callback @> ?', { template_id: template_id }.to_json) } # data @@ -254,7 +254,7 @@ def name # @return [String] def email_delivery_status - notify_callback.to_h.fetch('status', 'unknown') + notify_callback.to_h.symbolize_keys.fetch(:status, 'unknown') end # @return [String] @@ -408,7 +408,8 @@ def redact! last_name: 'User', email: "redacted_user#{id}@example.com", closed_at: Time.zone.now, - password: 'RedactedUser12!@') + password: 'RedactedUser12!@', + notify_callback: nil) mail_events.destroy_all notes.destroy_all diff --git a/spec/models/data_analysis/user_overview_spec.rb b/spec/models/data_analysis/user_overview_spec.rb index 42154cb96..63b991a9b 100644 --- a/spec/models/data_analysis/user_overview_spec.rb +++ b/spec/models/data_analysis/user_overview_spec.rb @@ -36,6 +36,8 @@ 'Terms and Conditions Agreed', 'Received Mail Yesterday', 'Received Mail Today', + 'User Email Success', + 'User Email Fail', ] end @@ -64,7 +66,7 @@ without_notes: 4, without_notes_percentage: 0.67, training_mail_recipients: 6, - early_years_mail_recipients: 0, + early_years_mail_recipients: 2, complete_registration_mail_recipients: 1, start_training_mail_recipients: 1, continue_training_mail_recipients: 0, @@ -74,12 +76,15 @@ terms_and_conditions_agreed: 6, mail_yesterday: 0, mail_today: 1, + mail_delivered: 1, + mail_undelivered: 0, }, ] end let(:user) do create :user, :registered, + early_years_emails: true, email: 'user@education.gov.uk', notify_callback: { notification_type: 'email', sent_at: Time.zone.today, status: 'delivered' } @@ -87,6 +92,7 @@ let(:user_2) do create :user, :registered, + early_years_emails: true, module_time_to_completion: { alpha: 1, bravo: 1, charlie: 0 } end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 6aa41ed8d..0e4ce39ea 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -199,6 +199,7 @@ expect(user.gov_one_id).to start_with "#{user.id}urn:fdc:gov.uk:2022:" expect(user.first_name).to eq 'Redacted' expect(user.last_name).to eq 'User' + expect(user.notify_callback).to be_nil expect(user.email).to eq "redacted_user#{user.id}@example.com" expect(user.valid_password?('RedactedUser12!@')).to eq true expect(user.closed_at).to be_within(30).of(Time.zone.now) diff --git a/spec/requests/webhooks_spec.rb b/spec/requests/webhooks_spec.rb index 24de99cbb..817d9cad5 100644 --- a/spec/requests/webhooks_spec.rb +++ b/spec/requests/webhooks_spec.rb @@ -55,9 +55,7 @@ it 'persists the callback' do post '/notify', params: notify, as: :json, headers: headers expect(response).to have_http_status(:ok) - expect(User.email_delivered).to include user.reload - # expect(user.mail_events.last.callback).to eq notify - # expect(user.notify_callback).to eq notify + expect(User.email_status('delivered')).to include user.reload end end end