Browse Source

Introduce base NotifyMailer to to abstract away shared Notify mail functionality

pull/851/head
James Rose 3 years ago
parent
commit
fa25b40bbf
  1. 5
      app/jobs/email_csv_job.rb
  2. 27
      app/mailers/csv_download_mailer.rb
  3. 37
      app/mailers/notify_mailer.rb

5
app/jobs/email_csv_job.rb

@ -3,6 +3,8 @@ class EmailCsvJob < ApplicationJob
BYTE_ORDER_MARK = "\uFEFF".freeze # Required to ensure Excel always reads CSV as UTF-8
EXPIRATION_TIME = 3.hours.to_i
def perform(user, search_term = nil, filters = {}, all_orgs = false, organisation = nil) # rubocop:disable Style/OptionalBooleanParameter
unfiltered_logs = organisation.present? && user.support? ? LettingsLog.all.where(owning_organisation_id: organisation.id) : user.lettings_logs
filtered_logs = FilterService.filter_lettings_logs(unfiltered_logs, search_term, filters, all_orgs, user)
@ -12,8 +14,7 @@ class EmailCsvJob < ApplicationJob
storage_service = Storage::S3Service.new(Configuration::PaasConfigurationService.new, ENV["CSV_DOWNLOAD_PAAS_INSTANCE"])
storage_service.write_file(filename, BYTE_ORDER_MARK + filtered_logs.to_csv(user))
duration = 3.hours.to_i
url = storage_service.get_presigned_url(filename, duration)
url = storage_service.get_presigned_url(filename, EXPIRATION_TIME)
CsvDownloadMailer.new.send_email(user, url, duration)
end

27
app/mailers/csv_download_mailer.rb

@ -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

37
app/mailers/notify_mailer.rb

@ -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…
Cancel
Save