Skip to content

Commit

Permalink
Merge pull request #6578 from fjordllc/feature/replace-notification-o…
Browse files Browse the repository at this point in the history
…n-no-correct-answer-with-active-delivery

ベストアンサーなし通知をactive_delivery化する
  • Loading branch information
komagata authored Jun 28, 2023
2 parents 455fc7c + 18685d3 commit e7fbc23
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 48 deletions.
18 changes: 18 additions & 0 deletions app/mailers/activity_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,24 @@ def update_regular_event(args = {})
message
end

# required params: question, receiver
def no_correct_answer(args = {})
@question ||= args[:question]
@receiver ||= args[:receiver]
@user = @receiver

@link_url = notification_redirector_url(
link: "/questions/#{@question.id}",
kind: Notification.kinds[:no_correct_answer]
)

subject = "[FBC] #{@user.login_name}さんの質問【 #{@question.title} 】のベストアンサーがまだ選ばれていません。"
message = mail to: @user.email, subject: subject
message.perform_deliveries = @user.mail_notification? && !@user.retired?

message
end

# required params: sender, receiver
def signed_up(args = {})
@sender ||= args[:sender]
Expand Down
8 changes: 0 additions & 8 deletions app/mailers/notification_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,4 @@ def create_page
subject = "[FBC] #{@page.user.login_name}さんがDocsに#{@page.title}を投稿しました。"
mail to: @user.email, subject: subject
end

# required params: question, receiver
def no_correct_answer
@user = @receiver
@notification = @user.notifications.find_by(link: "/questions/#{@question.id}", kind: Notification.kinds[:no_correct_answer])
subject = "[FBC] #{@user.login_name}さんの質問【 #{@question.title} 】のベストアンサーがまだ選ばれていません。"
mail to: @user.email, subject: subject
end
end
10 changes: 0 additions & 10 deletions app/models/notification_facade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,4 @@ def self.trainee_report(report, receiver)
def self.coming_soon_regular_events(today_events, tomorrow_events)
DiscordNotifier.with(today_events: today_events, tomorrow_events: tomorrow_events).coming_soon_regular_events.notify_now
end

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

NotificationMailer.with(
question: question,
receiver: receiver
).no_correct_answer.deliver_later(wait: 5)
end
end
2 changes: 1 addition & 1 deletion app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def notify_certain_period_passed_after_last_answer
return if Question.not_solved_and_certain_period_has_passed.blank?

Question.not_solved_and_certain_period_has_passed.each do |not_solved_question|
NotificationFacade.no_correct_answer(not_solved_question, not_solved_question.user)
ActivityDelivery.with(question: not_solved_question, receiver: not_solved_question.user).notify(:no_correct_answer)
end
end

Expand Down
1 change: 1 addition & 0 deletions app/views/activity_mailer/no_correct_answer.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= render '/notification_mailer/notification_mailer_template', title: "#{@user.login_name}さんの質問【 #{@question.title} 】のベストアンサーがまだ選ばれていません。", link_url: @link_url, link_text: "#{@user.login_name}さんの質問へ"
1 change: 0 additions & 1 deletion app/views/notification_mailer/no_correct_answer.html.slim

This file was deleted.

24 changes: 24 additions & 0 deletions test/deliveries/activity_delivery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,28 @@ class ActivityDeliveryTest < ActiveSupport::TestCase
ActivityDelivery.with(**params).notify(:chose_correct_answer)
end
end

test '.notify(:no_correct_answer)' do
question = questions(:question1)
params = {
question: question,
receiver: question.user
}

assert_difference -> { AbstractNotifier::Testing::Driver.deliveries.count }, 1 do
ActivityDelivery.notify!(:no_correct_answer, **params)
end

assert_difference -> { AbstractNotifier::Testing::Driver.enqueued_deliveries.count }, 1 do
ActivityDelivery.notify(:no_correct_answer, **params)
end

assert_difference -> { AbstractNotifier::Testing::Driver.deliveries.count }, 1 do
ActivityDelivery.with(**params).notify!(:no_correct_answer)
end

assert_difference -> { AbstractNotifier::Testing::Driver.enqueued_deliveries.count }, 1 do
ActivityDelivery.with(**params).notify(:no_correct_answer)
end
end
end
34 changes: 34 additions & 0 deletions test/mailers/activity_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -989,4 +989,38 @@ class ActivityMailerTest < ActionMailer::TestCase

assert ActionMailer::Base.deliveries.empty?
end

test 'no_correct_answer using synchronous mailer' do
question = questions(:question1)
receiver = question.user

ActivityMailer.no_correct_answer(question: question, receiver: receiver).deliver_now

assert_not ActionMailer::Base.deliveries.empty?
email = ActionMailer::Base.deliveries.last
query = CGI.escapeHTML({ kind: Notification.kinds[:no_correct_answer], link: "/questions/#{question.id}" }.to_param)
assert_equal ['noreply@bootcamp.fjord.jp'], email.from
assert_equal [receiver.email], email.to
assert_equal "[FBC] #{receiver.login_name}さんの質問【 #{question.title} 】のベストアンサーがまだ選ばれていません。", email.subject
assert_match(%r{<a .+ href="http://localhost:3000/notification/redirector\?#{query}">#{receiver.login_name}さんの質問へ</a>}, email.body.to_s)
end

test 'no_correct_answer with params using asynchronous mailer' do
question = questions(:question1)
receiver = question.user

mailer = ActivityMailer.with(question: question, receiver: receiver).no_correct_answer

perform_enqueued_jobs do
mailer.deliver_later
end

assert_not ActionMailer::Base.deliveries.empty?
email = ActionMailer::Base.deliveries.last
query = CGI.escapeHTML({ kind: Notification.kinds[:no_correct_answer], link: "/questions/#{question.id}" }.to_param)
assert_equal ['noreply@bootcamp.fjord.jp'], email.from
assert_equal [receiver.email], email.to
assert_equal "[FBC] #{receiver.login_name}さんの質問【 #{question.title} 】のベストアンサーがまだ選ばれていません。", email.subject
assert_match(%r{<a .+ href="http://localhost:3000/notification/redirector\?#{query}">#{receiver.login_name}さんの質問へ</a>}, email.body.to_s)
end
end
28 changes: 0 additions & 28 deletions test/mailers/notification_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,4 @@ class NotificationMailerTest < ActionMailer::TestCase
assert_equal '[FBC] kensyuさんが日報【 研修の日報 】を書きました!', email.subject
assert_match(/日報/, email.body.to_s)
end

test 'no_correct_answer' do
user = users(:kimura)
question = questions(:question8)
Notification.create!(
kind: 22,
sender: user,
user: user,
message: 'Q&A「テストの質問」のベストアンサーがまだ選ばれていません。',
link: "/questions/#{question.id}",
read: false
)
mailer = NotificationMailer.with(
question: question,
receiver: user
).no_correct_answer

perform_enqueued_jobs do
mailer.deliver_later
end

assert_not ActionMailer::Base.deliveries.empty?
email = ActionMailer::Base.deliveries.last
assert_equal ['noreply@bootcamp.fjord.jp'], email.from
assert_equal ['kimura@fjord.jp'], email.to
assert_equal '[FBC] kimuraさんの質問【 テストの質問 】のベストアンサーがまだ選ばれていません。', email.subject
assert_match(/まだ選ばれていません/, email.body.to_s)
end
end
7 changes: 7 additions & 0 deletions test/mailers/previews/activity_mailer_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,11 @@ def chose_correct_answer

ActivityMailer.with(answer: answer, receiver: receiver).chose_correct_answer
end

def no_correct_answer
question = Question.find(ActiveRecord::FixtureSet.identify(:question1))
receiver = User.find(question.user_id)

ActivityMailer.with(question: question, receiver: receiver).no_correct_answer
end
end

0 comments on commit e7fbc23

Please sign in to comment.