-
Notifications
You must be signed in to change notification settings - Fork 48
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
🎁 1019 batch email notifications #2200
Merged
Merged
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b2e8a22
🎁 Flesh out `BatchEmailNotificationJob`
kirkkwang 09ec444
revert add to schedule as it causes spec failures somehow
ShanaLMoore ddbcc91
Update app/jobs/prune_stale_guest_users_job.rb
ShanaLMoore 26317d8
Add `BatchEmailNotificationJob` to create_account
kirkkwang 6cd003c
Merge branch 'main' into i1019-batch-email-notifications
ShanaLMoore 643d55f
Change default value for batch_email_frequency
laritakr 0861258
UI to set email frequency
laritakr 519522b
Merge branch 'main' into i1019-batch-email-notifications
laritakr 23924e7
Fix spec
laritakr 3666922
Clean up views
kirkkwang b4cabac
Change 'off' to 'never'
kirkkwang d7c4b94
Add callback for `Mailboxer::Receipt`
kirkkwang 7c0c652
✅ Add specs for batch email notifications
kirkkwang bc7e324
Run translations
kirkkwang 04e61ff
Merge branch 'main' into i1019-batch-email-notifications
kirkkwang 19a6e4e
Fix preview arguments
kirkkwang c0d6351
Add Site's application name into email
kirkkwang f3740c6
Merge branch 'main' into i1019-batch-email-notifications
kirkkwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
class BatchEmailNotificationJob < ApplicationJob | ||
non_tenant_job | ||
after_perform do |job| | ||
reenqueue(job.arguments.first) | ||
end | ||
|
||
def perform(account) | ||
Apartment::Tenant.switch(account.tenant) do | ||
# Query for all users that have email_frequency not set to "off" | ||
users = User.where.not(batch_email_frequency: "off") | ||
users.each do |user| | ||
# Find all undelivered messages within the frequency range of a user and any emails that haven't been sent | ||
undelivered_messages = | ||
Mailboxer::Message.joins(:receipts) | ||
.where(mailboxer_receipts: { receiver_id: user.id, receiver_type: 'User', is_delivered: false }) | ||
.where('mailboxer_notifications.created_at >= ?', frequency_date(user.batch_email_frequency)) | ||
.select('mailboxer_notifications.*') | ||
.distinct | ||
.to_a | ||
|
||
next if undelivered_messages.blank? | ||
send_email(user, undelivered_messages) | ||
|
||
# Mark the as read | ||
undelivered_messages.each do |message| | ||
message.receipts.each do |receipt| | ||
receipt.update(is_delivered: true) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def reenqueue(account) | ||
BatchEmailNotificationJob.set(wait_until: Date.tomorrow.midnight).perform_later(account) | ||
end | ||
|
||
def frequency_date(frequency) | ||
case frequency | ||
when "daily" | ||
1.day.ago | ||
when "weekly" | ||
1.week.ago | ||
when "monthly" | ||
1.month.ago | ||
end | ||
end | ||
|
||
def send_email(user, undelivered_messages) | ||
mailer = HykuMailer.new | ||
mailer.summary_email(user, undelivered_messages) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Notifications</title> | ||
<style> | ||
table { | ||
width: 100%; | ||
border-collapse: collapse; | ||
margin-bottom: 20px; | ||
} | ||
th, td { | ||
border: 1px solid #ddd; | ||
padding: 8px; | ||
text-align: left; | ||
} | ||
th { | ||
background-color: #f2f2f2; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h2>Hello, <%= @user.name %></h2> | ||
<p>You have the following notifications:</p> | ||
<p>Click <a href="<%= @url %>">here</a> to view them in Hyku.</p> | ||
<table> | ||
<tr> | ||
<th>Date</th> | ||
<th>Subject</th> | ||
<th>Message</th> | ||
</tr> | ||
<% @messages.each do |message| %> | ||
<tr> | ||
<td><%= message.created_at.to_date %></td> | ||
<td><%= message.subject %></td> | ||
<td><%= message.body.gsub('href="', "href=\"#{URI(@url).origin}").html_safe %></td> | ||
</tr> | ||
<% end %> | ||
</table> | ||
</body> | ||
</html> |
5 changes: 5 additions & 0 deletions
5
db/migrate/20240429213539_add_batch_email_frequency_to_users.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddBatchEmailFrequencyToUsers < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :users, :batch_email_frequency, :string, default: 'off' unless column_exists?(:users, :batch_email_frequency) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
# Preview all emails at /rails/mailers/hyku_mailer/summary_email | ||
class HykuMailerPreview < ActionMailer::Preview | ||
def summary_email | ||
message = Struct.new(:subject, :body, :created_at) | ||
messages = [ | ||
message.new( | ||
'Deposit needs review ', | ||
'"dummy work (<a href="/concern/generic_works/ab387de7-5cc3-4bee-980b-c08c24a29dfd\">ab387de7-5cc3-4bee-980b-c08c24a29dfd</a>) was deposited by admin@example.com and is awaiting approval "', 1.day.ago | ||
), | ||
message.new('Passing batch create ', 'The batch create for admin@example.com passed.', 2.days.ago) | ||
] | ||
|
||
user = Struct.new(:email, :name).new('admin@example.com', 'Admin') | ||
|
||
HykuMailer.new.summary_email(user, messages) | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we should use the application &/or tenant name rather than "Hyku"? Not a big deal, but many of the users may not know what "Hyku" is. If the email is too generic, wouldn't they possibly just assume it's spam? Including the actual site name, a possible site logo, etc would seem more clear.
Additionally, I can see a future request might be to use i18n for the email text... just something to consider.