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

View File

@ -1,14 +1,29 @@
module Jobs module Jobs
class AwardGroup < ::Jobs::Scheduled class AwardGroup < ::Jobs::Scheduled
every 1.minute every 2.minutes
def execute(args) def execute
puts '====================== The Job was executed ===========================' puts '====================== The Job was executed ==========================='
user = User.find_by_email(args[:email]) user_queue.each do |email|
if user.present? user = User.find_by_email(email)
DiscourseDonations::Rewards.new(user).add_to_group(args[:group_name]) DiscourseDonations::Rewards.new(user).add_to_group(group_name) if user.present?
end 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 end
end end

View File

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

View File

@ -1,9 +1,18 @@
RSpec.describe Jobs::AwardGroup, type: :job do RSpec.describe Jobs::AwardGroup, type: :job do
it 'adds the user to a group' do let(:users) { [Fabricate(:user), Fabricate(:user)] }
user = Fabricate(:user) let(:grp) { Fabricate(:group, name: 'chimichanga') }
grp = Fabricate(:group)
Jobs::AwardGroup.new.execute(email: user.email, group_name: grp.name) before do
expect(user.groups).to include(grp) 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
end end