Move Welcome Notification to Active Jobs (#2485)
- creation of welcome notification is in own service - which is called by an active job Resolves: #1996
This commit is contained in:
parent
197e7cbbf8
commit
f3f8a51e35
5 changed files with 95 additions and 18 deletions
11
app/jobs/notifications/welcome_notification_job.rb
Normal file
11
app/jobs/notifications/welcome_notification_job.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
module Notifications
|
||||
class WelcomeNotificationJob < ApplicationJob
|
||||
queue_as :send_welcome_notification
|
||||
|
||||
def perform(receiver_id, service = WelcomeNotification::Send)
|
||||
welcome_broadcast = Broadcast.find_by(title: "Welcome Notification")
|
||||
|
||||
service.call(receiver_id, welcome_broadcast) if welcome_broadcast
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -112,25 +112,12 @@ class Notification < ApplicationRecord
|
|||
handle_asynchronously :send_mention_notification
|
||||
|
||||
def send_welcome_notification(receiver_id)
|
||||
welcome_broadcast = Broadcast.find_by(title: "Welcome Notification")
|
||||
return if welcome_broadcast.nil?
|
||||
|
||||
dev_account = User.dev_account
|
||||
json_data = {
|
||||
user: user_data(dev_account),
|
||||
broadcast: {
|
||||
processed_html: welcome_broadcast.processed_html
|
||||
}
|
||||
}
|
||||
Notification.create(
|
||||
user_id: receiver_id,
|
||||
notifiable_id: welcome_broadcast.id,
|
||||
notifiable_type: "Broadcast",
|
||||
action: welcome_broadcast.type_of,
|
||||
json_data: json_data,
|
||||
)
|
||||
Notifications::WelcomeNotificationJob.perform_later(receiver_id)
|
||||
end
|
||||
|
||||
def send_welcome_notification_without_delay(receiver_id)
|
||||
Notifications::WelcomeNotificationJob.perform_now(receiver_id)
|
||||
end
|
||||
handle_asynchronously :send_welcome_notification
|
||||
|
||||
def send_moderation_notification(notifiable)
|
||||
# notifiable is currently only comment
|
||||
|
|
|
|||
38
app/services/notifications/welcome_notification/send.rb
Normal file
38
app/services/notifications/welcome_notification/send.rb
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
# send welcome notification
|
||||
module Notifications
|
||||
module WelcomeNotification
|
||||
class Send
|
||||
def initialize(receiver_id, welcome_broadcast)
|
||||
@receiver_id = receiver_id
|
||||
@welcome_broadcast = welcome_broadcast
|
||||
end
|
||||
|
||||
delegate :user_data, to: Notifications
|
||||
|
||||
def self.call(*args)
|
||||
new(*args).call
|
||||
end
|
||||
|
||||
def call
|
||||
dev_account = User.dev_account
|
||||
json_data = {
|
||||
user: user_data(dev_account),
|
||||
broadcast: {
|
||||
processed_html: welcome_broadcast.processed_html
|
||||
}
|
||||
}
|
||||
Notification.create(
|
||||
user_id: receiver_id,
|
||||
notifiable_id: welcome_broadcast.id,
|
||||
notifiable_type: "Broadcast",
|
||||
action: welcome_broadcast.type_of,
|
||||
json_data: json_data,
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :receiver_id, :welcome_broadcast
|
||||
end
|
||||
end
|
||||
end
|
||||
22
spec/jobs/notifications/welcome_notification_job_spec.rb
Normal file
22
spec/jobs/notifications/welcome_notification_job_spec.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe Notifications::WelcomeNotificationJob, type: :job do
|
||||
include_examples "#enqueues_job", "send_welcome_notification"
|
||||
|
||||
describe "::perform_now" do
|
||||
let(:welcome_notification_service) { double }
|
||||
|
||||
before do
|
||||
create(:broadcast, :onboarding)
|
||||
allow(welcome_notification_service).to receive(:call)
|
||||
allow(User).to receive(:dev_account).and_return(create(:user))
|
||||
end
|
||||
|
||||
it "calls the service" do
|
||||
user = create(:user)
|
||||
|
||||
described_class.perform_now(user.id, welcome_notification_service)
|
||||
expect(welcome_notification_service).to have_received(:call).once
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe Notifications::WelcomeNotification::Send, type: :service do
|
||||
describe "::call" do
|
||||
before do
|
||||
allow(User).to receive(:dev_account).and_return(create(:user))
|
||||
end
|
||||
|
||||
it "checks a newly created welcome notification", :aggregate_failures do
|
||||
welcome_broadcast = create(:broadcast, :onboarding)
|
||||
welcome_notification = described_class.call(create(:user).id, welcome_broadcast)
|
||||
|
||||
expect(welcome_notification).to be_kind_of(Notification)
|
||||
expect(welcome_notification.notifiable_type).to eq "Broadcast"
|
||||
expect(welcome_notification.action).to eq "Onboarding"
|
||||
expect(welcome_notification.json_data["broadcast"]["processed_html"]).to eq welcome_broadcast.processed_html
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue