3 changed files with 44 additions and 25 deletions
@ -1,30 +1,11 @@
|
||||
class CsvDownloadMailer |
||||
require "notifications/client" |
||||
|
||||
class CsvDownloadMailer < NotifyMailer |
||||
CSV_DOWNLOAD_TEMPLATE_ID = "7890e3b9-8c0d-4d08-bafe-427fd7cd95bf".freeze |
||||
|
||||
def notify_client |
||||
@notify_client ||= ::Notifications::Client.new(ENV["GOVUK_NOTIFY_API_KEY"]) |
||||
end |
||||
|
||||
def send_email(user, link, duration) |
||||
return true if intercept_send?(user.email) |
||||
|
||||
notify_client.send_email( |
||||
def send_csv_download_mail(user, link, duration) |
||||
send_email( |
||||
email_address: user.email, |
||||
template_id: CSV_DOWNLOAD_TEMPLATE_ID, |
||||
personalisation: { name: user.name || user.email, link:, duration: ActiveSupport::Duration.build(duration).inspect }, |
||||
personalisation: { name: user.name, link:, duration: ActiveSupport::Duration.build(duration).inspect }, |
||||
) |
||||
end |
||||
|
||||
def intercept_send?(email) |
||||
return false unless email_allowlist |
||||
|
||||
email_domain = email.split("@").last.downcase |
||||
!(Rails.env.production? || Rails.env.test?) && email_allowlist.exclude?(email_domain) |
||||
end |
||||
|
||||
def email_allowlist |
||||
Rails.application.credentials[:email_allowlist] |
||||
end |
||||
end |
||||
|
@ -0,0 +1,37 @@
|
||||
class NotifyMailer |
||||
require "notifications/client" |
||||
|
||||
def notify_client |
||||
@notify_client ||= ::Notifications::Client.new(ENV["GOVUK_NOTIFY_API_KEY"]) |
||||
end |
||||
|
||||
def send_email(email, template_id, personalisation) |
||||
return true if intercept_send?(email) |
||||
|
||||
notify_client.send_email( |
||||
email_address: email, |
||||
template_id:, |
||||
personalisation:, |
||||
) |
||||
end |
||||
|
||||
def personalisation(record, token, url, username: false) |
||||
{ |
||||
name: record.name || record.email, |
||||
email: username || record.email, |
||||
organisation: record.respond_to?(:organisation) ? record.organisation.name : "", |
||||
link: "#{url}#{token}", |
||||
} |
||||
end |
||||
|
||||
def intercept_send?(email) |
||||
return false unless email_allowlist |
||||
|
||||
email_domain = email.split("@").last.downcase |
||||
!(Rails.env.production? || Rails.env.test?) && email_allowlist.exclude?(email_domain) |
||||
end |
||||
|
||||
def email_allowlist |
||||
Rails.application.credentials[:email_allowlist] |
||||
end |
||||
end |
Loading…
Reference in new issue