Skip to content

Commit

Permalink
Add mail success/failure running total and redact notify callback on …
Browse files Browse the repository at this point in the history
…user (#1231)
  • Loading branch information
peterdavidhamilton authored Jun 19, 2024
1 parent e991959 commit 498ce90
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
4 changes: 4 additions & 0 deletions app/models/data_analysis/user_overview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def column_names
'Terms and Conditions Agreed',
'Received Mail Yesterday',
'Received Mail Today',
'User Email Success',
'User Email Fail',
]
end

Expand Down Expand Up @@ -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

Expand Down
13 changes: 7 additions & 6 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion spec/models/data_analysis/user_overview_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
'Terms and Conditions Agreed',
'Received Mail Yesterday',
'Received Mail Today',
'User Email Success',
'User Email Fail',
]
end

Expand Down Expand Up @@ -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,
Expand All @@ -74,19 +76,23 @@
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: '[email protected]', notify_callback: {
notification_type: 'email', sent_at: Time.zone.today, status: 'delivered'
}
end

let(:user_2) do
create :user, :registered,
early_years_emails: true,
module_time_to_completion: { alpha: 1, bravo: 1, charlie: 0 }
end

Expand Down
1 change: 1 addition & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions spec/requests/webhooks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 498ce90

Please sign in to comment.