diff --git a/app/jobs/award_group.rb b/app/jobs/award_group.rb index 4094378..87ed61d 100644 --- a/app/jobs/award_group.rb +++ b/app/jobs/award_group.rb @@ -4,10 +4,11 @@ module Jobs every 1.minutes def execute(_args) - puts '====================== The Award Group Job was executed ===========================' user_queue.each do |email| user = User.find_by_email(email) - DiscourseDonations::Rewards.new(user).add_to_group(group_name) if user.present? + next if user.nil? + puts "Added user #{user.email} to #{group_name}" + DiscourseDonations::Rewards.new(user).add_to_group(group_name) end user_queue_reset end diff --git a/app/jobs/grant_badge.rb b/app/jobs/grant_badge.rb index 38b9fb1..2cb3d67 100644 --- a/app/jobs/grant_badge.rb +++ b/app/jobs/grant_badge.rb @@ -4,7 +4,27 @@ module Jobs every 5.minutes def execute(_args) - puts '====================== The Grant Badge Job was executed ===========================' + user_queue.each do |email| + user = User.find_by_email(email) + next if user.nil? + puts "Granted user #{user.email} with badge: #{badge_name}" + DiscourseDonations::Rewards.new(user).grant_badge(badge_name) + end + user_queue_reset + end + + private + + def user_queue + PluginStore.get('discourse-donations', 'badge:grant') || [] + end + + def user_queue_reset + PluginStore.set('discourse-donations', 'badge:grant', []) + end + + def badge_name + SiteSetting.discourse_donations_reward_badge_name end end end diff --git a/spec/jobs/grant_badge_spec.rb b/spec/jobs/grant_badge_spec.rb index 8c90dac..d9b75f2 100644 --- a/spec/jobs/grant_badge_spec.rb +++ b/spec/jobs/grant_badge_spec.rb @@ -5,7 +5,14 @@ RSpec.describe Jobs::GrantBadge, type: :job do before do SiteSetting.stubs(:discourse_donations_reward_badge_name).returns(badge.name) + PluginStore.set('discourse-donations', 'badge:grant', [users.first.email, users.last.email]) end - it 'grants all the users the badge' + it 'grants all the users the badge' do + Jobs::GrantBadge.new.execute(nil) + aggregate_failures do + expect(users.first.badges).to include(badge) + expect(users.last.badges).to include(badge) + end + end end