Last active
December 4, 2017 23:37
-
-
Save laubstein/9ac6cb84167e52583fae6df90c3c3627 to your computer and use it in GitHub Desktop.
Bacalhau para apurar evolução do nubank rewards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// data = resultado da chamada ao serviço que retorna os eventos | |
let data = require('./data/nubank_events.json'); | |
let rewards_money = 0; | |
let rewards_points = 0; | |
let rewards_count = 0; | |
let hasReward = false; | |
let date = /^(.{10})T(.{8}).*$/; | |
let rewards_netflix = 2600; | |
function getDate(d) { | |
return d.replace(date, '$1 $2'); | |
} | |
function echo(v) { | |
console.log(getDate(v.time), v.category, v.description || '', v.amount || ''); | |
} | |
function getRewardBallance() { | |
return ((rewards_points / 100) - (rewards_count * rewards_netflix)).toFixed(2); | |
} | |
data.events.reverse().forEach(v => { | |
switch (v.category) { | |
case 'transaction': | |
// case 'transaction_reversed': | |
if (hasReward) { | |
echo (v); | |
rewards_points += v.amount; | |
// caso centavos sejam desconsiderados | |
// rewards_points += parseInt(v.amount / 100, 10) * 100; | |
// caso centavos sejam arredondados | |
// rewards_points += Math.round(v.amount / 100) * 100; | |
} | |
break; | |
case 'rewards_signup': | |
hasReward = true; | |
console.log('=====> REWARDS'); | |
echo(v); | |
break; | |
case 'rewards_redemption': | |
let amount = v.message.split('R$ ')[1].replace(',', '.'); | |
amount = parseFloat(amount); | |
rewards_money += amount; | |
console.log('====> SALDO ANTERIOR', getRewardBallance()); | |
rewards_count++; | |
echo(v); | |
console.log('====> SALDO POSTERIOR', getRewardBallance()); | |
break; | |
case 'anticipate_event': | |
break; | |
default: | |
hasReward && echo(v); | |
break; | |
} | |
}); | |
let result = []; | |
result.push(`== REWARDS ==`); | |
result.push(`Total acumulado: ${rewards_points / 100}`); | |
result.push(`Vezes utilizado: ${rewards_count}`); | |
result.push(`Total utilizado: ${rewards_count * rewards_netflix}`); | |
result.push(`Total apagado: R$ ${rewards_money}`); | |
result.push(`Saldo pontos atual: ${getRewardBallance()}`); | |
console.log(result.join('\n')); | |
/* | |
Para o meu caso: | |
== REWARDS == | |
Total acumulado: 17249.7 | |
Vezes utilizado: 6 | |
Total utilizado: 15600 | |
Total apagado: R$ 227.4 | |
Saldo pontos atual: 1649.70 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment