add method to award and remove credits (#2681)
This commit is contained in:
parent
4013cf45de
commit
ae228cfe1f
2 changed files with 14 additions and 6 deletions
|
|
@ -62,16 +62,12 @@ class Internal::UsersController < Internal::ApplicationController
|
|||
|
||||
def add_credits
|
||||
amount = user_params[:add_credits].to_i
|
||||
credit_objects = []
|
||||
amount.times do
|
||||
credit_objects << Credit.new(user_id: @user.id)
|
||||
end
|
||||
Credit.import credit_objects
|
||||
Credit.add_to(@user, amount)
|
||||
end
|
||||
|
||||
def remove_credits
|
||||
amount = user_params[:remove_credits].to_i
|
||||
@user.credits.where(spent: false).limit(amount).delete_all
|
||||
Credit.remove_from(@user, amount)
|
||||
end
|
||||
|
||||
def user_status
|
||||
|
|
|
|||
|
|
@ -3,4 +3,16 @@ class Credit < ApplicationRecord
|
|||
|
||||
belongs_to :user, optional: true
|
||||
belongs_to :organization, optional: true
|
||||
|
||||
def self.add_to(user, amount)
|
||||
credit_objects = []
|
||||
amount.times do
|
||||
credit_objects << Credit.new(user_id: user.id)
|
||||
end
|
||||
Credit.import credit_objects
|
||||
end
|
||||
|
||||
def self.remove_from(user, amount)
|
||||
user.credits.where(spent: false).limit(amount).delete_all
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue