-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mail event log, metrics and scopes (#1208)
* Job and AppplicationJob coverage * Rework mailer jobs for postponed delivery with a mail event log * Defend against callback error and report failure * retest mail event callback update * New Module recipient exclusion for sent and pending mail Coverage 92.55% * Fix yard doc * Fix logging to Sentry upon delivery Ensure only mail events only receive callbacks once Document mail job scopes that require remedial work * Silence failed delivery alerts in prod Serialise Contentful module entries for background mail jobs * Parent mailer job coverage
- Loading branch information
1 parent
45f3730
commit 1284640
Showing
39 changed files
with
523 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,31 @@ | ||
# Notify only supports a single callback URL | ||
# | ||
class NotifyController < WebhookController | ||
# @see https://docs.notifications.service.gov.uk/ruby.html#delivery-receipts | ||
def update | ||
user = User.find_by(email: payload['to']) | ||
user.update!(notify_callback: payload) if user | ||
render json: { status: 'callback received' }, status: :ok | ||
if recipient | ||
recipient.update!(notify_callback: payload) | ||
mail_event.update!(callback: payload) if mail_event | ||
render json: { status: 'callback received' }, status: :ok | ||
else | ||
render json: { status: 'callback received' }, status: :not_modified | ||
end | ||
end | ||
|
||
private | ||
|
||
# @return [String] | ||
# @return [Boolean] | ||
def bot_token? | ||
request.headers['Authorization']&.include?(Rails.configuration.bot_token) | ||
end | ||
|
||
# @return [User, nil] | ||
def recipient | ||
User.find_by(email: payload['to']) | ||
end | ||
|
||
# @return [MailEvent, nil] | ||
def mail_event | ||
recipient.mail_events.where(template: payload['template_id'], callback: nil).last | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
class CompleteRegistrationMailJob < MailJob | ||
def run | ||
super do | ||
self.class.recipients.each(&:send_complete_registration_notification) | ||
self.class.recipients.find_each do |user| | ||
prepare_message(user) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,40 @@ | ||
# Base class for NotifyMailer jobs | ||
# | ||
class MailJob < ApplicationJob | ||
# @return [Array<User>] | ||
def self.recipients | ||
scope_name = "#{name.underscore}_recipients" | ||
User.send(scope_name) | ||
end | ||
|
||
# @return [Symbol] | ||
def self.template | ||
name.underscore.delete_suffix('_mail_job').to_sym | ||
end | ||
|
||
# @return [String] | ||
def self.template_id | ||
NotifyMailer::TEMPLATE_IDS[template] | ||
end | ||
|
||
# @return [Boolean] | ||
def self.enqueue? | ||
Types::Params::Bool[ENV.fetch('DELIVERY_QUEUE', Rails.env.production?)] | ||
end | ||
|
||
def run(*) | ||
log "#{self.class.recipients.count} recipients" | ||
|
||
super | ||
end | ||
|
||
private | ||
|
||
# @note Mailer arguments must be able to be serialised for delayed delivery | ||
# @param args [Array] | ||
# @return [Mail::Message, ActionMailer::MailDeliveryJob] | ||
def prepare_message(*args) | ||
message = NotifyMailer.send(self.class.template, *args) | ||
self.class.enqueue? ? message.deliver_later : message.deliver_now | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
class StartTrainingMailJob < MailJob | ||
def run | ||
super do | ||
self.class.recipients.each(&:send_start_training_notification) | ||
self.class.recipients.find_each do |user| | ||
prepare_message(user) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,14 @@ | ||
# Simple background job using a dummy template | ||
class TestBulkMailJob < MailJob | ||
# @note guard clause prevents testing against production data | ||
# | ||
def run | ||
expire if Rails.application.live? | ||
|
||
super do | ||
self.class.recipients.find_each do |recipient| | ||
recipient.test_email | ||
|
||
# TODO: user record log of mail activity? | ||
# recipient.update!(notify_log:) | ||
# where("notify_callback -> 'template_id' ?", template_id) | ||
self.class.recipients.find_each do |user| | ||
prepare_message(user) | ||
end | ||
end | ||
|
||
private | ||
|
||
# @return [String] | ||
def template_id | ||
NotifyMailer::TEMPLATE_IDS[:bulk_test] | ||
end | ||
end | ||
end |
Oops, something went wrong.