Implemented ActiveJob for badge_achievements#send_email_notification (#2962)
* Implemented ActiveJob for badge_achievements#send_email_notification * Sending email only if record not nil As per the review
This commit is contained in:
parent
6d1638ed68
commit
b16fa4f8cc
3 changed files with 37 additions and 2 deletions
10
app/jobs/badge_achievements/send_email_notification_job.rb
Normal file
10
app/jobs/badge_achievements/send_email_notification_job.rb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
module BadgeAchievements
|
||||
class SendEmailNotificationJob < ApplicationJob
|
||||
queue_as :badge_achievements_send_email_notification
|
||||
|
||||
def perform(badge_achievement_id)
|
||||
badge_achievement = BadgeAchievement.find_by(id: badge_achievement_id)
|
||||
NotifyMailer.new_badge_email(badge_achievement).deliver_now if badge_achievement
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -30,9 +30,12 @@ class BadgeAchievement < ApplicationRecord
|
|||
end
|
||||
|
||||
def send_email_notification
|
||||
NotifyMailer.new_badge_email(self).deliver if user.class.name == "User" && user.email.present? && user.email_badge_notifications
|
||||
BadgeAchievements::SendEmailNotificationJob.perform_later(id) if user.class.name == "User" && user.email.present? && user.email_badge_notifications
|
||||
end
|
||||
|
||||
def send_email_notification_without_delay
|
||||
BadgeAchievements::SendEmailNotificationJob.perform_now(id) if user.class.name == "User" && user.email.present? && user.email_badge_notifications
|
||||
end
|
||||
handle_asynchronously :send_email_notification
|
||||
|
||||
def award_credits
|
||||
Credit.add_to(user, 5)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe BadgeAchievements::SendEmailNotificationJob, type: :job do
|
||||
let(:user) { create(:user, email_badge_notifications: true) }
|
||||
let(:badge_achievement) { create(:badge_achievement, user: user) }
|
||||
|
||||
describe ".perform_later" do
|
||||
it "add job to the queue :badge_achievements_send_email_notification" do
|
||||
expect do
|
||||
described_class.perform_later(1)
|
||||
end.to have_enqueued_job.with(1).on_queue("badge_achievements_send_email_notification")
|
||||
end
|
||||
end
|
||||
|
||||
describe "#perform" do
|
||||
it "calls on NotifyMailer" do
|
||||
described_class.new.perform(badge_achievement.id) do
|
||||
expect(NotifyMailer).to have_received(:new_badge_email).with(badge_achievement)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue