3 changed files with 44 additions and 25 deletions
@ -1,30 +1,11 @@ |
|||||||
class CsvDownloadMailer |
class CsvDownloadMailer < NotifyMailer |
||||||
require "notifications/client" |
|
||||||
|
|
||||||
CSV_DOWNLOAD_TEMPLATE_ID = "7890e3b9-8c0d-4d08-bafe-427fd7cd95bf".freeze |
CSV_DOWNLOAD_TEMPLATE_ID = "7890e3b9-8c0d-4d08-bafe-427fd7cd95bf".freeze |
||||||
|
|
||||||
def notify_client |
def send_csv_download_mail(user, link, duration) |
||||||
@notify_client ||= ::Notifications::Client.new(ENV["GOVUK_NOTIFY_API_KEY"]) |
send_email( |
||||||
end |
|
||||||
|
|
||||||
def send_email(user, link, duration) |
|
||||||
return true if intercept_send?(user.email) |
|
||||||
|
|
||||||
notify_client.send_email( |
|
||||||
email_address: user.email, |
email_address: user.email, |
||||||
template_id: CSV_DOWNLOAD_TEMPLATE_ID, |
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 |
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 |
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