Skip to content

Commit

Permalink
Merged in r2-3065-email-changes (pull request #6968)
Browse files Browse the repository at this point in the history
R2-3065, R2-3074: Changing email styling, making portions of emails configurable and making copy changes
  • Loading branch information
jtoliver-quoin authored and pnabutovsky committed Dec 20, 2024
2 parents 1051b91 + 93b6507 commit ef1f5e2
Show file tree
Hide file tree
Showing 40 changed files with 813 additions and 328 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v2/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def load_extended
end

def welcome
@user.send_welcome_email(current_user)
@user.send_welcome_email
end

def identity_sync
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
# Default Rails route
class HomeController < ApplicationController
def v2
@theme = Rails.configuration.use_theme ? Theme.current : Theme.default
@theme = Theme.current
end
end
2 changes: 1 addition & 1 deletion app/controllers/themes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index; end
def manifest; end

def theme
@theme = Rails.configuration.use_theme ? Theme.current : Theme.default
@theme = Theme.current
end

def request_not_from_app_host?
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/user_mail_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class UserMailJob < ApplicationJob
queue_as :mailer

def perform(user_id, admin_user_id)
UserMailer.welcome(user_id, admin_user_id).deliver_now
def perform(user_id)
UserMailer.welcome(user_id).deliver_now
end
end
11 changes: 11 additions & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

# Superclass for all Mailers
class ApplicationMailer < ActionMailer::Base
before_action :load_theme
before_action :system_admin

layout 'mailer'

rescue_from StandardError do |error|
Expand All @@ -22,4 +25,12 @@ def log_mailer_error(error)
Rails.logger.error(error.message)
end
end

def load_theme
@theme = Theme.current
end

def system_admin
@system_admin = ContactInformation.first
end
end
1 change: 1 addition & 0 deletions app/mailers/devise_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Class for DeviseMailer
class DeviseMailer < Devise::Mailer
def reset_password_instructions(record, token, opts = {})
@locale = (record&.locale || I18n.locale)
opts[:subject] = t('user.password_reset.subject', locale: (record&.locale || I18n.locale))
super
end
Expand Down
5 changes: 4 additions & 1 deletion app/mailers/onboard_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
class OnboardMailer < ApplicationMailer
def onboard(user_id)
@user = User.find(user_id)
@subject = I18n.t('user.onboard_email.subject', system: @theme.get('site_title'), locale: @user.locale)
@locale = @user.locale

mail(
to: @user.email,
subject: I18n.t('user.onboard_email.subject', system: SystemSettings.current.system_name, locale: @user.locale)
subject: @subject
)
end
end
22 changes: 16 additions & 6 deletions app/mailers/record_action_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,62 @@ class RecordActionMailer < ApplicationMailer

def manager_approval_request(approval_notification)
@approval_notification = approval_notification

@subject = @approval_notification.subject
@locale = @approval_notification.locale
return unless @approval_notification.send_notification?
return unless assert_notifications_enabled(@approval_notification.manager, Approval::NOTIFICATION_ACTIONS_REQUEST)

mail(to: @approval_notification.manager.email, subject: @approval_notification.subject)
mail(to: @approval_notification.manager.email, subject: @subject)
end

def manager_approval_response(approval_notification)
@approval_notification = approval_notification
@subject = @approval_notification.subject
@locale = @approval_notification.locale

return unless @approval_notification.send_notification?
return unless assert_notifications_enabled(@approval_notification.owner, Approval::NOTIFICATION_ACTIONS_RESPONSE)

mail(to: @approval_notification.owner.email, subject: @approval_notification.subject)
mail(to: @approval_notification.owner.email, subject: @subject)
end

def transition_notify(transition_notification)
@transition_notification = transition_notification
@subject = transition_notification.subject
@locale = @transition_notification.locale

return if @transition_notification.transition.nil?
return unless assert_notifications_enabled(
@transition_notification.transitioned_to, Transition::NOTIFICATION_ACTION
)

mail(to: @transition_notification&.transitioned_to&.email, subject: transition_notification.subject)
mail(to: @transition_notification&.transitioned_to&.email, subject: @subject)
end

def transfer_request(transfer_request_notification)
@transfer_request_notification = transfer_request_notification
@subject = @transfer_request_notification.subject
@locale = @transfer_request_notification.locale

return if @transfer_request_notification.transition.nil?
return unless assert_notifications_enabled(
@transfer_request_notification.transitioned_to, Transfer::NOTIFICATION_ACTION
)

mail(to: @transfer_request_notification&.transitioned_to&.email, subject: @transfer_request_notification.subject)
mail(to: @transfer_request_notification&.transitioned_to&.email, subject: @subject)
end

def alert_notify(alert_notification)
@alert_notification = alert_notification
@subject = @alert_notification.subject
@locale = @alert_notification.locale

return unless assert_notifications_enabled(@alert_notification.user)
return if @alert_notification.user == @alert_notification.record.last_updated_by

Rails.logger.info("Sending alert notification to #{@alert_notification.user.user_name}")

mail(to: @alert_notification.user.email, subject: @alert_notification.subject, locale: @alert_notification.locale)
mail(to: @alert_notification.user.email, subject: @subject, locale: @alert_notification.locale)
end

private
Expand Down
72 changes: 35 additions & 37 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,85 @@

# Sends out notifications to the email associated with this User.
class UserMailer < ApplicationMailer
def welcome(user_id, admin_user_id, one_time_password = nil)
user, admin = load_users!(user_id, admin_user_id)
@email_body = email_body(user, admin, one_time_password)
@email_greeting = greeting(user)
def welcome(user_id, one_time_password = nil)
load_users!(user_id)
@email_body = email_body(@user, one_time_password)
@email_greeting = greeting(@user)
@subject = subject(@user)
@locale = @user.locale

mail(
to: user.email,
subject: subject(user)
to: @user.email,
subject: @subject
)
end

private

def subject(user)
key = user.using_idp? ? 'subject' : 'subject_instructions'
I18n.t(
'user.welcome_email.subject',
system: SystemSettings.current.system_name,
"user.welcome_email.#{key}",
system: @theme.get('site_title'),
locale: user.locale
)
end

def greeting(user)
I18n.t(
'user.welcome_email.greeting',
system: SystemSettings.current.system_name,
system: @theme.get('site_title'),
locale: user.locale
)
end

def email_body(user, admin, one_time_password)
return email_body_native(user, admin) unless user.using_idp?
def email_body(user, one_time_password)
return email_body_native(user) unless user.using_idp?

if one_time_password
email_body_otp(user, admin, one_time_password)
email_body_otp(user, one_time_password)
else
email_body_sso(user, admin)
email_body_sso(user)
end
end

def email_body_native(user, admin)
def email_body_native(user)
I18n.t(
'user.welcome_email.body_native',
role_name: user.role.name,
admin_full_name: admin.full_name,
admin_email: admin.email,
host: root_url,
greeting: @theme.t('email_welcome_greeting', user.locale),
locale: user.locale
)
end

# rubocop:disable Metrics/AbcSize
def email_body_sso(user, admin)
def email_body_sso(user)
idp_name = user.identity_provider&.name
prefix = 'user.welcome_email.sso.'

{
header: I18n.t("#{prefix}body", role_name: user.role.name, locale: user.locale),
step1: I18n.t("#{prefix}step1", host: root_url, identity_provider: idp_name, locale: user.locale),
step2: I18n.t("#{prefix}step2", identity_provider: idp_name, user_name: user.user_name, locale: user.locale),
step3: I18n.t("#{prefix}step3", identity_provider: idp_name, locale: user.locale),
step4: '',
footer: I18n.t("#{prefix}footer", admin_full_name: admin.full_name, admin_email: admin.email, locale: user.locale)
step1: I18n.t("#{prefix}step1", system: site_path(@theme.get('site_title')),
product_name: @theme.get('product_name'),
identity_provider: idp_name, locale: user.locale),
step2: I18n.t("#{prefix}step2", product_name: @theme.get('product_name'), host: root_url,
identity_provider: idp_name)
}
end
# rubocop:enable Metrics/AbcSize

def email_body_otp(user, admin, one_time_password)
def email_body_otp(user, one_time_password)
prefix = 'user.welcome_email.otp.'
{
header: I18n.t("#{prefix}body", role_name: user.role.name, locale: user.locale),
step1: I18n.t("#{prefix}step1", admin_full_name: admin.full_name, admin_email: admin.email, locale: user.locale),
step2: I18n.t("#{prefix}step2", host: root_url, locale: user.locale),
# TODO: OTP is currently an empty string to avoid re-translation. Address when we change the message.
step3: I18n.t("#{prefix}step3", otp: '', locale: user.locale),
otp: one_time_password,
step4: I18n.t("#{prefix}step4", locale: user.locale),
footer: ''
step1: I18n.t("#{prefix}step1", site_title: @theme.get('site_title'), locale: user.locale),
step2: I18n.t("#{prefix}step2", otp: one_time_password, host: root_url, locale: user.locale)
}
end

def load_users!(user_id, admin_user_id)
user = User.find(user_id)
admin_user = User.find(admin_user_id)
[user, admin_user]
def load_users!(user_id)
@user = User.find(user_id)
end

def site_path(name, path = root_url)
ActionController::Base.helpers.link_to(name, path, style: "color: #{@theme.get('email_link_color')}")
end
end
49 changes: 45 additions & 4 deletions app/models/importers/yml_config_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@
class Importers::YmlConfigImporter < ValueObject
attr_accessor :file_name, :class_to_import, :locale, :errors, :failures

IMPORTABLE_CLASSES = %w[FormSection Lookup Theme].freeze

def initialize(opts = {})
if opts[:file_name].present?
opts[:class_to_import] = opts[:file_name].downcase.include?('lookup') ? 'Lookup' : 'FormSection'
end
opts[:class_to_import] = selected_import_class(opts[:file_name]) if opts[:file_name].present?
opts.merge!(errors: [], failures: [])
super(opts)
end

# TODO: Refactor when other classes are needed for import
def selected_import_class(filename = '')
filename_str = filename.downcase

if filename_str.include?('system')
%w[Theme]
elsif filename_str.include?('lookup')
'Lookup'
else
'FormSection'
end
end

def import
return log_errors('Import Not Processed: No file_name passed in') if file_name.blank?

Expand Down Expand Up @@ -51,10 +64,23 @@ def valid_locale(config_data)
locale
end

def import_class(model_class, config)
return unless IMPORTABLE_CLASSES.include?(model_class)

send("import_#{model_class.underscore}", locale, config)
end

def process_config_data(config_data)
config_data.each_value do |config|
config = strip_hash_values!(config)
send("import_#{class_to_import.underscore}", locale, config) if %w[FormSection Lookup].include?(class_to_import)

if class_to_import.is_a?(Array)
class_to_import.each do |model_class|
import_class(model_class, config)
end
else
import_class(class_to_import, config)
end
end
end

Expand Down Expand Up @@ -85,6 +111,21 @@ def import_lookup(locale, config)
end
end

# rubocop:disable Metrics/AbcSize
def import_theme(locale, config)
theme = Theme.current

config['theme'].each do |key, value|
theme.data[key] = {} unless theme.data[key].present?
theme.data[key][locale.to_s] = value
end

Rails.logger.info 'Updating theme'
theme.bypass_logos = true
theme.save!
end
# rubocop:enable Metrics/AbcSize

def strip_hash_values!(hash)
hash.each_value do |value|
case value
Expand Down
Loading

0 comments on commit ef1f5e2

Please sign in to comment.