diff --git a/app/models/account.rb b/app/models/account.rb
index efdab34810..36ae405746 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -47,6 +47,8 @@ class Account < ApplicationRecord
format: { with: /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/ },
unless: proc { |a| a.tenant == 'public' || a.tenant == 'single' }
+ after_save :schedule_jobs_if_settings_changed
+
def self.admin_host
host = ENV.fetch('HYKU_ADMIN_HOST', nil)
host ||= ENV['HOST']
@@ -176,16 +178,43 @@ def find_job(klass)
def find_or_schedule_jobs
account = Site.account
AccountElevator.switch!(self)
- [
+
+ jobs_to_schedule = [
EmbargoAutoExpiryJob,
- LeaseAutoExpiryJob,
- BatchEmailNotificationJob,
- DepositorEmailNotificationJob,
- UserStatCollectionJob
- ].each do |klass|
+ LeaseAutoExpiryJob
+ ]
+
+ jobs_to_schedule << BatchEmailNotificationJob if batch_email_notifications
+ jobs_to_schedule << DepositorEmailNotificationJob if depositor_email_notifications
+ jobs_to_schedule << UserStatCollectionJob if user_analytics
+
+ jobs_to_schedule.each do |klass|
klass.perform_later unless find_job(klass)
end
+
account ? AccountElevator.switch!(account) : reset!
end
+
+ private
+
+ def schedule_jobs_if_settings_changed
+ return unless settings
+
+ relevant_settings = [
+ 'batch_email_notifications',
+ 'depositor_email_notifications',
+ 'user_analytics'
+ ]
+
+ return unless saved_changes['settings']
+ old_settings = saved_changes['settings'][0] || {}
+ new_settings = saved_changes['settings'][1] || {}
+
+ old_relevant_settings = old_settings.slice(*relevant_settings)
+ new_relevant_settings = new_settings.slice(*relevant_settings)
+
+ return unless old_relevant_settings != new_relevant_settings
+ find_or_schedule_jobs
+ end
end
# rubocop:enable Metrics/ClassLength
diff --git a/app/models/concerns/account_settings.rb b/app/models/concerns/account_settings.rb
index 2e9dc2e917..5d2996c06b 100644
--- a/app/models/concerns/account_settings.rb
+++ b/app/models/concerns/account_settings.rb
@@ -23,11 +23,13 @@ module AccountSettings
setting :allow_downloads, type: 'boolean', default: true
setting :allow_signup, type: 'boolean', default: true
setting :analytics_provider, type: 'string'
+ setting :batch_email_notifications, type: 'boolean', default: false
setting :bulkrax_field_mappings, type: 'json_editor', default: Hyku.default_bulkrax_field_mappings.to_json
setting :bulkrax_validations, type: 'boolean', disabled: true
setting :cache_api, type: 'boolean', default: false
setting :contact_email, type: 'string', default: 'change-me-in-settings@example.com'
setting :contact_email_to, type: 'string', default: 'change-me-in-settings@example.com'
+ setting :depositor_email_notifications, type: 'boolean', default: false
setting :doi_reader, type: 'boolean', default: false
setting :doi_writer, type: 'boolean', default: false
setting :file_acl, type: 'boolean', default: true, private: true
@@ -50,6 +52,7 @@ module AccountSettings
setting :smtp_settings, type: 'hash', private: true, default: {}
setting :solr_collection_options, type: 'hash', default: solr_collection_options
setting :ssl_configured, type: 'boolean', default: true, private: true
+ setting :user_analytics, type: 'boolean', default: false
setting :weekly_email_list, type: 'array', disabled: true
setting :yearly_email_list, type: 'array', disabled: true
diff --git a/app/views/hyrax/dashboard/profiles/_edit_primary.html.erb b/app/views/hyrax/dashboard/profiles/_edit_primary.html.erb
index 06ae76b4ae..e16f2a34f1 100644
--- a/app/views/hyrax/dashboard/profiles/_edit_primary.html.erb
+++ b/app/views/hyrax/dashboard/profiles/_edit_primary.html.erb
@@ -56,12 +56,14 @@
-
+ <% if current_account.batch_email_notifications %>
+
+ <% end %>
<%= render 'trophy_edit', trophies: @trophies %>
diff --git a/app/views/hyrax/users/_user_info.html.erb b/app/views/hyrax/users/_user_info.html.erb
index e6c62a1a2b..08b1e4ef01 100644
--- a/app/views/hyrax/users/_user_info.html.erb
+++ b/app/views/hyrax/users/_user_info.html.erb
@@ -81,7 +81,9 @@
<% end %>
- <%= t("hyrax.user_profile.email_frequency.label").html_safe %>
- <% frequency = user.batch_email_frequency || 'not_set' %>
- <%= t("hyrax.user_profile.email_frequency.#{frequency}") %>
+ <% if current_account.batch_email_notifications %>
+ <%= t("hyrax.user_profile.email_frequency.label").html_safe %>
+ <% frequency = user.batch_email_frequency || 'not_set' %>
+ <%= t("hyrax.user_profile.email_frequency.#{frequency}") %>
+ <% end %>
diff --git a/spec/features/accounts_spec.rb b/spec/features/accounts_spec.rb
index d8e412c2cd..5ad12c7db6 100644
--- a/spec/features/accounts_spec.rb
+++ b/spec/features/accounts_spec.rb
@@ -16,6 +16,7 @@
allow(Apartment::Tenant).to receive(:switch).with(account.tenant) do |&block|
block.call
end
+ allow_any_instance_of(Account).to receive(:find_or_schedule_jobs)
end
around do |example|
diff --git a/spec/models/concerns/account_settings_spec.rb b/spec/models/concerns/account_settings_spec.rb
index 4a57aea9a9..1f9ae3feeb 100644
--- a/spec/models/concerns/account_settings_spec.rb
+++ b/spec/models/concerns/account_settings_spec.rb
@@ -10,10 +10,12 @@
expect(account.public_settings(is_superadmin: true).keys.sort).to eq %i[allow_downloads
allow_signup
analytics_provider
+ batch_email_notifications
bulkrax_field_mappings
cache_api
contact_email
contact_email_to
+ depositor_email_notifications
doi_reader
doi_writer
email_domain
@@ -30,7 +32,8 @@
s3_bucket
smtp_settings
solr_collection_options
- ssl_configured]
+ ssl_configured
+ user_analytics]
end
# rubocop:enable RSpec/ExampleLength
end
diff --git a/spec/services/create_account_spec.rb b/spec/services/create_account_spec.rb
index 70ba896ff4..e13e7db117 100644
--- a/spec/services/create_account_spec.rb
+++ b/spec/services/create_account_spec.rb
@@ -116,18 +116,52 @@
end
describe '#schedule_recurring_jobs' do
- it "Enqueues Recurring jobs" do
- [
- EmbargoAutoExpiryJob,
- LeaseAutoExpiryJob,
- BatchEmailNotificationJob,
- DepositorEmailNotificationJob,
- UserStatCollectionJob
- ].each do |klass|
- expect(account).to receive(:find_job).with(klass).and_return(false)
- expect(klass).to receive(:perform_later)
+ context 'when settings are enabled' do
+ before do
+ allow(account).to receive(:batch_email_notifications).and_return(true)
+ allow(account).to receive(:depositor_email_notifications).and_return(true)
+ allow(account).to receive(:user_analytics).and_return(true)
+ end
+
+ it "enqueues recurring jobs" do
+ [
+ EmbargoAutoExpiryJob,
+ LeaseAutoExpiryJob,
+ BatchEmailNotificationJob,
+ DepositorEmailNotificationJob,
+ UserStatCollectionJob
+ ].each do |klass|
+ expect(account).to receive(:find_job).with(klass).and_return(false)
+ expect(klass).to receive(:perform_later)
+ end
+ subject.schedule_recurring_jobs
+ end
+ end
+
+ context 'when settings are disabled' do
+ before do
+ allow(account).to receive(:batch_email_notifications).and_return(false)
+ allow(account).to receive(:depositor_email_notifications).and_return(false)
+ allow(account).to receive(:user_analytics).and_return(false)
+ end
+
+ it "only enqueues embargo and lease jobs" do
+ [EmbargoAutoExpiryJob, LeaseAutoExpiryJob].each do |klass|
+ expect(account).to receive(:find_job).with(klass).and_return(false)
+ expect(klass).to receive(:perform_later)
+ end
+
+ [
+ BatchEmailNotificationJob,
+ DepositorEmailNotificationJob,
+ UserStatCollectionJob
+ ].each do |klass|
+ expect(account).not_to receive(:find_job).with(klass)
+ expect(klass).not_to receive(:perform_later)
+ end
+
+ subject.schedule_recurring_jobs
end
- subject.schedule_recurring_jobs
end
end
end