Skip to content

Instantly share code, notes, and snippets.

@jamescook
Last active August 29, 2015 14:22
Show Gist options
  • Save jamescook/a0eb5c1d1b709b69f86f to your computer and use it in GitHub Desktop.
Save jamescook/a0eb5c1d1b709b69f86f to your computer and use it in GitHub Desktop.
missing =CSV.read("/home/deploy/missing_debits_from_june8th.csv");nil
key= BunchBall.session_key
start_date = Time.parse("2015-06-01").to_i
end_date = Time.parse("2015-06-30").to_i
missing.shift #user_id, debit
#125672
def get_all_history(options)
params = {
:method => 'user.getPointsHistory',
:userId => options[:user_id],
:start => options[:start_date],
:end => options[:end_date].to_i,
:startIndex => options[:start_offset] || 0,
:pointCategory => 'Points',
:returnCount => 100,
:sessionKey => options[:key]
}
result = BunchBall.send_to_bunchball(params, nil).body
if result['Nitro']['PointsHistoryRecord'] == true
return []
end
records = Array.wrap(result['Nitro']['PointsHistoryRecord']['PointsHistoryItem'])
records.flatten
end
CSV.open("/tmp/really_fix_balances.csv", "wb") do |csv|
csv << ["user_id", "total balance", "june credits"]
missing.each do |user_id, debit|
next if user_id == 181499 # cafewellmonitor
user = User.find(user_id)
balance = BunchBall.get_current_balance(user: user, point_category: 'Points')
unless balance.is_a?(Hash)
puts "Error"
puts balance.inspect
next
end
history = get_all_history(user_id: user_id, start_date: start_date, end_date: end_date, key: key)
if history.size >= 100
done = false
start_offset = 0
until done
start_offset = start_offset + 101
r = get_all_history(user_id: user_id, start_date: start_date, end_date: end_date, key: key, start_offset: start_offset)
if r.empty? || history[-1] == r[-1]
done = true
end
history << r
end
history.flatten!
history.uniq{|r| r['ts'] }.select{|h| h['tags'].present? } # ignore corrections, which are untagged points
end
#puts credits.inspect
if history.empty? # No history in may
csv << [user_id, balance[:points], 0]
#next #Nothing to credit back, only perform the debit if they have a positive balance
end
credits = history.select{|item| item['action'] == 'credit' && item['tags'].present? }.map{|item| item['amount'].to_i }.sum #June credits
# Debit balance
Rails.logger.info "[BB!!] User #{user_id}: Balance is #{balance[:points]}, june credits are #{credits}"
#puts "[BB] User #{user_id}: Balance is #{balance[:points]}, june credits are #{credits}"
debit_success = false
if balance[:points] > 0
Rails.logger.info "[BB!!] Debitting #{balance[:points]} for #{user_id}"
#debit_success = BunchBall.debit_balance_points(user_id, 'Points', balance[:points])
#sleep 0.3
end
if credits > 0
Rails.logger.info "[BB!!] Crediting #{credits} for #{user_id}"
#BunchBall.credit_points_currency(user: user, updateLifetimeBalance: false, point_category: 'Points', points: credits)
#sleep 0.3
end
csv << [user_id, balance[:points], credits]
puts
puts
sleep 0.1
end;nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment