Skip to content

Commit

Permalink
Merge pull request #5227 from fjordllc/feature/notify-mentor-when-new…
Browse files Browse the repository at this point in the history
…-user-registers

新規ユーザー登録時にメンターにメール通知およびサイト内通知するようにした
  • Loading branch information
komagata authored Aug 16, 2022
2 parents da9cc49 + 546ddfe commit be92891
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def target_allowlist
def create_free_user!
if @user.save
UserMailer.welcome(@user).deliver_now
notify_to_mentors(@user)
notify_to_chat(@user)
redirect_to root_url, notice: 'サインアップメールをお送りしました。メールからサインアップを完了させてください。'
else
Expand Down Expand Up @@ -115,6 +116,7 @@ def create_user!

if @user.save
UserMailer.welcome(@user).deliver_now
notify_to_mentors(@user)
notify_to_chat(@user)
redirect_to root_url, notice: 'サインアップメールをお送りしました。メールからサインアップを完了させてください。'
else
Expand All @@ -124,6 +126,12 @@ def create_user!
end
# rubocop:enable Metrics/MethodLength, Metrics/BlockLength

def notify_to_mentors(user)
User.mentor.each do |mentor_user|
NotificationFacade.signed_up(user, mentor_user)
end
end

def notify_to_chat(user)
ChatNotifier.message "#{user.name}さんが新たなメンバーとしてJOINしました🎉\r#{url_for(user)}"
end
Expand Down
8 changes: 8 additions & 0 deletions app/mailers/notification_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,12 @@ def hibernated
subject = "[FBC] #{@sender.login_name}さんが休会しました。"
mail to: @user.email, subject: subject
end

# required params: sender, receiver
def signed_up
@user = @receiver
@notification = @user.notifications.find_by(link: "/users/#{@sender.id}", kind: Notification.kinds[:signed_up])
subject = "[FBC] #{@sender.login_name}さんが新しく入会しました!"
mail to: @user.email, subject: subject
end
end
3 changes: 2 additions & 1 deletion app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class Notification < ApplicationRecord
assigned_as_checker: 16,
product_update: 17,
graduated: 18,
hibernated: 19
hibernated: 19,
signed_up: 20
}

scope :unreads, -> { where(read: false) }
Expand Down
10 changes: 10 additions & 0 deletions app/models/notification_facade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,14 @@ def self.hibernated(sender, receiver)
def self.tomorrow_regular_event(event)
DiscordNotifier.with(event: event).tomorrow_regular_event.notify_now
end

def self.signed_up(sender, receiver)
ActivityNotifier.with(sender: sender, receiver: receiver).signed_up.notify_now
return unless receiver.mail_notification? && !receiver.retired?

NotificationMailer.with(
sender: sender,
receiver: receiver
).signed_up.deliver_later(wait: 5)
end
end
15 changes: 15 additions & 0 deletions app/notifiers/activity_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,19 @@ def hibernated(params = {})
read: false
)
end

def signed_up(params = {})
params.merge!(@params)
sender = params[:sender]
receiver = params[:receiver]

notification(
body: "🎉 #{sender.login_name}さんが新しく入会しました!",
kind: :signed_up,
sender: sender,
receiver: receiver,
link: Rails.application.routes.url_helpers.polymorphic_path(sender),
read: false
)
end
end
1 change: 1 addition & 0 deletions app/views/notification_mailer/signed_up.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= render 'notification_mailer_template', title: "#{@sender.login_name}さんが新しく入会しました!", link_url: notification_url(@notification), link_text: "#{@sender.login_name}さんのページへ"
10 changes: 10 additions & 0 deletions test/application_system_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
teardown do
ActionMailer::Base.deliveries.clear
end

def vcr_options
{
record: :once,
match_requests_on: [
:method,
VCR.request_matchers.uri_without_param(:source)
]
}
end
end
28 changes: 28 additions & 0 deletions test/mailers/notification_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,32 @@ class NotificationMailerTest < ActionMailer::TestCase
assert_equal '[FBC] sotugyouさんが卒業しました。', email.subject
assert_match(/卒業/, email.body.to_s)
end

test 'signed up' do
user = users(:hajime)
mentor = users(:mentormentaro)
Notification.create!(
kind: 20,
sender: user,
user: mentor,
message: '🎉 hajimeさんが新しく入会しました!',
link: "/users/#{user.id}",
read: false
)
mailer = NotificationMailer.with(
sender: user,
receiver: mentor
).signed_up

perform_enqueued_jobs do
mailer.deliver_later
end

assert_not ActionMailer::Base.deliveries.empty?
email = ActionMailer::Base.deliveries.last
assert_equal ['[email protected]'], email.from
assert_equal ['[email protected]'], email.to
assert_equal '[FBC] hajimeさんが新しく入会しました!', email.subject
assert_match(/入会/, email.body.to_s)
end
end
12 changes: 12 additions & 0 deletions test/notifiers/activity_notifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,16 @@ class ActivityNotifierTest < ActiveSupport::TestCase
notification.notify_now
end
end

test '#signed_up' do
notification = ActivityNotifier.with(sender: users(:hajime), receiver: users(:mentormentaro)).signed_up

assert_difference -> { AbstractNotifier::Testing::Driver.deliveries.count }, 1 do
notification.notify_now
end

assert_difference -> { AbstractNotifier::Testing::Driver.enqueued_deliveries.count }, 1 do
notification.notify_later
end
end
end
106 changes: 106 additions & 0 deletions test/system/notification/signed_up_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# frozen_string_literal: true

require 'application_system_test_case'

class Notification::SignedUpTest < ApplicationSystemTestCase
setup do
@delivery_mode = AbstractNotifier.delivery_mode
AbstractNotifier.delivery_mode = :normal
end

teardown do
AbstractNotifier.delivery_mode = @delivery_mode
end

test 'notify mentors when signed up as adviser' do
visit '/users/new?role=adviser'

email = '[email protected]'

within 'form[name=user]' do
fill_in 'user[login_name]', with: 'haruko'
fill_in 'user[email]', with: email
fill_in 'user[name]', with: 'テスト 春子'
fill_in 'user[name_kana]', with: 'テスト ハルコ'
fill_in 'user[description]', with: 'テスト春子です。'
fill_in 'user[password]', with: 'testtest'
fill_in 'user[password_confirmation]', with: 'testtest'
find('label', text: 'アンチハラスメントポリシーに同意').click
find('label', text: '利用規約に同意').click
end

click_button 'アドバイザー登録'
assert_text 'サインアップメールをお送りしました。メールからサインアップを完了させてください。'
assert User.find_by(email: email).adviser?

visit_with_auth notifications_path, 'komagata'
within first('.card-list-item.is-unread') do
assert_selector '.card-list-item-title__link-label', text: '🎉 harukoさんが新しく入会しました!'
end
end

test 'notify mentors when signed up as trainee' do
visit '/users/new?role=trainee'

email = '[email protected]'

within 'form[name=user]' do
fill_in 'user[login_name]', with: 'natsumi'
fill_in 'user[email]', with: email
fill_in 'user[name]', with: 'テスト 夏美'
fill_in 'user[name_kana]', with: 'テスト ナツミ'
fill_in 'user[description]', with: 'テスト夏美です。'
fill_in 'user[password]', with: 'testtest'
fill_in 'user[password_confirmation]', with: 'testtest'
select '学生', from: 'user[job]'
find('label', text: 'Mac(Apple チップ)').click
select '未経験', from: 'user[experience]'
first('.choices__inner').click
find('.choices__list--dropdown').click
find('.choices__list').click
find('#choices--js-choices-single-select-item-choice-2').click
find('label', text: 'アンチハラスメントポリシーに同意').click
find('label', text: '利用規約に同意').click
end

click_button '参加する'
assert_text 'サインアップメールをお送りしました。メールからサインアップを完了させてください。'
assert User.find_by(email: email).trainee?

visit_with_auth notifications_path, 'komagata'
within first('.card-list-item.is-unread') do
assert_selector '.card-list-item-title__link-label', text: '🎉 natsumiさんが新しく入会しました!'
end
end

test 'notify mentors when signed up as normal user' do
visit '/users/new'
within 'form[name=user]' do
fill_in 'user[login_name]', with: 'taro'
fill_in 'user[email]', with: '[email protected]'
fill_in 'user[name]', with: 'テスト 太郎'
fill_in 'user[name_kana]', with: 'テスト タロウ'
fill_in 'user[description]', with: 'テスト太郎です。'
fill_in 'user[password]', with: 'testtest'
fill_in 'user[password_confirmation]', with: 'testtest'
fill_in 'user[after_graduation_hope]', with: '起業したいです'
select '学生', from: 'user[job]'
find('label', text: 'Mac(Apple チップ)').click
select '未経験', from: 'user[experience]'
find('label', text: 'アンチハラスメントポリシーに同意').click
find('label', text: '利用規約に同意').click
end

fill_stripe_element('4242 4242 4242 4242', '12 / 50', '111')

VCR.use_cassette 'sign_up/valid-card', vcr_options do
click_button '参加する'
assert_text 'サインアップメールをお送りしました。メールからサインアップを完了させてください。'
end

visit_with_auth notifications_path, 'komagata'
within first('.card-list-item.is-unread') do
assert_selector '.card-list-item-title__link-label', text: '🎉 taroさんが新しく入会しました!'
end
end
end

0 comments on commit be92891

Please sign in to comment.