Skip to content

Commit

Permalink
🚧 WIP - Author notifications
Browse files Browse the repository at this point in the history
boilerplate is WIP. template with sample email has been created at this point but needs to be dynamic.

TODO:
- understand UserStats and replace placeholder data
- make email dynamic, including the date, tenant name, link to dashboard, etc
- Do we want to make an option for the user to not receive emails? If so - we need UI work and possibly migrations.
- specs
  • Loading branch information
Shana Moore committed May 29, 2024
1 parent ec7566e commit 9898968
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/mailers/hyku_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ def summary_email(user, messages, account)
template_name: 'summary_email')
end

def depositor_email(user, statistics, account)
@user = user
@statistics = statistics
@account = account
@url = notifications_url_for(@account)
@application_name = account.sites.application_name

mail(to: 'to email',
subject: "Monthly Downloads Summary for #{@application_name}",
from: user.email,
template_path: 'hyku_mailer',
template_name: 'depositor_email')
end

private

def host_for_tenant
Expand Down
1 change: 1 addition & 0 deletions app/services/create_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def schedule_recurring_jobs
EmbargoAutoExpiryJob.perform_later(account)
LeaseAutoExpiryJob.perform_later(account)
BatchEmailNotificationJob.perform_later(account)
DepositorEmailNotificationJob.perform_later(account)
end

private
Expand Down
33 changes: 33 additions & 0 deletions app/views/hyku_mailer/depositor_email.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; }
.email-container { max-width: 600px; margin: auto; padding: 20px; border: 1px solid #ccc; }
.footer { font-size: 12px; color: #666; margin-top: 20px; }
.highlight { color: #0056b3; }
.button {
display: inline-block;
padding: 10px 20px;
margin: 10px 0;
background-color: #0056b3;
color: white;
text-decoration: none;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="email-container">
<p>Dear Author,</p>
<p>You had <%= @statistics[:new_downloads] %> new download in December 2023 across your <%= @statistics[:papers_count] %> papers in MUShare. Your current readership:</p>
<ul>
<li><%= @statistics[:total_downloads] %> Total Downloads</li>
</ul>
<a href="url" class="button">VISIT MY DASHBOARD</a>
<div class="footer">
<p>These monthly reports are provided to you by bepress on behalf of MUShare. For questions, comments, or to add more content and increase your readership and visibility as an author, please contact your repository administrator.</p>
</div>
</div>
</body>
</html>
47 changes: 47 additions & 0 deletions depositor_email_notification_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_literal: true

class DepositorEmailNotificationJob < ApplicationJob
non_tenant_job

after_perform do |job|
reenqueue(job.arguments.first)
end

def perform(account)
Apartment::Tenant.switch(account.tenant) do
stats_period = (Date.today - 1.month).beginning_of_month..(Date.today - 1.month).end_of_month
users = User.all

users.each do |user|
statistics = gather_statistics_for(user, stats_period)
next if statistics.nil?

HykuMailer.depositor_email(user, statistics, account).deliver_now
end
end
end

private

def reenqueue(account)
DepositorEmailNotificationJob.set(wait_until: Date.next_month.beginning_of_month).perform_later(account)
end

def gather_statistics_for(user, stats_period)
# Dummy statistics - replace with actual logic to calculate view and download counts.
# users.stats could be useful: https://github.com/samvera/hyrax/blob/4d2c654a8b9f144b35a6e013b31f80cb4cf47aeb/app/models/concerns/hyrax/user_usage_stats.rb#L3
# there's also this: https://github.com/samvera/hyrax/blob/main/app/presenters/hyrax/file_usage.rb
# Fetching statistics for the previous month
last_month_stats = user.stats.where(date: stats_period)

return nil if last_month_stats.empty?

{
new_file_downloads: last_month_stats.sum(:file_downloads),
new_work_views: last_month_stats.sum(:work_views),
total_file_views: user.total_file_views,
total_file_downloads: user.total_file_downloads,
total_work_views: user.total_work_views
}
end
end
21 changes: 21 additions & 0 deletions spec/mailers/previews/hyku_mailer_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,25 @@ def summary_email

HykuMailer.new.summary_email(user, messages, account)
end

def depositor_email
# Creating mock statistics
statistics = {
new_downloads: 1,
total_downloads: 26,
papers_count: 2
}

# Mock user
user = Struct.new(:email, :name).new('depositor@example.com', 'Depositor Name')

# Mock site and account setup
site = Struct.new(:application_name)
sites = site.new('MUShare')

account = Struct.new(:cname, :contact_email, :sites).new('mushare.local', 'admin@mushare.com', sites)

# Calling the depositor_email method of the HykuMailer with mock data
HykuMailer.new.depositor_email(user, statistics, account)
end
end

0 comments on commit 9898968

Please sign in to comment.