diff --git a/app/mailers/activity_mailer.rb b/app/mailers/activity_mailer.rb index 353d765dfbb..335f339e86e 100644 --- a/app/mailers/activity_mailer.rb +++ b/app/mailers/activity_mailer.rb @@ -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] diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index f3853b75630..6b94ae64e3f 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -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 diff --git a/app/models/notification_facade.rb b/app/models/notification_facade.rb index d4ab724248c..cc34d28b23c 100644 --- a/app/models/notification_facade.rb +++ b/app/models/notification_facade.rb @@ -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 diff --git a/app/models/question.rb b/app/models/question.rb index 617d694a141..d08cf1ba982 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -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 diff --git a/app/views/activity_mailer/no_correct_answer.html.slim b/app/views/activity_mailer/no_correct_answer.html.slim new file mode 100644 index 00000000000..d9f68aa4e55 --- /dev/null +++ b/app/views/activity_mailer/no_correct_answer.html.slim @@ -0,0 +1 @@ += render '/notification_mailer/notification_mailer_template', title: "#{@user.login_name}さんの質問【 #{@question.title} 】のベストアンサーがまだ選ばれていません。", link_url: @link_url, link_text: "#{@user.login_name}さんの質問へ" diff --git a/app/views/notification_mailer/no_correct_answer.html.slim b/app/views/notification_mailer/no_correct_answer.html.slim deleted file mode 100644 index 38877c95751..00000000000 --- a/app/views/notification_mailer/no_correct_answer.html.slim +++ /dev/null @@ -1 +0,0 @@ -= render 'notification_mailer_template', title: "#{@user.login_name}さんの質問【 #{@question.title} 】のベストアンサーがまだ選ばれていません。", link_url: notification_url(@notification), link_text: "#{@user.login_name}さんの質問へ" diff --git a/test/deliveries/activity_delivery_test.rb b/test/deliveries/activity_delivery_test.rb index 605c4d4ce6b..257867c989a 100644 --- a/test/deliveries/activity_delivery_test.rb +++ b/test/deliveries/activity_delivery_test.rb @@ -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 diff --git a/test/mailers/activity_mailer_test.rb b/test/mailers/activity_mailer_test.rb index b2771479020..334c07bbf69 100644 --- a/test/mailers/activity_mailer_test.rb +++ b/test/mailers/activity_mailer_test.rb @@ -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{#{receiver.login_name}さんの質問へ}, 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{#{receiver.login_name}さんの質問へ}, email.body.to_s) + end end diff --git a/test/mailers/notification_mailer_test.rb b/test/mailers/notification_mailer_test.rb index d3c5eb20be2..cf1798c2da0 100644 --- a/test/mailers/notification_mailer_test.rb +++ b/test/mailers/notification_mailer_test.rb @@ -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 diff --git a/test/mailers/previews/activity_mailer_preview.rb b/test/mailers/previews/activity_mailer_preview.rb index aaed95efe96..83fdd3dc11a 100644 --- a/test/mailers/previews/activity_mailer_preview.rb +++ b/test/mailers/previews/activity_mailer_preview.rb @@ -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