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

Feature/tf+ 35 email notifications #277

Merged
merged 27 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6125134
spike: send email after user creation using action mailer and sendgrid
jacob271 Dec 12, 2022
ec5f818
fix linter
jacob271 Dec 12, 2022
fc94041
fix linter again
jacob271 Dec 12, 2022
f74f014
update Gemfile.lock after removing gem
jacob271 Dec 12, 2022
2de09a7
Merge branch 'dev' into feature/tf+-35-email-notifications
jacob271 Jan 16, 2023
bf5abee
remove bookkeeper mailer
jacob271 Jan 16, 2023
a711742
generate mailer for notifications
jacob271 Jan 16, 2023
32eac62
update default from mail address
jacob271 Jan 16, 2023
0de6411
remove user creation email
jacob271 Jan 16, 2023
c13bf8a
include notification title in subject and add internationalization
jacob271 Jan 16, 2023
200103f
send basic mail fo lend request notification
jacob271 Jan 16, 2023
09381b5
include description of notification in email
jacob271 Jan 18, 2023
d8f4cee
implement send_mail in generic notification and send mails where an a…
jacob271 Jan 18, 2023
e812fd2
test that mail renders header and body
jacob271 Jan 18, 2023
1e08539
test that an email is sent for return request and lend request notifi…
jacob271 Jan 18, 2023
a84dd1c
create factory for move_up_on_waitlist_notifications
jacob271 Jan 18, 2023
3eab560
wip: test send email for move up on waitlist notification
jacob271 Jan 18, 2023
4fed7e2
Merge branch 'dev' into feature/tf+-35-email-notifications
jacob271 Jan 21, 2023
4b7b96a
tests for move_up_on_waitlist notification mails
jacob271 Jan 21, 2023
3641b3b
fix linter
jacob271 Jan 21, 2023
5647ff9
ignore delivery errors in production
jacob271 Jan 21, 2023
069ab3d
read sendgrid apikey from github secrets
jacob271 Jan 21, 2023
c2ce370
revert changes to workflows since the apikey will be stored directly …
jacob271 Jan 21, 2023
a0f2089
remove redundant information about new notification in email subjects
jacob271 Jan 23, 2023
ed5e47a
adapt test to last changes in email subject
jacob271 Jan 23, 2023
3a7f8d7
Merge branch 'dev' into feature/tf+-35-email-notifications
jacob271 Jan 23, 2023
77e6f76
Merge branch 'dev' into feature/tf+-35-email-notifications
jacob271 Jan 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ group :development do
# Use console on exceptions pages [https://github.com/rails/web-console]
gem "web-console"

gem "letter_opener"
# Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
# gem "rack-mini-profiler"

Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ GEM
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
json (2.6.2)
launchy (2.5.0)
addressable (~> 2.7)
letter_opener (1.8.1)
launchy (>= 2.2, < 3)
json-jwt (1.16.1)
activesupport (>= 4.2)
aes_key_wrap
Expand Down Expand Up @@ -403,6 +407,7 @@ DEPENDENCIES
factory_bot_rails
importmap-rails
jbuilder
letter_opener
net-http
omniauth-rails_csrf_protection
omniauth_openid_connect
Expand Down
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
default from: "bookkeeperblue.notification@gmail.com"
layout "mailer"
end
9 changes: 9 additions & 0 deletions app/mailers/notification_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class NotificationMailer < ApplicationMailer
def notification(notification)
@title = notification.title
@description = notification.description

mail(to: notification.receiver.email,
subject: I18n.t("notification_mailer.notification.subject", notification_name: @title))
end
end
1 change: 1 addition & 0 deletions app/models/lend_request_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class LendRequestNotification < ApplicationRecord

belongs_to :borrower, class_name: "User"
belongs_to :item
after_create :send_mail

def title
I18n.t "views.notifications.lend_request.title"
Expand Down
8 changes: 8 additions & 0 deletions app/models/move_up_on_waitlist_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class MoveUpOnWaitlistNotification < ApplicationRecord

belongs_to :item

after_create :check_position

def title
I18n.t("views.notifications.move_up_on_waitlist.title")
end
Expand All @@ -12,4 +14,10 @@ def description
I18n.t("views.notifications.move_up_on_waitlist.description", position: item.waitlist.position(receiver) + 1,
item: item.name)
end

def check_position
return unless item.waitlist.position(receiver).zero?

send_mail
end
end
4 changes: 4 additions & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ def method_missing(method, *args, &block)
def respond_to_missing?(_method, _include_private = false)
false
end

def send_mail
NotificationMailer.notification(self).deliver_now
end
end
1 change: 1 addition & 0 deletions app/models/return_request_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ReturnRequestNotification < ApplicationRecord

validates :receiver, presence: true
validates :date, presence: true
after_create :send_mail

def title
I18n.t "views.notifications.return_request.title"
Expand Down
5 changes: 5 additions & 0 deletions app/views/notification_mailer/notification.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1><%= I18n.t("notification_mailer.notification.subject", notification_name: @title) %></h1>

<p>
<%= @description %>
</p>
3 changes: 3 additions & 0 deletions app/views/notification_mailer/notification.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= I18n.t("notification_mailer.notification.subject", notification_name: @title) %>

<%= @description %>
7 changes: 5 additions & 2 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@
# Store uploaded files on the local file system (see config/storage.yml for options).
config.active_storage.service = :local

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
config.action_mailer.raise_delivery_errors = true

config.action_mailer.perform_caching = false

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.perform_deliveries = true

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log

Expand Down
12 changes: 11 additions & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false
config.action_mailer.raise_delivery_errors = false

# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
Expand All @@ -75,6 +75,16 @@
# Don't log any deprecations.
config.active_support.report_deprecations = false

ActionMailer::Base.smtp_settings = {
user_name: 'apikey', # This is the string literal 'apikey', NOT the ID of your API key
password: ENV.fetch("SENDGRID_API_KEY", ""), # This is the secret sendgrid API key which was issued during API key creation
domain: 'gmail.com',
address: 'smtp.sendgrid.net',
port: 587,
authentication: :plain,
enable_starttls_auto: true
}

# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new

Expand Down
3 changes: 3 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,6 @@ de:
helpers:
submit:
update: "Gegendstand ändern"
notification_mailer:
notification:
subject: "%{notification_name}: %{item_name}"
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,6 @@ en:
helpers:
submit:
update: "Update Item"
notification_mailer:
notification:
subject: "%{notification_name}"
6 changes: 5 additions & 1 deletion spec/factories/move_up_on_waitlist_notifications.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
FactoryBot.define do
factory :move_up_on_waitlist_notification do
item { nil }
sequence(:active) { |n| n % 2 }
sequence(:date) { |n| Time.strptime("2022-11-0#{(n % 9) + 1} 13:47:20", "%Y-%m-%d %H:%M:%S") }
sequence(:unread) { true }
receiver { FactoryBot.build(:user) }
item { FactoryBot.build(:item) }
end
end
4 changes: 4 additions & 0 deletions spec/features/notifications/lend_request_notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
@notification.save
end

it "sends an email after creation" do
expect(ActionMailer::Base.deliveries.last.to).to eq [user.email]
end

it "has accept and decline buttons" do
visit notifications_path
click_on('Lend Request')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "rails_helper"

describe "Move Up On Waitlist Notifications", type: :feature do
let(:password) { 'password' }
let(:receiver) { build(:user, password: password) }

before do
sign_in receiver
FactoryBot.reload
@notification = build(:move_up_on_waitlist_notification, receiver: receiver, active: true)
@notification.item.waitlist = build(:empty_waitlist)
end

it "sends an email after creation if position is zero" do
@notification.item.waitlist.users << receiver
@notification.save
expect(@notification.item.waitlist.position(receiver)).to eq 0
expect(ActionMailer::Base.deliveries.last.to).to eq [receiver.email]
end

it "sends no email after creation if position is non zero" do
@notification.item.waitlist.users << build(:max, password: password)
@notification.item.waitlist.users << receiver
@notification.save
expect(ActionMailer::Base.deliveries).to be_empty
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@
@notification.reload
expect(@notification.unread).to be true
end

it "sends an email after creation" do
expect(ActionMailer::Base.deliveries.last.to).to eq [user.email]
end
end
3 changes: 3 additions & 0 deletions spec/fixtures/bookkeeper/notification
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Bookkeeper#notification

Hi, find me in app/views/bookkeeper/notification
3 changes: 3 additions & 0 deletions spec/fixtures/notification/notification
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Notification#notification

Hi, find me in app/views/notification/notification
22 changes: 22 additions & 0 deletions spec/mailers/notification_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "rails_helper"

RSpec.describe NotificationMailer, type: :mailer do
describe "notification" do
before do
@notification = build(:lend_request_notification)
end

let(:mail) { described_class.notification(@notification) }

it "renders the headers" do
expect(mail.subject).to eq("Lend Request")
expect(mail.to).to eq([@notification.receiver.email])
expect(mail.from).to eq(["[email protected]"])
end

it "renders the body" do
expect(mail.body.encoded).to match(@notification.description)
end
end

end
7 changes: 7 additions & 0 deletions spec/mailers/previews/notification_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Preview all emails at http://localhost:3000/rails/mailers/notification
class NotificationPreview < ActionMailer::Preview
# Preview this email at http://localhost:3000/rails/mailers/notification/notification
def notification
NotificationMailer.notification(build(:lend_request_notification))
end
end