Skip to content

Commit

Permalink
Merge pull request #1790 from tactilenews/1788_send_admin_email_when_…
Browse files Browse the repository at this point in the history
…onboarding_success_heading_or_text_changes

Send admin email notifying of change to welcome_message
  • Loading branch information
mattwr18 authored Mar 11, 2024
2 parents fbb64ac + b9cce43 commit 271a227
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/adapters/postmark_adapter/outbound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ def contributor_resubscribed!(admin, contributor)

with(admin: admin, contributor: contributor).contributor_resubscribed_email.deliver_later
end

def welcome_message_updated!(admin)
return unless admin&.email && admin&.admin?

with(admin: admin).welcome_message_updated_email.deliver_later
end
end

def bounce_email
Expand Down Expand Up @@ -150,6 +156,16 @@ def contributor_resubscribed_email
mail(to: admin.email, subject: subject, message_stream: message_stream)
end

def welcome_message_updated_email
admin = params[:admin]

subject = I18n.t('adapter.postmark.welcome_message_updated.subject', project_name: Setting.project_name)
text = I18n.t('adapter.postmark.welcome_message_updated.text')
message_stream = Setting.postmark_transactional_stream
@text = [subject, text].join("\n")
mail(to: admin.email, subject: subject, message_stream: message_stream)
end

def message_email
@msg = params[:message]
@text = msg.text
Expand Down
11 changes: 11 additions & 0 deletions app/models/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Setting < RailsSettings::Base

delegate :onboarding_logo, to: :class
delegate :onboarding_hero, to: :class
after_commit :notify_admin_of_welcome_message_change

def self.onboarding_logo
ActiveStorage::Blob.find_by(id: onboarding_logo_blob_id)
Expand Down Expand Up @@ -104,4 +105,14 @@ def self.onboarding_hero=(blob)
signal: true,
whats_app: true
}

private

def notify_admin_of_welcome_message_change
return unless var.match?(/onboarding_success/) && saved_change_to_value?

User.admin.find_each do |admin|
PostmarkAdapter::Outbound.welcome_message_updated!(admin)
end
end
end
3 changes: 3 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ de:
subject: "%{contributor_name} hat darum gebeten, keine weiteren Nachrichten über %{channel} von %{project_name} zu erhalten."
contributor_resubscribed_email:
subject: "%{contributor_name} hat erneut den Empfang von Nachrichten über %{channel} von %{project_name} angefordert."
welcome_message_updated:
subject: "%{project_name} hat seine Willkommensnachricht aktualisiert"
text: "Wenn die Instanz WhatsApp verwendet, muss eine neue Vorlage erstellt werden, bevor versucht wird, die Willkommensnachricht zu senden."
whats_app:
request_template:
new_request_morning_1: "Guten Morgen %{first_name}, wir haben eine neue Frage zum Thema „%{request_title}“. Wenn du antworten möchtest, klicke auf 'Antworten'."
Expand Down
82 changes: 82 additions & 0 deletions spec/models/setting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,86 @@
end
end
end

context '::onboarding_success_*' do
let!(:users) { create_list(:user, 2) }
let!(:admin) { create_list(:user, 3, admin: true) }
let(:param) { 'new value' }

describe 'onboarding_success_heading' do
let(:default_value) { File.read(File.join('config', 'locales', 'onboarding', 'success_heading.txt')) }

context 'getter' do
subject { Setting.send(method) }

let(:method) { :onboarding_success_heading }

it { is_expected.to eq(default_value) }
end

context 'setter' do
subject { Setting.send(method, param) }

let(:method) { 'onboarding_success_heading=' }

context 'setter other than onboarding_success_*' do
let(:method) { 'project_name=' }

it 'does not notify admin' do
expect { subject }.not_to have_enqueued_job
end
end

it 'updates the value' do
expect { subject }.to change(Setting, :onboarding_success_heading).from(default_value).to(param)
end

it 'sends an email to all admin that the value was updated' do
expect { subject }.to have_enqueued_job.on_queue('default').with(
'PostmarkAdapter::Outbound',
'welcome_message_updated_email',
'deliver_now', # How ActionMailer works in test environment, even though in production we call deliver_later
{
params: { admin: an_instance_of(User) },
args: []
}
).exactly(3).times
end
end
end

describe 'onboarding_success_text' do
let(:default_value) { File.read(File.join('config', 'locales', 'onboarding', 'success_text.txt')) }

context 'getter' do
subject { Setting.send(method) }

let(:method) { :onboarding_success_text }

it { is_expected.to eq(default_value) }
end

context 'setter' do
subject { Setting.send(method, param) }

let(:method) { 'onboarding_success_text=' }

it 'updates the value' do
expect { subject }.to change(Setting, :onboarding_success_text).from(default_value).to(param)
end

it 'sends an email to all admin that the value was updated' do
expect { subject }.to have_enqueued_job.on_queue('default').with(
'PostmarkAdapter::Outbound',
'welcome_message_updated_email',
'deliver_now', # How ActionMailer works in test environment, even though in production we call deliver_later
{
params: { admin: an_instance_of(User) },
args: []
}
).exactly(3).times
end
end
end
end
end

0 comments on commit 271a227

Please sign in to comment.