Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎁 adds batch email, depositor email, and user stat to Account Settings #2429

Merged
merged 8 commits into from
Jan 27, 2025
41 changes: 35 additions & 6 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions app/models/concerns/account_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]'
setting :contact_email_to, type: 'string', default: '[email protected]'
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
Expand All @@ -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

Expand Down
14 changes: 8 additions & 6 deletions app/views/hyrax/dashboard/profiles/_edit_primary.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@
</div><!-- .form-group -->

<!-- OVERRIDE: Add batch email frequency to edit -->
<div class="form-group row">
<%= f.label :batch_email_frequency, t("hyrax.user_profile.email_frequency.label").html_safe, class: 'col-4 col-form-label' %>
<div class="col-8">
<%= f.select :batch_email_frequency, controller.frequency_options, {}, { class: "form-control" } %>
</div>
</div><!-- .form-group -->
<% if current_account.batch_email_notifications %>
<div class="form-group row">
<%= f.label :batch_email_frequency, t("hyrax.user_profile.email_frequency.label").html_safe, class: 'col-4 col-form-label' %>
<div class="col-8">
<%= f.select :batch_email_frequency, controller.frequency_options, {}, { class: "form-control" } %>
</div>
</div><!-- .form-group -->
<% end %>

<%= render 'trophy_edit', trophies: @trophies %>

Expand Down
8 changes: 5 additions & 3 deletions app/views/hyrax/users/_user_info.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@
<% end %>

<!-- OVERRIDE Hyrax 5.0: Display batch email frequency -->
<dt><%= t("hyrax.user_profile.email_frequency.label").html_safe %></dt>
<% frequency = user.batch_email_frequency || 'not_set' %>
<dd><%= t("hyrax.user_profile.email_frequency.#{frequency}") %></dd>
<% if current_account.batch_email_notifications %>
<dt><%= t("hyrax.user_profile.email_frequency.label").html_safe %></dt>
<% frequency = user.batch_email_frequency || 'not_set' %>
<dd><%= t("hyrax.user_profile.email_frequency.#{frequency}") %></dd>
<% end %>
</dl>
1 change: 1 addition & 0 deletions spec/features/accounts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts about this? This spec was failing because of the after_save addition. cc @orangewolf

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this seems fine

end

around do |example|
Expand Down
5 changes: 4 additions & 1 deletion spec/models/concerns/account_settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,7 +32,8 @@
s3_bucket
smtp_settings
solr_collection_options
ssl_configured]
ssl_configured
user_analytics]
end
# rubocop:enable RSpec/ExampleLength
end
Expand Down
56 changes: 45 additions & 11 deletions spec/services/create_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading