When user makes an in-app purchase, I want their tokens count to decrement by the cost. This will just delete the user's tokens
(This code used to work and broke recently)
var senderRef = db.ref(`users/${sender.id}/tokens`)
var amount = 1000
var tokensCount = await (await senderRef.once('value')).val()
console.log("SENDER TOKENS: ", tokensCount, sender.id)
await senderRef.transaction(async function(currentTokens) {
var newAmount = currentTokens - amount
if (newAmount >= 0) {
return newAmount
} else {
return
}
}).then(async (result) => {
var tokensCount = await (await senderRef.once('value')).val()
console.log("SENDER TOKENS AFTER TRANSACTION: ", tokensCount, sender.id)
})