Skip to content

Commit

Permalink
Merge pull request #7510 from fjordllc/feature/add-bell-notification-…
Browse files Browse the repository at this point in the history
…for-inquiry

お問い合わせがあったら管理者に通知する
  • Loading branch information
komagata authored Jul 15, 2024
2 parents a146f10 + 62ff6c8 commit 694e488
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 32 deletions.
3 changes: 2 additions & 1 deletion app/controllers/inquiries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ def new

def create
@inquiry = Inquiry.new(inquiry_params)

result = valid_recaptcha?('inquiry')

if result && @inquiry.save
Newspaper.publish(:came_inquiry, { inquiry: @inquiry })
InquiryMailer.incoming(@inquiry).deliver_later
redirect_to new_inquiry_url, notice: 'お問い合わせを送信しました。'
else
Expand Down
15 changes: 15 additions & 0 deletions app/models/inquiry_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class InquiryNotifier
NOTIFICATION_SENDER_NAME = 'pjord'

def call(payload)
inquiry = payload[:inquiry]
return if inquiry.nil?

sender = User.find_by(login_name: NOTIFICATION_SENDER_NAME)
User.admins.each do |receiver|
ActivityDelivery.with(inquiry:, receiver:, sender:).notify(:came_inquiry)
end
end
end
27 changes: 2 additions & 25 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class Notification < ApplicationRecord
no_correct_answer: 22,
comebacked: 23,
create_article: 24,
added_work: 25
added_work: 25,
came_inquiry: 26
}

scope :unreads, -> { where(read: false) }
Expand All @@ -60,30 +61,6 @@ class Notification < ApplicationRecord
after_update NotificationCallbacks.new
after_destroy NotificationCallbacks.new

class << self
def checked(check)
Notification.create!(
kind: kinds[:checked],
user: check.receiver,
sender: check.sender,
link: Rails.application.routes.url_helpers.polymorphic_path(check.checkable),
message: "#{check.sender.login_name}さんが#{check.checkable.title}を確認しました。",
read: false
)
end

def came_answer(answer)
Notification.create!(
kind: kinds[:answered],
user: answer.receiver,
sender: answer.sender,
link: Rails.application.routes.url_helpers.polymorphic_path(answer.question),
message: "#{answer.user.login_name}さんから回答がありました。",
read: false
)
end
end

def unread?
!read
end
Expand Down
16 changes: 16 additions & 0 deletions app/notifiers/activity_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,20 @@ def added_work(params = {})
read: false
)
end

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

notification(
body: "#{inquiry.name}さんから問い合わせがありました。",
kind: :came_inquiry,
receiver:,
sender:,
link: Rails.application.routes.url_helpers.admin_inquiry_path(inquiry),
read: false
)
end
end
2 changes: 2 additions & 0 deletions config/initializers/newspaper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@

Newspaper.subscribe(:work_create, WorkNotifier.new)
Newspaper.subscribe(:work_destroy, WorkNotificationDestroyer.new)

Newspaper.subscribe(:came_inquiry, InquiryNotifier.new)
end
2 changes: 1 addition & 1 deletion db/fixtures/discord_profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ discord_profile_nagai-kyuukai:
times_url: https://discord.com/channels/1079802367970656257/1224708078847397908
times_id: 1224708078847397908

discord_profire_pjord:
discord_profile_pjord:
user: pjord
account_name:
times_url:
Expand Down
Binary file added db/fixtures/files/users/avatars/pjord.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions db/fixtures/talks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,6 @@ talk_kensyu-not-setting-end-date:
user: kensyu-not-setting-end-date
action_completed: true

talk_harikirio:
user: harikirio
talk_pjord:
user: pjord
action_completed: true
4 changes: 2 additions & 2 deletions db/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ paginataion-jirou:

pjord:
login_name: pjord
email:
email: [email protected]
crypted_password: $2a$10$n/xv4/1luueN6plzm2OyDezWlZFyGHjQEf4hwAW1r3k.lCm0frPK. # testtest
salt: zW3kQ9ubsxQQtzzzs4ap
name: ピヨルド
Expand All @@ -1418,7 +1418,7 @@ pjord:
course: course1
company: company1
admin: true
mentor: false
mentor: true
unsubscribe_email_token: Xw-T0PjrO2LTkCx1C8KA9w
updated_at: "2024-03-09 00:00:30"
created_at: "2024-03-05 00:00:30"
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1018,3 +1018,21 @@ harikirio: #必修でないプラクティスも修了しているユーザー
created_at: "2020-01-01 00:00:12"
sent_student_followup_message: true
last_activity_at: "2020-01-01 00:00:12"

pjord:
login_name: pjord
email: [email protected]
crypted_password: $2a$10$n/xv4/1luueN6plzm2OyDezWlZFyGHjQEf4hwAW1r3k.lCm0frPK. # testtest
salt: zW3kQ9ubsxQQtzzzs4ap
name: ピヨルド
name_kana: ピヨルド
company: company1
description: "管理者のピヨルドです。バイキングの末裔です。"
course: course1
job: office_worker
os: 回答なし
admin: true
adviser: true
updated_at: "2014-01-01 00:00:01"
created_at: "2014-01-01 00:00:01"
last_activity_at: "2014-01-01 00:00:01"
2 changes: 1 addition & 1 deletion test/models/generation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class GenerationTest < ActiveSupport::TestCase
assert_equal 3, Generation.new(5).count_classmates_by_target(:trainees)
assert_equal 1, Generation.new(5).count_classmates_by_target(:hibernated)
assert_equal 2, Generation.new(5).count_classmates_by_target(:graduated)
assert_equal 2, Generation.new(5).count_classmates_by_target(:advisers)
assert_equal 3, Generation.new(5).count_classmates_by_target(:advisers)
assert_equal 2, Generation.new(5).count_classmates_by_target(:retired)
end
end
17 changes: 17 additions & 0 deletions test/notifiers/activity_notifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,21 @@ class ActivityNotifierTest < ActiveSupport::TestCase
notification.notify_later
end
end

test '#came_inquiry' do
params = {
inquiry: Inquiry.first,
sender: users(:pjord),
receiver: users(:komagata)
}
notification = ActivityNotifier.with(params).came_inquiry

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

0 comments on commit 694e488

Please sign in to comment.