add the users from the queue into the group

This commit is contained in:
Rimian Perkins 2017-04-27 18:28:38 +10:00
parent 898d1a32d5
commit 0a20a10237
4 changed files with 57 additions and 26 deletions

View File

@ -16,17 +16,20 @@ module DiscourseDonations
response['rewards'] = []
if reward_current_user?(payment)
reward = DiscourseDonations::Rewards.new(current_user)
if reward.add_to_group(group_name)
response['rewards'] << { type: :group, name: group_name }
end
if reward.grant_badge(badge_name)
response['rewards'] << { type: :badge, name: badge_name }
end
else
if group_name.present?
# Jobs.enqueue(:award_group, email: email, group_name: group_name)
if reward?(payment)
if current_user.present?
reward = DiscourseDonations::Rewards.new(current_user)
if reward.add_to_group(group_name)
response['rewards'] << { type: :group, name: group_name }
end
if reward.grant_badge(badge_name)
response['rewards'] << { type: :badge, name: badge_name }
end
elsif email.present?
if group_name.present?
store = PluginStore.get('discourse-donations', 'group:add') || []
PluginStore.set('discourse-donations', 'group:add', store << email)
end
end
end
@ -35,8 +38,12 @@ module DiscourseDonations
private
def reward_current_user?(payment)
current_user.present? && payment.present? && payment.successful?
def create_account
params[:create_account] == 'true'
end
def reward?(payment)
payment.present? && payment.successful?
end
def group_name

View File

@ -1,14 +1,29 @@
module Jobs
class AwardGroup < ::Jobs::Scheduled
every 1.minute
every 2.minutes
def execute(args)
def execute
puts '====================== The Job was executed ==========================='
user = User.find_by_email(args[:email])
if user.present?
DiscourseDonations::Rewards.new(user).add_to_group(args[:group_name])
user_queue.each do |email|
user = User.find_by_email(email)
DiscourseDonations::Rewards.new(user).add_to_group(group_name) if user.present?
end
user_queue_reset
end
private
def user_queue
PluginStore.get('discourse-donations', 'group:add') || []
end
def user_queue_reset
PluginStore.set('discourse-donations', 'group:add', [])
end
def group_name
SiteSetting.discourse_donations_reward_group_name
end
end
end

View File

@ -49,9 +49,9 @@ module DiscourseDonations
expect(response_rewards).to be_empty
end
it 'enqueues add to group' do
skip 'still working on this'
Jobs.expects(:enqueue).with(:award_group, email: params[:email], group_name: group_name)
it 'stores the email in group:add' do
PluginStore.expects(:get).with('discourse-donations', 'group:add').returns([])
PluginStore.expects(:set).with('discourse-donations', 'group:add', [params[:email]])
post :create, params
end

View File

@ -1,9 +1,18 @@
RSpec.describe Jobs::AwardGroup, type: :job do
it 'adds the user to a group' do
user = Fabricate(:user)
grp = Fabricate(:group)
Jobs::AwardGroup.new.execute(email: user.email, group_name: grp.name)
expect(user.groups).to include(grp)
let(:users) { [Fabricate(:user), Fabricate(:user)] }
let(:grp) { Fabricate(:group, name: 'chimichanga') }
before do
SiteSetting.stubs(:discourse_donations_reward_group_name).returns(grp.name)
PluginStore.set('discourse-donations', 'group:add', [users.first.email, users.last.email])
end
it 'adds the users to the group' do
Jobs::AwardGroup.new.execute
aggregate_failures do
expect(users.first.groups).to include(grp)
expect(users.last.groups).to include(grp)
end
end
end